For Programmers: Free Programming Magazines  


Home > Archive > Fortran > December 2004 > Re: small numerical differences in floating point result between









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: small numerical differences in floating point result between
Rich Townsend

2004-12-14, 3:57 am

JS wrote:
> We have the same floating point intensive C++ program that runs on
> Windows on Intel chip and on Sun Solaris on SPARC chips. The program
> reads the exactly the same input files on the two platforms. However,
> they generate slightly different results for floating point numbers.
>
> Are they really supposed to generate exactly the same results? I
> guess so because both platforms are supposed to be IEEE floating point
> standard (754?) compliant. I have turned on the Visual C++ compile
> flags which will make sure the Windows produce standard compliant code
> (the /Op flags). However, they still produce different results. I
> suspect that this may be due to a commerical mathematical library that
> we use which can't be compiled using /Op option. If I had recompiled
> everything using /Op option, the two should have produced the same
> results.
>
> Am I right?


Yes and no. As I understand it, the IEEE floating point standard places
reasonably-tight constraints on how *atomic* floating-point operations
are undertaken. For instance, given the bit patterns making up two
floating point numbers a and b, the standard says how to find the bit
pattern making up their product a*b.

However, a typical computer program is not a single atomic operation; it
is a whole sequence of them. Take for example this assignment:

d = a + b + c

What order should the sum be undertaken in? Should it be

d = (a + b) + c,

or

d = a + (b + c)?

Mathematically, these are identical. But in a computer program the "+"
operation does not represent true mathematical addition, but rather a
floating-point approximation of it. Even if this approximation conforms
to the IEEE standard, the results of the two assignments above will
differ in many situations. Consider, for instance, when:

a = 1.e10
b = -1.e10
c = 1.

Assuming floating-point math with a precision less than ten decimal
significant digits, the first expression above above will give d = 1,
but the second expression will give d = 0.

Therefore, the result of the *original* assignment above (the one
without the parentheses) depends on how the compiler decides to join the
two atomic addition operations. Even though these operations might
individually conform to the IEEE standard, their result will vary
depending on the order in which the compiler decides to perform them.
This is nothing to do with the IEEE standard per se, but a fundamental
limitation of finite-precision floating-point math.

Hopefully, this should underline the idiocy in rubbishing one compiler
because it produces slightly different results to another.

cheers,

Rich

--
Dr Richard H D Townsend
Bartol Research Institute
University of Delaware

[ Delete VOID for valid email address ]
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com