views:

1220

answers:

4

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

I am trying to implement a logic in which each bit of a 64bit number represents something. I loose 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?

TIA

-Rakesh

+1  A: 

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 ?

mP
whole numbers. I will look at GWT implementation. thanks.
A: 

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.

Georg
I think arrays and string would be an inefficient datastructure here. Because I would be using only few of the 64 bits. Currently I am thinking of using 2 numeric types for higher 32 and lower 32 bits. Thanks
The problem is you can't rely on a double to be consistent if you try to do bit-manipulation. Javascript isn't made for manipulating data on the bit-layer.
Georg
Totally get your point. I am an expert on javascript or floating point numeric, is there a datatype in js which uses fixed point arithmetic?
No, it's sad but true. :( But for a normal web application it's just not needed.
Georg
+1  A: 

Perhaps i should have added some technical detail. Basically teh 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 doesnt need to fiddle or be aware of the tuple. By using this approach GWt avoids the problem your probably alluding too, namely "longs" dropping the lower bits of precision which isnt acceptable in many cases.

Whilst floats are by definition imprecise / approximations of a value a whole number like a long isnt. 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.

mP
A: 

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.

mP
Answering a question with a question is never helpful. Plus, server-side JavaScript is becoming more and more common.
scotts
Because maybe just maybe the asker is doing the wrong thing, because they don't know what they are doing...
mP