I have looked everywhere but I couldnt find any information related to this topic. Also, Is there a java - like Long / BigDecimal datatype in dart?
3 Answers
Dart 2
For dart2js generated JavaScript, Pixel Elephant's answer is still true.
within the range -253 to 253
Other execution platforms have fixed-size integers with 64 bits.
The type BigInt was added to typed_data
Since Dart 2.0 will switch to fixed-size integers, we also added a
BigIntclass to thetyped_datalibrary. Developers targeting dart2js can use this class as well. TheBigIntclass is not implementing num, and is completely independent of the num hierarchy.
Dart 1
There are also the packages
-
3So beginning with Dart 2.0, is the range of
intfor Android and iOS same which is-2^63 to 2^63 - 1Commented Mar 22, 2019 at 12:46
That depends if you are running in the Dart VM or compiling to JavaScript.
On the Dart VM an int is arbitrary precision and has no limit.
When compiling to JavaScript you are actually using floating point numbers, since that is all that JavaScript supports. This means you are restricted to representing integers within the range -253 to 253
-
1What do you mean by arbitrary precision? Like a bignum? The documentation says it's 64 bit on Dart VM Commented Jun 7, 2019 at 11:10
-
1So here's a basic explanation: yes, most languages do have a defined limit. This will typically match the underlying hardware - commonly either 32-bit or 64-bit. Some languages however will implement numbers with arbitrary precision for flexibility's sake (see Python). Arbitrary precision takes some special coding, because your computer can only do arithmetic only at most 64-bit numbers, so when a language uses arbitrary precision, they're typically using a special implementation that eventually breaks arithmetic down into smaller 64-bit chunks. @DanielGomezRico– widaviesCommented Sep 17, 2020 at 2:11
-
1@DanielGomezRico Read more: rushter.com/blog/python-integer-implementation/…. Its also important to note that arbitrary precision numbers are a bit slower than using the native.– widaviesCommented Sep 17, 2020 at 2:12
-
2This is no longer true - see the other answer stackoverflow.com/a/31691112/2215742– CactuxCommented Mar 25, 2021 at 10:19
-
2In modern Dart the integers are not arbitrary precision any more. See more about this decision here: groups.google.com/a/dartlang.org/g/core-dev/c/ZdEBRgxLb70/m/… Commented Jun 14, 2022 at 13:24
First option:
int maxInteger = double.maxFinite.toInt();
Better way:
const maxInteger = -1 >>> 1;
The >>> right shift with zero padding
will transform 1111 ... 1111 (-1 representation) to 0111 ... 1111 (max int representation)
const minInteger = -maxInteger;