From: Douglas Seifert Date: 2009-11-22T23:42:34+09:00 Subject: Re: really crazy about this (do/while) --000e0cd1a9d8721a070478f6b72f Content-Type: text/plain; charset=ISO-8859-1 > > So my compromise is, when I want to work with some fraction of a number, > and I > want it to be as precise as possible (with no regard for performance), I > make > it a Rational as long as I can get away with. I can always call to_f on the > end result. > > I might be Doing It Wrong, though. Maybe BigDecimal is the way to go? > > BigDecimal is probably easier to code with, but can kill performance as has been mentioned previously. Recalling the old Fortran days, we always had a constant called EPS we used in these kind of situations. EPS (short for epsilon) was set to a small number, usually 1e-7. Anytime we needed to compare two floating point values, we would not compare them directly, but rather compare the absolute value of their difference to EPS. In the original poster's code, here is how we would have dealt with the issue: eps = 1e-7 temp = 98.3 i = 0 begin i += 1 puts "step" + i.to_s + " befre adding is " + temp.to_s temp += 0.1 puts "step" + i.to_s + " after adding is " + temp.to_s puts "temp - 98.6: #{temp-98.6}" puts #end while temp < 98.6 end while (temp - 98.6) < -eps Having to remember to do this all the time is difficult and annoying. -Doug Seifert --000e0cd1a9d8721a070478f6b72f--