Does Javascript handle integer overflow and underflow? If yes, how?

24,744

Solution 1

In a simple test, when I try this:

var max = Number.MAX_VALUE;
var x = max + 10;

var min = Number.MIN_VALUE;
var y = min / 10;

I find that x and max have the same value (in Chrome, IE and Firefox) so it appears that some overflows are just pegged to the max value. And, y gets pegged to 0 so some underflows seem to go to zero.

Ahhh, but it is not quite that simple. Not all overflows go to Number.MAX_VALUE and not all underflows go to Number.MIN_VALUE. If you do this:

var max = Number.MAX_VALUE;
var z = max * 2;

Then, z will be Infinity.

It turns out that it depends upon how far you overflow/underflow. If you go too far, you will get INFINITY instead. This is because of the use of IEEE 754 round-to-nearest mode where the max value can be considered nearer than infinity. See Adding to Number.MAX_VALUE for more detail. Per that answer, values of 1.7976931348623158 × 10308 or greater round to infinity. Values between Number.MAX_VALUE and that will round to Number.MAX_VALUE.

To, make things even more complicated, there is also something as gradual underflow which Javascript supports. This is where the mantissa of the floating point value has leading zeroes in it. Gradual underflow allows floating point to represent some smaller numbers that it could not represent without that, but they are represented at a reduced precision.

You can see exactly where the limits are:

>>> Number.MAX_VALUE + 9.979201e291
1.7976931348623157e+308
>>> Number.MAX_VALUE + 9.979202e291
Infinity

Here's a runnable snippet you can try in any browser:

var max = Number.MAX_VALUE;
var x = max + 10;

var min = Number.MIN_VALUE;
var y = min / 10;

var z = max * 2;

document.getElementById("max").innerHTML = max;
document.getElementById("max10").innerHTML = x;
document.getElementById("min").innerHTML = min;
document.getElementById("min10").innerHTML = y;
document.getElementById("times2").innerHTML = z;
body {
    font-family: "Courier New"; 
    white-space:nowrap;
}
Number.MAX_VALUE &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= <span id="max"></span><br>
Number.MAX_VALUE + 10 = <span id="max10"></span><br>
<br>
Number.MIN_VALUE &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= <span id="min"></span><br>
Number.MIN_VALUE / 10 = <span id="min10"></span><br>  
<br>
Number.MAX_VALUE * 2 &nbsp;= <span id="times2"></span><br>

Solution 2

The maximum and minimum is +/- 9007199254740992

Try these Number type properties:

alert([Number.MAX_VALUE, Number.MIN_VALUE]);

From the ECMAScript 2020 language specification, section "The Number Type":

Note that all the positive and negative mathematical integers whose magnitude is no greater than 253 are representable in the Number type (indeed, the mathematical integer 0 has two representations, +0 and −0).

Test:

var x = 9007199254740992;
var y = -x;
x == x + 1; // true !
y == y - 1; // also true !
Share:
24,744

Related videos on Youtube

Jérôme Verstrynge
Author by

Jérôme Verstrynge

You can contact me via my LinkedIn profile.

Updated on July 05, 2022

Comments

  • Jérôme Verstrynge
    Jérôme Verstrynge almost 2 years

    We know that Java does not handle underflows and overflows, but how does Javascript handle these for integers?

    Does it go back to a minimum/maximum? If yes, which minimum/maximum?

    I need to split a string and compute a hash value based on its characters.

  • Jérôme Verstrynge
    Jérôme Verstrynge over 10 years
    Ok, but how does JS handle overflow and underflows?
  • jfriend00
    jfriend00 over 10 years
    I don't think this actually answers the OP's question.
  • Rahul Tripathi
    Rahul Tripathi over 10 years
    @jfriend00:- Updated my answer with a link which can help. Do correct me if I am completely missing the obvious!!! :)
  • Jérôme Verstrynge
    Jérôme Verstrynge over 10 years
    Rahul, did you actually run alert([Number.MAX_VALUE, Number.MIN_VALUE]); in JsFiddle? I don't think Number.MIN_VALUE is what you think...
  • Rahul Tripathi
    Rahul Tripathi over 10 years
    @JVerstry:- I have just given an idea of showing that in JsFiddle..Is that making a wrong sense??
  • Jérôme Verstrynge
    Jérôme Verstrynge over 10 years
    I tried with -Number.MAX_VALUE and they are also the same. So no going back to minimum and maximum....
  • jfriend00
    jfriend00 over 10 years
    Added info for underflow too.
  • Bernhard Hofmann
    Bernhard Hofmann about 10 years
    Yet Number.MAX_VALUE * 2 results in Infinity. "Go figure" as they say.
  • HopefullyHelpful
    HopefullyHelpful over 8 years
    With floating point numbers addition of a x small number with with a much bigger number (2^54*x) is simply ignored due to rounding, while multiplication is can always be executed (until the exponent overflows). For anyone left wondering of bernhard result.
  • Isaac
    Isaac over 6 years
    1.7976931348623157e+308 + 0.0000000000000001e+308 == Infinity
  • jfriend00
    jfriend00 almost 6 years
    Added more info about overflow to Infinity.
  • MonteCristo
    MonteCristo over 4 years
    Link in the answer is SPAM