[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Float numbers equality.
- From: Coda Highland <chighland@...>
- Date: Fri, 9 Aug 2013 15:50:28 -0700
On Fri, Aug 9, 2013 at 3:47 PM, Coda Highland <firstname.lastname@example.org> wrote:
> On Fri, Aug 9, 2013 at 3:18 PM, Luiz Henrique de Figueiredo
> <email@example.com> wrote:
>>> function float_equal(lhs, rhs, epsilon)
>>> return math.abs(lhs - rhs) < epsilon
>> As mentioned before, if you have to do this, you need to use *relative* error,
>> not absolute error:
>> return math.abs(lhs - rhs) < epsilon*rhs
>> Floating-poin numbers are not distributed uniformily in their range;
>> there's lot of clustering and empty intervals, due to scaling.
> Ah, whoops, yeah, you're right, I got sloppy there. Really that ought
> to be epsilon*math.max(lhs,rhs) even.
> /s/ Adam
Er. Actually, I just need to get some sleep. It doesn't matter which
side you multiply by there because if the two terms are close enough
that an epsilon comparison is necessary in the first place then you're
going to get basically the same result.
I don't usually need to do that kind of order-of-magnitude scaling
because my epsilon comparisons are with code where I already know the
range of the numbers involved.