2

I was using parseInt() to get some big prime numbers, but it's working as expected. For example,

parseInt("18014398241046527", 10); // Gives me 18014398241046528??
parseInt("18014398241046528", 10); // Gives me 18014398241046528
parseInt("18014398241046529", 10); // Still gives me 18014398241046528??

I tested them on both Chrome version 20.0.1132.47 and Firefox 12.0. Was this because the numbers that I was trying to parse were too large?

3
  • 1
    Not too large: too many significant digits. Commented Jul 5, 2012 at 6:17
  • P.S. Not a problem with parseInt(), just the way JS numbers work. There are a few JS libraries around that can handle more significant digits, e.g., BigNumber. Commented Jul 5, 2012 at 6:24
  • It seems there is a pattern for overflowing here. It all starts from 2 bytes and 01, 10, 11 will be 10 and 00 will be 00<pre><code>0 '->' 11111111111111110 1 '->' 11111111111111112 2 '->' 11111111111111112 3 '->' 11111111111111112 4 '->' 11111111111111114 5 '->' 11111111111111116 6 '->' 11111111111111116 7 '->' 11111111111111116 8 '->' 11111111111111118</code></pre> Commented Jul 5, 2012 at 7:19

2 Answers 2

5

A number in JavaScript is floating point double precision, which can only contain up to 53-bit of precision (approximately 16 decimal digits). The number you have in the question is 17 digits, so it cannot store the number exactly.

Wikipedia article on JavaScript syntax/Number.

Reference to JavaScript (1.1) standard.

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

Comments

0

This is not an issue with parseInt; this number is too long (too precise - see nhahtdh's answer) for JavaScript's Number data type (proof: +"18014398241046527" yields 18014398241046528 as well).

I think the only way to work around this is to input the number as a string and work on it in chunks.

7 Comments

"...proof..."? You can't be seious. Prove it to me with the underlying V8 and SpiderMonkey codes, but not like this
@Alexander - The fact that other string to number conversion methods have the same issue is proof that the "problem" isn't just with parseInt(). Though nhahtdh's answer is significantly more precise (Ha! See what I did there?) as to what's actually happening. The underlying JS engine code is not relevant here.
@nnnnnn, I will put it in other words: cite the standard, at the very least. That "proof" still doesn't converge anything useful. The JS engine implementation can end up be wrong, but with this amount of information, you just can't know.
@Alexander while identical results do not, of necessity, have the same cause, the combination of identical results and the fact that both match expected behavior make it very likely that we're not just seeing some bug. Along the same lines, I cannot be certain that I haven't been infected with some strange virus that perfectly mimics stackoverflow.com, yet I take my interfacing with this website as proof that it is online. "Proof" is not an absolute word; it is an indication of overwhelming evidence, which I think I have provided.
@Alexander if you think further evidence should be provided then you're welcome to edit the answer. I don't want to spend the time digging around for it because I don't think it is necessary.
|

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.