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

5
  • Can you show us your code; you might be going wrong with this somewhere.. Your PHP code that is Commented Apr 17, 2013 at 22:34
  • My PHP code is pretty old, so don't blame the coding... I'm trying to do this in JavaScript: pastebin.com/MYiTc4rh - Inputs would be for example STEAM_0:0:1023 for the first function. The second function works with 76561197960267774 Commented Apr 17, 2013 at 22:39
  • 2
    +/- 9007199254740992 is the range of integer (2<sup>63</sup>) for JS. Commented Apr 17, 2013 at 22:42
  • 1
    Relevant: stackoverflow.com/questions/4288821/… Commented Apr 17, 2013 at 22:47
  • stackoverflow.com/questions/3072307/… Commented Apr 17, 2013 at 23:02

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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 `

1 Comment

I now use BigNumber - works fine. Thanks for your answer!
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

Comments

-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)

2 Comments

Please share more details. How do these two links explain or resolve the initial problem?
@NicoHaase Provided Details as per initial problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.