4

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

CREEATION
  • 232
  • 1
  • 10
  • Can you show us your code; you might be going wrong with this somewhere.. Your PHP code that is – Daryl Gill Apr 17 '13 at 22:34
  • My PHP code is pretty old, so don't blame the coding... I'm trying to do this in JavaScript: http://pastebin.com/MYiTc4rh - Inputs would be for example STEAM_0:0:1023 for the first function. The second function works with 76561197960267774 – CREEATION Apr 17 '13 at 22:39
  • 2
    +/- 9007199254740992 is the range of integer (263) for JS. – zkanoca Apr 17 '13 at 22:42
  • 1
    Relevant: http://stackoverflow.com/questions/4288821/how-to-deal-with-big-numbers-in-javascript – aug Apr 17 '13 at 22:47
  • http://stackoverflow.com/questions/3072307/is-there-a-bignum-library-for-javascript – zad Apr 17 '13 at 23:02

4 Answers4

3

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 `

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
3

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.

multimediaxp
  • 9,348
  • 13
  • 49
  • 80
1

You could use the bcmath extension for PHP and the JS conversion of it to get consistant results across the 2 languages:

bcadd('76561197960267774', '76561197960267774');
// 153122395920535548 

http://jsfiddle.net/zELmm/

http://phpjs.org/functions/bcadd/

http://php.net/manual/en/function.bcadd.php

Petah
  • 45,477
  • 28
  • 157
  • 213
-1

Update:-

BigInt was added as a native features of JavaScript.

But still there are some precision error on round off, A workaround Comparison

For example here full code for comparing BigInt with Number in JavaScript

As per Initial Problem Here :-

PHP: 76561197960267774 (the right result)

var num = BigInt( 1023 * 2 ) + 76561197960265728n;

console.log(num.toString()); 

/* 
num.toString() removes n at the end of digit.

Output: 76561197960267774
*/

Where bigint, created by appending n to the end of an integer literal or by calling the BigInt() constructor.

So after using BigInt:-

JavaScript: 76561197960267774 (the right result)

Deepender
  • 67
  • 7