'Is there a constant for max/min int/double value in dart?

Is there a constant in dart that tells us what is the max/min int/double value ?

Something like double.infinity but instead double.maxValue ?



Solution 1:[1]

For double there are

double.maxFinite (1.7976931348623157e+308)
double.minPositive (5e-324)

In Dart 1 there was no such number for int. The size of integers was limited only by available memory

In Dart 2 int is limited to 64 bit, but it doesn't look like there are constants yet.

For dart2js different rules apply

When compiling to JavaScript, integers are therefore restricted to 53 significant bits because all JavaScript numbers are double-precision floating point values.

Solution 2:[2]

I found this from dart_numerics package.

enter image description here

Solution 3:[3]

here you are the int64 max value:

const int intMaxValue = 9223372036854775807;

for dart web is 2^53-1:

const int intMaxValue = 9007199254740991;

"The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent integers between -(2^53 - 1) and 2^53 - 1." see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER

Solution 4:[4]

No,

Dart does not have a built-in constant for the max value of an int

but this is how to get the max value

Because ints are signed in Dart, they have a range (inclusive) of [-2^31, 2^31-1] if 32-bit and [-2^63, 2^63 - 1] if 64-bit. The first bit in an int is called the 'sign-bit'. If the sign-bit is 1, the int is negative; if 0, the int is non-negative. In the max int, all the bits are 1 except the sign bit, which is 0. We can most easily achieve this by writing the int in hexadecimal notation (integers preceded with '0x' are hexadecimal):

int max = 0x7fffffff; // 32-bit
int max = 0x7fffffffffffffff; // 64-bit

In hexadecimal (a.k.a. hex), each hex digit specifies a group of 4 bits, since there are 16 hex digits (0-f), there are 2 bit digits (0-1), and 2^4 = 16. There is a compile error if more bits than the bitness are specified; if fewer bits than the bitness were specified, then the hexadecimal integer will be padded with 0's until the number of bits is the bitness. So, to indicate that all the bits are 1 except for the sign-bit, we will need to use bitness / 4 hex characters (e.g. 16 for 64-bit architecture). The first hex character will represent the binary integer '0111' (7), which is 0x7, and all the other hex characters will represent the binary integer '1111' (15), or 0xf.

Alternatively, you could use bit-shifting, which I will not explain, but feel free to Google it.

int bitness = ... // presumably 64
int max = (((1 << (bitness - 2)) - 1) << 1) + 1;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lrn
Solution 2 Pinkesh Darji
Solution 3
Solution 4 jamesdlin