This is the mail archive of the libc-alpha@cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Floating point problem on glibc-2.1.1


> Date: Mon, 3 May 1999 21:27:12 +0800
> From: Tung-Han Hsieh <thhsieh@linux.org.tw>
> 
> Hello,
> 
> We discover a floating point problem on glibc-2.1.1:
> 
> #include <math.h>
> #include <stdio.h>
> 
> main()
> {
>     printf("%d\n", (int)(log(8.0)/log(2.0)));
> }
> 
> 
> When compile this simple program with command:
> 
> 	cc file.c -lm
> 
> and run it, the output is "2", but not "3"; while in other plateforms,
> say, Solaries-2.5.1, the output is "3".
> 
> This problem also exists in glibc-2.0.7.

Can you say what hardware you are using?  Floating-point is different
between sparc and intel, for instance.

When I run it on powerpc, I get:

[geoffk@geoffk /tmp]$ gcc -O -Wall -W z.c -o z -lm
z.c:5: warning: return-type defaults to `int'
z.c: In function `main':
z.c:7: warning: control reaches end of non-void function
[geoffk@geoffk /tmp]$ ./z
3

so it works fine here.

However, note that (int)x rounds down, so even if log(8.0)/log(2.0)
came out a tiny bit below 3.0, you would get '2' as the final result.
Usually, code like this indicates that you haven't thought about
errors, or (much more rarely) you have carefully computed the
error bounds and know it will always work.

-- 
Geoffrey Keating <geoffk@ozemail.com.au>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]