views:

390

answers:

1

I have some inputs on my site representing floating point numbers with up to ten precision digits (in decimal). At some point, in the client side validation code, I need to compare a couple of those values to see if they are equal or not, and here, as you would expect, the intrinsics of IEEE754 make that simple check fails with things like (2.0000000000==2.0000000001) = true.

I may break the floating point number in two longs for each side of the dot, make each side a 64 bit long and do my comparisons manually, but it looks so ugly!

Any decent Javascript library to handle arbitrary (or at least guaranteed) precision float numbers on Javascript?

Thanks in advance!

PS: A GWT based solution has a ++

+1  A: 

There is the GWT-MATH library at http://code.google.com/p/gwt-math/.

However, I warn you, it's a GWT jsni overlay of a java->javascript automated conversion of java.BigDecimal (actually the old com.ibm.math.BigDecimal).

It works, but speedy it is not. (Nor lean. It will pad on a good 70k into your project).

At my workplace, we are working on a fixed point simple decimal, but nothing worth releasing yet. :(

Adam Malter
looks promising! thanks!(I don't think the performance will be an issue, is just for validation, the real math is done on the server using R ;) )
Opsi