From: Yohanes Santoso Date: 2002-12-28T18:08:55+09:00 Subject: Re: 1210 / 100 = 12? what? Tom Sawyer writes: > mathmatical computing that are 100% accurate. granted they are > comupationally intense and must indeed keep 2/3 as 2/3 and never > approximate to .6666. and at some points require a set degree of > accuracy to return, least the As an aside note, the result of a floating point operation must be treated carefully, since the underlying representation of the real number cannot fully represent certain numbers. For example: > 2.4/0.2 == 12 false How come? Because 2.4 and 0.2 are some of the numbers that cannot be represented fully in IEEE floating point format: > "%.20f" % 2.4 "2.39999999999999991118" > "%.20f" % 0.2 "0.20000000000000001110" Thus, in performing a floating point operation, the only guarantee that you will have is that the result will be within a certain range (tolerance) from the actual, correct result. If you do any meaningful floating point operation, it will be useful if you have something like this: class Float @@tolerance = 1E-6 #adjust this as needed def around?(other) other = other.to_f unless other.is_a? Float (other-self).abs < @@tolerance end end > (2.4/0.2).around? 12 true Which is as we expected. YS.