From: Paul Lutus Date: 2006-11-13T15:05:05+09:00 Subject: Re: Ruby floating point behavior typical of other langs?? bradjpeek wrote: / ... > I'm fairly new to Ruby and don't do much programming, but when I saw > this example I was surprised that the default behavior is that 3.2 - > 2.0 != 1.2 > > To me, this violates the "Principal of least surprise", Do you mean the "principle of least suprise"? One word means primary, the other means standard or canon. They sound the same, but that's all they have in common. > but I guess it > isn't a big deal because I don't remember it being discussed in > Programming Ruby book (but it certainly may have been). > > Do other languages work this way? All of them that use binary internal storage, yes. A decimal value like 1.2 is a continuing fraction in binary and cannot be precisely represented in binary in a finite number of places. Sort of like 1/3 in either binary or decimal. http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems -- Paul Lutus http://www.arachnoid.com