【发布时间】:2016-03-02 00:53:27
【问题描述】:
Number() function returns incorrect values on some arguments, like this:
Number('10000000712224641') returns 10000000712224640
Number('10000000544563531') returns 10000000544563532
I tested this on Firefox, Chome, IE and Node.js. Why is this happening?
【问题讨论】:
-
Betting on floating point precision.
-
JavaScript (as well as every other programming language) have well documented examples of problems like this.
-
@JuanMendes Aren't all numbers in JS stored the same way?
-
@JuanMendes: It certainly looks like floating point precision to me. Overflow would lead to values becoming negative or otherwisewildlywrong.
-
@JonSkeet The first time John Skeet has said something to me... Reviewed my answer to explain that some integers above
Number.MAX_SAFE_INTEGERcan be expressed and yes, it is because of precision
标签: javascript