0

I need to convert a Fortran code into a C++ one and I have the following problem:

I'm trying to compute a double value named R12 with both Fortran 77 and C++ languages but I obtained two values very close each others but with two different precision . here is my Fortran code :

R12 = sqrt(95699.68D0+1123.6D0*flx) - 408.99D0

with double flx = 192.9D0. The value I obtain is R12 = 149.97522253177789

I do the same with C++:

R12 = sqrt(95699.68+1123.6*flx) - 408.99 ; 

The value I obtain here is R12 = 149.97522253177794.

Can you guys please help me to find where this difference is from, cause it's very important for my work to have the exact precision

5
  • 3
    Define "exact precision"? Using floating point values you will never have "exact precision", and for irrational numbers you would need infinite precision anyway. double has 15-17 numbers of precision and that is what you have here. May 26, 2015 at 13:51
  • You can also read more about this here: en.wikipedia.org/wiki/Machine_epsilon
    – CppChris
    May 26, 2015 at 13:53
  • Have you checked that (1) the result of the multiplication is identical in both languages? (2) the result of the addition is identical in both languages? (3) the result of sqrt is identical in both languages? You need to drill down to the essence of the problem before asking on SO. May 26, 2015 at 13:54
  • I've checked it before, the sqrt function give the same result for both languages, the problem comes from the soustraction between sqrt and 408.99 May 26, 2015 at 13:57
  • 2
    Check, if you haven't already, that you have told your compiler to use IEEE arithmetic in both the Fortran and C++ code. By default most Fortran compilers, in compliance with that language's long-standing standard(s), do not require adherence to IEEE's standard for floating-point arithmetic, which was defined long after Fortran started crunching numbers. I'm not sure what C++ compilers are required to do but I suspect that they too can play fast and loose with f-p arithmetic unless told explicitly to behave themselves. May 26, 2015 at 15:45

1 Answer 1

5

With double precision, you have 52 bits (roughly 15 decimals) of significand precision. In your case this is:

Fortran:

0.14997522253177789e3
                ^

C++

0.14997522253177794e3
                ^

You can see that both results are identical up to the 15th digit. My guess is that C++ and Fortran apply a different conversion (rounding) to the decimal system.

In Fortran 2003+ you can give a ROUND specifier to write:

program test

  write(*,*)                     'Default    ', sqrt(95699.68D0+1123.6D0*192.9D0) - 408.99D0
  write(*,*,round='UP')          'UP         ', sqrt(95699.68D0+1123.6D0*192.9D0) - 408.99D0
  write(*,*,round='DOWN')        'DOWN       ', sqrt(95699.68D0+1123.6D0*192.9D0) - 408.99D0
  write(*,*,round='ZERO')        'ZERO       ', sqrt(95699.68D0+1123.6D0*192.9D0) - 408.99D0
  write(*,*,round='NEAREST')     'NEAREST    ', sqrt(95699.68D0+1123.6D0*192.9D0) - 408.99D0

end program

results in

 Default   149.97522253177794     
 UP        149.97522253177795     
 DOWN      149.97522253177794     
 ZERO      149.97522253177794     
 NEAREST   149.97522253177794  
2
  • yean i know but the thing is that there is a lot of arithmetic instructions after that one, so the little difference is getting bigger at the end of my program May 26, 2015 at 13:59
  • 1
    My guess is that the numbers are identical in binary representation, but you should check that! BTW: I get exactly the same result in Fortran as you get in C++... May 26, 2015 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.