Bug 53470 - sqrt() gives bad answers
Summary: sqrt() gives bad answers
Keywords:
Status: CLOSED CURRENTRELEASE
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: glibc
Version: 7.0
Hardware: alpha
OS: Linux
medium
medium
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: Aaron Brown
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2001-09-10 02:36 UTC by Ed Richley
Modified: 2016-11-24 15:23 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2002-12-15 17:43:18 UTC
Embargoed:


Attachments (Terms of Use)

Description Ed Richley 2001-09-10 02:36:41 UTC
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:
Always

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 08:19:12 UTC
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 11:58:25 UTC
Here's the program:

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

#define N 10
main(){
  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);

}
bose$ 

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
KbK.i
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:
 /usr/lib/gcc-lib/alpha-redhat-linux/2.96/include
 /usr/include
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
lpha-redhat-linux/2.96/../../../crtn.o
bose$

And the output:

bose$ bc_test
0.846400000000000041  0.919999847412120819
bose$ 

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

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


Thanks.




Comment 3 Jakub Jelinek 2001-09-10 15:40:39 UTC
Still cannot reproduce it.
Can you please upgrade your glibc at least to the errata release?

Comment 4 Ed Richley 2001-09-19 00:41:55 UTC
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~
bose$ 




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