This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Floating point computation "non-bug"
- From: Tim Prince <timothyprince at sbcglobal dot net>
- To: Dave Elcock <dave at sias dot com>, gcc-bugs at gcc dot gnu dot org
- Date: Fri, 11 Apr 2003 06:44:35 -0700
- Subject: Re: Floating point computation "non-bug"
- References: <3E96BA92.60003@sias.com>
- Reply-to: tprince at computer dot org
On Friday 11 April 2003 05:52, Dave Elcock wrote:
>a
> ridiculously simple example of what's going wrong for me:
>
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main (int argc,char **argv)
> {
> float f1, f2;
> f1 = atof(argv[1]);
> f2 = f1 * 10.0;
> printf("f1 %f\n"
> "f2 %f\n"
> "(int)f2 %d\n"
> "(int)(f1*10.0) %d\n",
> f1, f2, (int)f2, (int)(f1*10.0));
> }
>
>
> Simply compiling with 'gcc test.c' and then running './a.out 2.3' gives
> the following output:
>
> f1 2.300000
> f2 23.000000
> (int)f2 23
> (int)(f1*10.0) 22
>
>
> I can't see how such a simple example should be affected by floating
> point precision. At the very least, the last two answers should be
> consistent. I would suggest that this IS a bug, despite the text of the
> explanation on the GCC Bugs web page, and the linked paper.
But, in effect, you're demanding that long double and double should be
identical, but expecting the compiler to read your mind. There are several
ways to make that happen, but you chose not to.
Or, if you mean to use lrint() instead of (int), write it that way. Yes,
some compilers have command line options to make that conversion
automatically. That violates all the C standards.
>
> Compiled with another compiler (e.g. Portland Group's pgcc), I get the
> correct answer for the last line, so it's not about the machine's
> precision.
How not? You're probably choosing inconsistent options between the two
compilers. You chose not to reveal the information, so I'll assume that. We
don't even know whether your two compilers are using the same or different
library implementations of atof(), or whether you made any effort to make
them the same.
>
> If I had the expertise necessary to help with this, you could be sure
> I'd get stuck right in and help out.
If you have the expertise to write this little sample, you have the expertise
to fix it.
--
Tim Prince