cyberwombat
cyberwombat

Reputation: 40064

How is small integer overflow avoided in Javascript

I am reading Douglas Crockford's book - Javascript the good parts - and he says:

JavaScript has a single number type. Internally, it is represented as 64-bit floating point, the same as Java’s double. Unlike most other programming languages, there is no separate integer type, so 1 and 1.0 are the same value. This is a significant convenience because problems of overflow in short integers are completely avoided...

I am not overly familiar with other languages so would like a bit of an explanation. I can understand why a 64 bit helps but his statement seems to apply to the lack of floats and doubles.

What would be (pseudo code perhaps) an example of a short integer overflow situation that wont occur in JS?

Upvotes: 14

Views: 390

Answers (1)

Andrew Shepherd
Andrew Shepherd

Reputation: 45222

Suppose you had an 8 bit unsigned number.

Here are a selection of digital and binary representations:

1: 00000001

2: 00000010

15: 00001111

255: 11111111

If you have 255 and add 1, what happens? There's no more bits, so it wraps around to

0: 00000000

Here's a demonstration in C# using uint (an unsigned 32-bit integer)

using System;

public class Program
{
    public static void Main()
    {
        uint n = 4294967294;
        for(int i = 0; i < 4; ++i)
        {
            n = n + 1;
            Console.WriteLine("n = {0}", n); 
        }

    }
}

This will output:

n = 4294967294
n = 4294967295
n = 0
n = 1

This is the problem you don't get in javascript.


You get different problems.

For example:

var n = 9007199254740991;
var m = n + 1;
var p = m + 1;
alert('n = ' + n + ' and m = ' + m + ' and p = ' + p);

You will see:

n = 9007199254740991 and m = 9007199254740992 and p = 9007199254740992

Rather than wrapping around, your number representations will shed accuracy.


Note that this 'shedding accuracy' behavior is not unique to javascript, it's what you expect from floating-point data types. Another .NET example:

using System;

public class Program
{
    public static void Main()
    {
        float n = 16777214; // 2^24 - 2
        for(int i = 0; i < 4; ++i) 
        {
            Console.WriteLine(string.Format("n = {0}", n.ToString("0")));
            Console.WriteLine("(n+1) - n = {0}", (n+1)-n);
            n = n + 1;                
        }
    }
}

This will output:

n = 16777210
(n+1) - n = 1
n = 16777220
(n+1) - n = 1
n = 16777220
(n+1) - n = 0
n = 16777220
(n+1) - n = 0

Upvotes: 9

Related Questions