15

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?

1

3 Answers 3

15

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 BigInt class to the typed_data library. Developers targeting dart2js can use this class as well. The BigInt class is not implementing num, and is completely independent of the num hierarchy.

Dart 1

There are also the packages

1
  • 3
    So beginning with Dart 2.0, is the range of int for Android and iOS same which is -2^63 to 2^63 - 1
    – CopsOnRoad
    Commented Mar 22, 2019 at 12:46
15

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

9
  • 1
    What do you mean by arbitrary precision? Like a bignum? The documentation says it's 64 bit on Dart VM
    – cubuspl42
    Commented Jun 7, 2019 at 11:10
  • 1
    So 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
    – widavies
    Commented 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.
    – widavies
    Commented Sep 17, 2020 at 2:12
  • 2
    This is no longer true - see the other answer stackoverflow.com/a/31691112/2215742
    – Cactux
    Commented Mar 25, 2021 at 10:19
  • 2
    In 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/…
    – Erik Corry
    Commented Jun 14, 2022 at 13:24
3

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.