What Are the Limits of Numeric Data Types in JS and Dart

Here I will explain the limits of the Number data type in JS and the Double,Int types in Dart. I will not deal with “pure” JS here, but with the JS you compile from Dart. If you have an integer in JavaScript, it is very simple: the correct limit is given by MDN, which is 9007199254740991 for positive values and -9007199254740992 for negative ones. I wrote this code in Dart:

DART
import 'dart:html' as Html;
void main()
{
int highest = 9007199254740992;
int highestMinusOne = highest - 1;
int highestPlusOne = highest + 1;
int highest2 = 9007199254740991;
int highest2MinusOne = highest2 - 1;
int highest2PlusOne = highest2 + 1;
Html.window.console.log(highest.toString());
Html.window.console.log(highestMinusOne.toString());
Html.window.console.log(highestPlusOne.toString());
Html.window.console.log(highest2.toString());
Html.window.console.log(highest2MinusOne.toString());
Html.window.console.log(highest2PlusOne.toString());
}

And ran it in Dartium. The result was this:

MaxNumber in Javascript
As you can see, in Dartium and Dart the JavaScript limit has no meaning at all. Unfortunately, I could not find on Google what the actual limit of a Dart int in Dartium is. Then I compiled the program to JavaScript and ran it in Opera 26. The result was a bit different:
MaxNumber Plus in Javascript
As you can surely see, line 3 shows the same number as the first line, which means that the number was not incremented. However, I did not test whether it would behave the same way in pure (uncompiled) JS. That covers the int data type, and now let’s look at Double.

A simple rule settles it all: to get the same number in both JS and Dart, you need to follow the rule of max. 15 nines (or other digits). It does not matter where you put the decimal point, and you can also put just a zero before or after the decimal point. As long as the remaining number of digits is max. 15, neither Dart nor JS will mangle the number. So all of these numbers are OK:

  • 9.99999999999999
  • 99999999999999.9
  • 999999999999999.0 (here JavaScript will not print the trailing .0, but the number is otherwise exactly the same)
  • 0.999999999999999
  • 99999999.9999999
Categories

Leave a Reply