Re: 0.06 == 0.06 returns false in Ruby?
- From: "doug meyer" <doug.meyer@xxxxxxxxxx>
- Date: Fri, 31 Aug 2007 11:50:49 +0900
There is also a class called BigDecimal (or something like that) if
you want to have really accurate numbers and no floating-point errors.
On 8/30/07, Dan Zwell <dzwell@xxxxxxxxx> wrote:
Dan Zwell wrote:
To compare floats, you must ask whether they are within a certain
threshold of each other.
Epsilon = 0.00000000001
return (num1-num2).abs < Epsilon # num1 == num2
Dan
Oops, I didn't realize that Ruby comes with an Epsilon. This should be a
fine test for equality:
return (num1-num2).abs < Float::EPSILON
Now, whether Float#==(other) should make this check might be worth
thinking about, but I really have no opinion on the matter--I'm used to
not comparing floats like this.
Dan
.
- Follow-Ups:
- Re: 0.06 == 0.06 returns false in Ruby?
- From: Ravil Bayramgalin
- Re: 0.06 == 0.06 returns false in Ruby?
- References:
- 0.06 == 0.06 returns false in Ruby?
- From: Jason G.
- Re: 0.06 == 0.06 returns false in Ruby?
- From: Dan Zwell
- Re: 0.06 == 0.06 returns false in Ruby?
- From: Dan Zwell
- 0.06 == 0.06 returns false in Ruby?
- Prev by Date: Re: 0.06 == 0.06 returns false in Ruby?
- Next by Date: Re: Difference between Distributed ruby and Rinda
- Previous by thread: Re: 0.06 == 0.06 returns false in Ruby?
- Next by thread: Re: 0.06 == 0.06 returns false in Ruby?
- Index(es):
Relevant Pages
|