5

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 ++

opsidao
  • 1,441
  • 16
  • 23
  • This problem may appear on ANY arbitrary precision floating point, otherwise you set the precision for equality. This is the intrinsic nature of floating point numbers. Use fixed-point math with arbitrary length. – kirilloid Jan 21 '10 at 20:05
  • @kirilloid of course it may appear, just thought it was worth elaborating the question (usually leads to more elaborated answers). Thanks anyway! – opsidao Jan 21 '10 at 20:25

2 Answers2

1

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
  • 106
  • 3
  • 1
    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 ;) ) – opsidao Jan 21 '10 at 20:27
1

Use an arbitrary precision integer library such as silentmatt’s javascript-biginteger, which can store and calculate with integers of any arbitrary size.

Since you want ten decimal places, you’ll need to store the value n as n×10^10. For example, store 1 as 10000000000 (ten zeroes), 1.5 as 15000000000 (nine zeroes), etc. To display the value to the user, simply place a decimal point in front of the tenth-last character (and then cut off any trailing zeroes if you want).

Alternatively you could store a numerator and a denominator as bigintegers, which would then allow you arbitrarily precise fractional values (but beware – fractional values tend to get very big very quickly).

Daniel Cassidy
  • 24,676
  • 5
  • 41
  • 54