Alessio said:
Hi,
There's a way ( or some advice ) to get same results with different
compilers ?
Let me explain better.
I've developped an application ( from a pv-wave source ) that use some
math function (fabs, sqrt,....), I noticed that using different
compiler, results are different.
I think that trouble is the way compilers represent floats in memory.
How can I manage this ?
The only way to absolutely guarantee getting the same results is to
emulate floating point behavior within your own code: create a struct
whose members are integers (and which would presumably be bit-fields)
representing the sign, signifcand, and exponent, and write your own
routines for addition, subtraction, multiplication, etc. Then build
your own variant of the standard library's math functions that work with
this emulated floating point type. This would be a lot easier to do in a
language with operator overloads than it would be in C, but it's still a
lot of hard work. Because all of the actual operations involve integers,
you can ensure that exactly identical results are produced (if you're
careful).
If that seems like far too much trouble, you're right - it is. What you
should be doing is writing your code so that it does not require exactly
identical results. However, while you can't, at any reasonable cost,
eliminate such variations, you can minimize them by using double or even
long double to minimize round-off errors.
If you can afford to restrict the portability of your code, C99 added a
number of new features that allow you to determine things about your
floating point environment that would affect the results:
* __STDC_IEC_559__ should be pre-#defined by the implementation only if
it conforms to IEC/IEEE 60559, a standard that imposes much stricter
requirements on floating point operations than the C standard itself does.
* If the value of FLT_ROUNDS is either 0, 1, 2, or 3, then it tells you
useful information on how rounding is performed. If you don't like the
value of FLT_ROUNDS, you can attempt to change the rounding by calling
fsetround(), but pay attention to the return value: if it's non-zero,
your attempt failed.
* If the value of FLT_EVAL_METHOD is not -1, it tells you something
useful about how intermediate results are handled.
Even implementations that pre#define __STDC_IEC_559__, can be put in the
same rounding mode, and have the same value for FLT_EVAL_METHOD can
still produce different results from the same code, but the variations
should be much smaller than if there are differences in those regards.