From: Florian Frank Date: 2009-11-22T23:49:46+09:00 Subject: Re: really crazy about this (do/while) David Masover wrote: > 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? > > > Another possibility is using a small, but >0, epsilon and check if the distance between the number and the limit is less than its value. Ruby does this automatically for you, if you use its #step functions: >> 98.3.step(98.6, 0.1).to_a # => [98.3, 98.4, 98.5, 98.6] >> 98.4.step(98.6, 0.1).to_a # => [98.4, 98.5, 98.6] >> (98.3..98.6).step(0.1).to_a # => [98.3, 98.4, 98.5, 98.6] >> (98.4..98.6).step(0.1).to_a # => [98.4, 98.5, 98.6] Maybe the general advice here is, don't do it yourself, if Ruby offers a better way itself. -- Florian Frank