0

I have a cpp code that is interfaced to a fortran code. The fortran code needs input arguments given by the cpp one. During the transfer there is a problem with the rounding, giving a little error than then cause major changes. For example -5.35300000 in cpp become -5.35299998 in fortran. In cpp as well as in fortran all variables are defined as double type variable. I compile gcc-9.3 version.

If instead of compiling with gcc on linux, I compile with intell compiler on windows I don't have this rounding problem.

How can I overcome this rounfing issue with gcc?

Thanks

Laetis
  • 1,337
  • 3
  • 16
  • 28
  • 1
    Welcome to floating point math. Expecting *identical* results is just not going to happen. Those numbers are, for all intents and purposes, the same as far as IEEE floating point math goes. Always be aware of [machine epsilon](https://en.wikipedia.org/wiki/Machine_epsilon) and account for it in your code. – tadman Jan 14 '21 at 14:43
  • `-5.35300000 in cpp` -- There is no exact binary representation for this value. Where are you seeing in c++ that this is the actual value? Even in source code, `double x = -5.353;` is not exactly `-5.353`. – PaulMcKenzie Jan 14 '21 at 14:44
  • I'm tempted to posit https://stackoverflow.com/questions/588004/is-floating-point-math-broken as a duplicate but there's not enough detail to be sure – Ian Bush Jan 14 '21 at 15:14
  • 2
    Can you show us your code? Better even a [MWE](https://stackoverflow.com/help/minimal-reproducible-example). Try to make it as short as possible and include the compile commands. – jack Jan 14 '21 at 15:33
  • thanks for your answers. The code is really huge I don't think I can extract a simple example, especially cause I don't master cpp even a bit. When I print values in cpp it give -5.353 without more info, I know it's declared as double but not more. I am just puzzled by the difference of behavior between gcc and intell. – Laetis Jan 15 '21 at 08:00
  • How do you print the value in cpp? – aka.nice Jan 15 '21 at 11:19

0 Answers0