[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[bug-gawk] GAWK 4.1 SQRT() bug
From: |
Katherine Wasserman |
Subject: |
[bug-gawk] GAWK 4.1 SQRT() bug |
Date: |
Sat, 02 Aug 2014 12:27:00 -0400 |
In version 4.1.60 of GAWK the sqrt() function does not work correctly on
bignums.
Here's a demo of the problem along with, a function that does work correctly.
Running this program (sqrt-bug.awk):
--------------------------------------------------------------------
BEGIN {
a=11111111111111111111111111111111111111111111111111111111111
print sqrt(a^2)
print sq_root(a^2)
}
function sq_root(x, temp,r,z)
{ temp=substr(x,1,length(x)/2) + 0 # a good first guess
z=0
while (abs(z-temp)>1)
{ z=temp
div(x,temp,r)
temp=r["quotient"] + temp
div(temp,2,r)
temp=r["quotient"]
}
return temp
}
function abs(x)
{ return (x<0 ? -x : x)
}
--------------------------------------------------------------------
gawk -M -f sqrt-bug.awk
results in:
11111111111111111261130863809439559987542611609749437808640
11111111111111111111111111111111111111111111111111111111111
Thanks,
Katie
- [bug-gawk] GAWK 4.1 SQRT() bug,
Katherine Wasserman <=