JavaScript 64 bit numeric precision

妖精的绣舞 提交于 2019-12-08 18:00:50

问题


Is there a way to represent a number with higher than 53-bit precision in JavaScript? In other words, is there a way to represent 64-bit precision number?

I am trying to implement some logic in which each bit of a 64-bit number represents something. I lose the lower significant bits when I try to set bits higher than 2^53.

Math.pow(2,53) + Math.pow(2,0) == Math.pow(2,53)

Is there a way to implement a custom library or something to achieve this?


回答1:


Google's Closure library has goog.math.Long for this purpose.




回答2:


The GWT team have added a long emulation support so java longs really hold 64 bits. Do you want 64 bit floats or whole numbers ?




回答3:


I'd just use either an array of integers or a string.

The numbers in javascript are doubles, I think there is a rounding error involved in your equation.




回答4:


Perhaps I should have added some technical detail. Basically the GWT long emulation uses a tuple of two numbers, the first holding the high 32 bits and the second the low 32 bits of the 64 bit long.

The library of course contains methods to add stuff like adding two "longs" and getting a "long" result. Within your GWT Java code it just looks like two regular longs - one doesn't need to fiddle or be aware of the tuple. By using this approach GWT avoids the problem you're probably alluding to, namely "longs" dropping the lower bits of precision which isn't acceptable in many cases.

Whilst floats are by definition imprecise / approximations of a value, a whole number like a long isn't. GWT always holds a 64 bit long - maths using such longs never use precision. The exception to this is overflows but that accurately matches what occurs in Java etc when you add two very large long values which require more than 64 bits - eg 2^32-1 + 2^32-1.

To do the same for floating point numbers will require a similar approach. You will need to have a library that uses a tuple.




回答5:


The following code might work for you; I haven't tested it however yet: BigDecimal for JavaScript




回答6:


Yes, 11 bit are reserved for exponent, only 52 bits containt value also called fraction. Javascript allows bitwise operations on numbers but only first 32 bits are used in those operations according to Javascript standard specification.

I do not understand misleading GWT/Java/long answers in Javascript/double question though? Javascript is not Java.




回答7:


Why would anyone need 64 bit precision in javascript ?

Longs sometimes hold ID of stuff in a DB so its important not to lose some of the lower bits... but floating point numbers are most of the time used for calculations. To use floats to hold monetary or similar exacting values is plain wrong. If you truely need 64 bit precision do the maths on the server where its faster and so on.



来源:https://stackoverflow.com/questions/506200/javascript-64-bit-numeric-precision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!