From: Markus Date: 2004-10-15T23:57:34+09:00 Subject: Re: Rounding to X digits Interesting. I hope to heck you used a script to find that edge case. What made you suspect there would be a difference, given that they are mathematically equivalent? I'll definitely come back to this after the coffee hits. -- Markus On Fri, 2004-10-15 at 07:28, trans. (T. Onoma) wrote: > On Thursday 14 October 2004 01:55 pm, Markus wrote: > | Here's mine (with a few extras): > | > | class Numeric > | def iff(cond) > | cond ? self : self-self > | end > | def to_the_nearest(n) > | n*(self/n).round > | end > | def aprox(x,n=0.001) > | (to_the_nearest n) == (x.to_the_nearest n) > | end > | def at_least(x); (self >= x) ? self : x; end > | def at_most(x); (self <= x) ? self : x; end > | end > | > > | def to_the_nearest(n) > | n*(self/n).round > | end > def round_to_nearest( n=0.01 ) > (self * (1/n)).round.to_f / (1/n) > end > > For some reason they don't give the exact same answer. Try an edge case. > > irb(main):066:0> 0.1 * (134.45/0.1).round > => 134.4 > irb(main):067:0> (134.45 * (1/0.1)).round.to_f / (1/0.1) > => 134.5 > > T.