2

Does anyone know why the following would return the third item's value + 1 rather than just returning the item's correct value?

JSFiddle: http://jsfiddle.net/gz4gA/

var items = [10150778347353933, 95698693940, 10151139500303463, 337394396325739, 181838311261, 186661577450, 131239636262, 408877845803303, 130260147046659, 10151850964430652, 190553376302, 256062744215, 73099117435, 30571361895, 85358203371, 133621058800, 67899501771, 422515109312, 56271008850, 107997326726, 382312857465, 144163385597840, 10150882307310061, 184244804960462, 63953746442, 114588455254586, 201992215550, 401799715673, 287217811299764, 96668113887, 106824841365, 130084127023410, 186061480932, 60351005403, 209693813195, 10150593297330601, 319622191193, 68686268942, 290590784383, 63467856465, 470235003273, 127564720953, 127437064077, 102351319089, 113530295368, 82102942060, 290699835908, 81445359355, 137490940846, 119104781632, 415115129332, 120520318032430, 142666108227, 146711457500, 344723925588563, 239108779476988, 121853939741, 109322101963, 54456613147, 473850413696, 10150329755772674];
console.log(items[2]);

2
  • 2
    Possibly exceeded the maximum integer that javascript can represent? Commented Oct 30, 2012 at 5:09
  • 1
    Item codes are better represented as strings. Saves the problem of leading zeroes disappearing as well. Commented Oct 30, 2012 at 5:19

2 Answers 2

4

JavaScript numbers are 64-bit floating point values. The largest integer value that can be represented exactly is 253, or 9007199254740992. Numbers larger than that (including the 3rd element of your array) are represented only approximately. See, for instance, ECMA 262-5, section 8.5.

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

3 Comments

Thanks for the quick response and explanation!
That isn't the largest integer value that can be represented exactly, see Number.MAX_VALUE.
@RobG - Well, yes, some larger integers can be represented exactly. That was poorly worded. What I should have said was that it was the largest of the range integers without gaps that can be represented. Beyond that, you begin to find integers that cannot be represented. (Note, for instance, that Number.MAX_VALUE - 1 is not representable in JavaScript. console.log(Number.MAX_VALUE == (Number.MAX_VALUE-1)) will print true.)
2

Per this answer https://stackoverflow.com/a/307200/816620, the top of the range where javascript can represent all integers is: 9007199254740992 which is smaller than your 10151139500303463. So, your number is too large.

If these are something like item codes rather than something you really need to use as a number, then perhaps you should store them as strings, not as numbers.

2 Comments

That isn't correct. The sequence of continuous integers that can be represented exactly is -2^53 to +2^53. There are many more that can be represented exactly, but they aren't continuous. The largest integer is given by Number.MAX_VALUE, which is approximately 1.7976931348623157 × 10^308, and Number.MIN_VALUE, which is approximately 5 × 10^‑324.
@RobG - Rob, you're being kind of technical here and not really addressing the context of the OP's question. Max integer in this context means the OP's integer is beyond the range where javascript can represent any integer exactly.

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.