floating point precision

Hi everybody,

I run a program for scientific calculation on different platforms (windows + gcc, windows + visual studio, a linux cluster with, I think, an intel compiler). Unfortunately, although the program is deterministic, I get different results on the cluster compared to the other two platforms. My question is: is there a c++ instruction, valid of all platforms, that be be used to make sure that calculations are performed with the same precision?

I achieved this objective with the two windows platforms using the instruction

asm ("fldcw %0" : : "m" (*&0x27F));

for the gcc one and compiling the visual studio one with the 'precise' option, but this doesn't seem to work for the cluster.

Thanks,

Alex
> is there a c++ instruction, valid of all platforms, that be be used
> to make sure that calculations are performed with the same precision?

Use a library for floating-point computations with accurate rounding . (There would be a small performance hit).
For example MPFR http://www.mpfr.org/
With a C++ wrapper http://www.holoborodko.com/pavel/mpfr/

Even simple floats are pretty precise for scientific computation. If your calculations are producing significantly different results because of some really small precision issue, I would reconsider your equations before assuming that the FPU is screwing up.

Accurate rounding is useful for only two domains: banking (and related bookkeeping tasks) and printing human readable text.
Well, reconsidering the equations is certainly a good idea, I wouldn't say it's simple though ...
> I run a program for scientific calculation

If your scientific calculations use any kind of computational algebra or numerical analysis, you probably need a library for floating-point computations with correct rounding.

AFAIK, scientific and engineering computations are the primary domains where such libraries are used. The 'Software Using MPFR' section on the MPFR page should give you an idea of the kind of programs where the library is used.
Numeric computations on finite-precision computers are a difficult, but really well explored field, so examining the equations for numeric stability is probably worthwhile.
But if resetting the FPU control word worked for you on Windows, you could just try setting your rounding mode directly using C++11/C99 function fesetround(): http://en.cppreference.com/w/cpp/numeric/fenv/feround (I think your case is fesetround(FE_TOWARDZERO))
Last edited on
Topic archived. No new replies allowed.