JavaScript VS PHP: The same calculation returns different result

JavaScript VS PHP: The same calculation returns different result

I’m trying to do some simple math (?) in JavaScript:

( 1023 * 2 ) + 76561197960265728;

I already did the same calculation in PHP, but the results are different:

JavaScript: 76561197960267780

PHP: 76561197960267774 (the right result)

I then tried the following:
http://jsfiddle.net/YxBa4/

Is there a “limit” in JavaScript for high numbers in calculations?

//edit:

Thanks for your answers, I now use the BigNumber one.

My full working code now:

on JSFiddle

In Javascript, numbers are 64 bit floating point values. The largest integer (magnitude) is 253, or Math.pow(2,53), or 9007199254740992. taken from: http://notepad2.blogspot.ca/2012/04/maximum-integer-in-javascript.html

You are 1351079458714420 over the limit.

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that’s 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5. taken from http://php.net/manual/en/language.types.integer.php

So basically PHP allows you more capacity for Integer values according to the PHP configuration.

The value 76561197960265730 id greater than the maximum allowed number size in javascript. Note that there are no real integers in Javascript just the Number type which is always a 64bit floating point value and platform independent. But the largest possible integer value is just 2^53 because 11 bits are at least reserved for the numbers after the comma and the signage bit.

In your example you can see this by simply doing:

alert(76561197960265728);

What will give you already an error, even without any calculations. (output: ‘76561197960265730’)

In php the maximum integer value depends on your system. If you are on a 64 bit system, the MAX integer value is 2^64 what is greater than (2 * 1023) + 76561197960265728. That’swhy the calculation succeeded in PHP – on a 64 bit system

In PHP you can detect the maximum integer size on your system by reading the constant PHP_INT_MAX and `

.
.
.
.