Solution 1 :
That the computation works on the web is basically an artifact of the Dart web implementation where Dart is transpiled to JavaScript and allows Dart’s type abstractions to break down somewhat.
Since the values in your 8 * pow(10, 24)
expression are all int
literals, the computation is done with integer arithmetic.
On the web, Dart int
s are backed by JavaScript number primitives, which are double-precision floating pointer numbers.
On your phone, you’re using the Dart VM, where int
s are always fixed-width integers with typical fixed-width integer limitations. The Dart VM uses 64-bit signed integers. The maximum value is 263 – 1, which is roughly 9.2×1018. You’re trying to compute 8×1024, which is much larger, so your computation overflows and ends up wrapping around to a negative value.
To fix this you could force using floating-point arithmetic by using a double
literal:
8 * pow(10.0, 24)
but in this case you could just use a double
literal directly and avoid the computation: 8e24
.
Problem :
I created an App in Flutter, it is a unit converter, has over 815 conversions, there are 815 functions, when I build and install apk file on my phone, it return negative integer on one conversion(bits to Yottabytes), but the same function returns positive integer on web.
I’ve imported ‘dart:math’ in my dart file
double bitsToYottabytes(double n) {return n/(8*pow(10,24));}