Subj : Re: Weird Math To : Andy Madigan From : Igor Bukanov Date : Thu Sep 16 2004 01:24 am Andy Madigan wrote: > I've been running some scripts through rhino, and one script kept on > getting an equation wrong, I finally found the problem, I ran a simple > test through the Rhino Shell, here's the output: > > js> 100 - 69.1 > 30.900000000000006 > > Am I going nuts? Is this something about how javascript does math that > I don't know? (Note: 100 - 59.1 is correct) 69.1 can not be represented as exact Java double (binary representation of 0.1 is infinite fraction) so it is rounded to the nearest representable number. When the rounded number is subtracted from 100, its closes decimal that when read back preserve the binary presentation is 30.900000000000006. You do not see that with 100 - 59.1 since 69.1. and 59.1 have rather different binary presentations (starting from the fact that 69 > 64 and 59 < 64) so in this case the decimal 40.9 is the best fit. Note that SpiderMonkey has exactly the same behavior which is not surprising since Rhino code is just a translation of SM code into Java. Regards, Igor .