Perhaps the constants get folded away, but the variables don't.
If the compiler uses a different floating-point rounding mode than the runtime,
that would explain the discrepancy. (A good optimizer should be able to
constant-fold the expression involving variables, too.)
I'm not sure if the C standard mandates a compiler to use the same floating
point rounding mode as its runtime environment.
Bye,
Jens.
--
mailto:j...@acm.org phone:+49-7031-464-7698 (TELNET 778-7698)
http://www.bawue.de/~jjk/ fax:+49-7031-464-7351
PGP: 06 04 1C 35 7B DC 1F 26 As the air to a bird, or the sea to a fish,
0x555DA8B5 BB A2 F0 66 77 75 E1 08 so is contempt to the contemptible. [Blake]
On Thu, 01 Mar 2001 10:54:58 GMT, Tony Porczyk wrote:
> Chronos Tachyon <chronos...@please.no.spam.in.my.mail.com> writes:
>
> >I tried running your code, but I didn't get the problem you described.
> >The only change I made was to "#include <stdio.h>" to quiet the warning
> >about printf being undeclared.
> >user$ gcc -O2 -ansi -Wall -o temp temp.c
> >[...]
>
> Original poster said, "without optimizations".
>
> t.
Duh. You're right, of course. I tried again without -O2 (habits are hard
to break), and got "10 = 9", the opposite of what the guy was complaining
about...
--
Chronos Tachyon
Guardian of Eristic Paraphernalia
Gatekeeper of the Region of Thud
[Reply instructions: My real domain is "echo <address> | cut -d. -f6,7"]
Chas2K.01
--
----- * --- http://www.unixstar.com -----------
- Tomorrow's leaders are in Scouting today -
- Support a free and democratic Taiwan -
-----------------------------------------------
> > You are so right. What if I tell you that I have two machines with
> > identical processors (Intel PIII). One running Linux and one running
> > FreeBSD. Both us gcc-2.95.2. Linux uses libc.so.6 and FreeBSD uses
> > libc.so.4. They produce different results. The code shouldn't have to
> > rely on libc. The 10 = 9 happens on all linux machines I have testet
> > which includes 4 year old installs and new ones. I have not seen it
> > happen on any other Intel based machine.
> >
> > I know that there is a problem when type casting back and forth from
> > int to float. But... with the same compiler and on the same
> > architecture I would expect to see the same error.
>
> Instead of complaining, you should be glad to have learned that same
> compiler + same architecture doesn't guarantee same results. Look at it
> like this:
>
> 1. The C Standard doesn't guarantee results for floating point
> operations that are the same everywhere.
>
> 2. A well-written program will produce approximately the same results
> everywhere.
>
> 3. If your program gives different results like in your case where the
> only difference is the operating system, then you _know_ that your program
> is not well-written. A very valuable information. If one computer says
> nine, and the other says ten, at least you know that both results cannot
> be trusted.
Yes, one of the purposes for changing precision mode would be to check for
unexpected numerical flakiness in your program.
And, why is this message so heavily cross-posted that it can't be replied
to? What does this have to do with a mac, for example?