Large numbers erroneously rounded in JavaScript

后端 未结 6 1125
夕颜
夕颜 2020-11-21 23:58

See this code:



        
6条回答
  •  后悔当初
    2020-11-22 00:26

    JavaScript uses double precision floating point values, ie a total precision of 53 bits, but you need

    ceil(lb 714341252076979033) = 60
    

    bits to exactly represent the value.

    The nearest exactly representable number is 714341252076979072 (write the original number in binary, replace the last 7 digits with 0 and round up because the highest replaced digit was 1).

    You'll get 714341252076979100 instead of this number because ToString() as described by ECMA-262, §9.8.1 works with powers of ten and in 53 bit precision all these numbers are equal.

提交回复
热议问题