Bug 53470 - sqrt() gives bad answers
sqrt() gives bad answers
Product: Red Hat Linux
Classification: Retired
Component: glibc (Show other bugs)
alpha Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
Aaron Brown
Depends On:
  Show dependency treegraph
Reported: 2001-09-09 22:36 EDT by Ed Richley
Modified: 2016-11-24 10:23 EST (History)
1 user (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2002-12-15 12:43:18 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Ed Richley 2001-09-09 22:36:41 EDT
From Bugzilla Helper:
User-Agent: Mozilla/4.51 [en] (X11; I; Linux 2.2.14-5.0 sparc)

Description of problem:
Take x = (double)0.92; y = (double)1.6e-9;
z = sqrt(x*x+y*y) gives a result LESS THAN x!!!!

This DOES NOT occur on older versions and on other platforms. 

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
1. double x,y,z, xs;
2. x = 0.92;
3. y = 1.6e-9;
4. xs = x*x+y*y;
5. z = sqrt(xs);
6. fprintf("z = %lf\n",z)

Actual Results:  z = 0.919999847412120819

Expected Results:  z =  0.920000000000000040

Additional info:

With the COMPAQ c-compiler (ccc), it works fine. Only gcc screws up with 
the impossible answer that z < x.
Comment 1 Jakub Jelinek 2001-09-10 04:19:12 EDT
I cannot reproduce it neither at -O0 nor at -O2 on both 7.0, 7.1 and rawhide.
Anyway, unless you use -mieee flag to gcc, the result is definitely not
surprising, Alpha default mode is flush-to-zero arithmetics which is given
alpha hardware much faster but of course has the drawback of giving less
precise results.
Do you have actual testcase which shows the result together with gcc and
glibc version numbers? Putting the above statements into main() together
with #include <stdio.h> and #include <math.h> and fixes for obvious errors
really does not show anything...
Comment 2 Ed Richley 2001-09-10 07:58:25 EDT
Here's the program:

bose$ more bc_test.c
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#define N 10
  double Xr, Xz, Xs, X;
  int i, j;

  Xr = .92000000000;
  Xz = 1.6513e-9;

  Xs = Xr*Xr+Xz*Xz;
  X = sqrt(Xs);

  fprintf(stdout,"%20.18lf  %20.18lf\n",Xs,X);


Compiler output:
bose$ gcc bc_test.c -o bc_test -lm -v
Reading specs from /usr/lib/gcc-lib/alpha-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.0)
 /usr/lib/gcc-lib/alpha-redhat-linux/2.96/cpp0 -lang-c -v -D__GNUC__=2 -D__GNUC_
MINOR__=96 -D__GNUC_PATCHLEVEL__=0 -Dlinux -Dunix -D_LONGLONG -D__alpha__ -D__EL
F__ -D__linux__ -D__unix__ -D_LONGLONG -D__alpha__ -D__ELF__ -D__linux -D__unix 
-Asystem(linux) -D__LANGUAGE_C__ -D__LANGUAGE_C -DLANGUAGE_C -Acpu(alpha) -Amach
ine(alpha) -D__alpha -D__alpha__ -D__alpha_ev4__ -Acpu(ev4) bc_test.c /tmp/ccNNm
GNU CPP version 2.96 20000731 (Red Hat Linux 7.0) (cpplib)
 (Alpha GNU/Linux for ELF)
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/usr/alpha-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
End of search list.
 /usr/lib/gcc-lib/alpha-redhat-linux/2.96/cc1 /tmp/ccNNmKbK.i -quiet -dumpbase b
c_test.c -version -o /tmp/ccSberIN.s
GNU C version 2.96 20000731 (Red Hat Linux 7.0) (alpha-redhat-linux) compiled by
 GNU C version 2.96 20000731 (Red Hat Linux 7.0).
 as -o /tmp/cc60CpWf.o /tmp/ccSberIN.s
 /usr/lib/gcc-lib/alpha-redhat-linux/2.96/collect2 -m elf64alpha -O1 -dynamic-li
nker /lib/ld-linux.so.2 -o bc_test /usr/lib/gcc-lib/alpha-redhat-linux/2.96/../.
./../crt1.o /usr/lib/gcc-lib/alpha-redhat-linux/2.96/../../../crti.o /usr/lib/gc
c-lib/alpha-redhat-linux/2.96/crtbegin.o -L/usr/lib/gcc-lib/alpha-redhat-linux/2
.96 -L/usr/lib/gcc-lib/alpha-redhat-linux/2.96/../../.. /tmp/cc60CpWf.o -lm -lgc
c -lc -lgcc /usr/lib/gcc-lib/alpha-redhat-linux/2.96/crtend.o /usr/lib/gcc-lib/a

And the output:

bose$ bc_test
0.846400000000000041  0.919999847412120819

Also, this may be important:
bose$ rpm -qf /usr/lib/libm.so

Finally, this is on an older Alpha; a 21064. 


Comment 3 Jakub Jelinek 2001-09-10 11:40:39 EDT
Still cannot reproduce it.
Can you please upgrade your glibc at least to the errata release?
Comment 4 Ed Richley 2001-09-18 20:41:55 EDT
OK, I upgraded to RH7.1 which has a newer version of glibc, and the
problem seems to have gone away. 

Strange, how loarge the executable is...

bose$ !ls
ls -las bc_test*
 196 -rwxrwxr-x    1 richley  richley    193725 Sep 18 20:48 bc_test
   4 -rw-rw-r--    1 richley  richley       243 Sep 11 08:26 bc_test.c
   4 -rw-rw-r--    1 richley  richley       250 Sep  9  2001 bc_test.c~

Note You need to log in before you can comment on or make changes to this bug.