[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Error Gawk Manual FP Precision
From: |
arnold |
Subject: |
Re: [bug-gawk] Error Gawk Manual FP Precision |
Date: |
Mon, 20 Aug 2018 01:28:11 -0600 |
User-agent: |
Heirloom mailx 12.4 7/29/08 |
Hi.
> > Dear bug-gawk:
> >
> > The section on comparing two floating point values correctly recommends
> > making avoiding simple equality comparisons in favor of ensuring the
> > values are close enough.
> >
> > The code given in 15.4.1.2 "Be Careful Comparing Values" is clearly
> > wrong, though:
> >
> > The general wisdom when comparing floating-point values is to see
> > if they are within some small range of each other (called a delta,
> > or tolerance). You have to decide how small a delta is important
> > to you. Code to do this looks something like the following:
> >
> > delta = 0.00001 # for example
> > difference = abs(a) - abs(b) # subtract the two values
> > if (difference < delta)
> > # all ok
> > else
> > # not ok
> >
> > If the desired value, a is -1000, and b is +1000, the answer is off by
> > 2000. But difference will be zero.
>
How does this look to you? I'm not sure it's right.
Thanks,
Arnold
------------------------------------------------------------
function abs(f)
{
return f < 0 ? -f : f
}
function within_delta(a, b, delta)
{
if ((a > 0 && b > 0) || (a < 0 && b < 0))
return abs(a - b) <= abs(delta)
else
return (a - b) <= abs(delta)
}
function test_delta(a, b, delta, result)
{
result = within_delta(a, b, delta)
printf("%.12g, %.12g within delta %.12g -> %s\n", a, b, delta,
result ? "TRUE" : "FALSE")
}
BEGIN {
delta = 0.001
n = split("100.0001 100.0002 100 -100 100.0001 -100.0001", pairs)
for (i = 1; i <= n; i++)
pairs[i] += 0 # force numeric
for (i = 1; i <= n; i += 2)
test_delta(pairs[i], pairs[i+1], delta)
}