Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29238 Discussions

Loosing precision when passing Float from Fortran to C function

aliwin
Beginner
550 Views
Hi there,

I have a program that has both Fortran and C functions. When passing Real*4 parameters from Fortran to a C function(as a float), I noticed that the decimal part changed.

Values of the REAL*4 parameters in the calling Fortran function:
P1 = 99999.90 P2 = 999.9000 P3 = 99.99000

Values of the corresponding float parameters in the calledC function:
P1 = 99999.898 P2 = 999.90002 P3 = 99.989998

How can Ipass float numbers from Fortran to C without loosing precision ?
Or is there any specific setting that I need to dounder the properties/Fortran/FloatingPoint menu ?

Thanks,
Ali
0 Kudos
2 Replies
Steven_L_Intel1
Employee
550 Views
Those values are not exactly representable in single precision - or really, in any precision of binary floating point. What you got was the closest representable value. You can switch to REAL(8)/double and get more digits.
0 Kudos
TimP
Honored Contributor III
550 Views
By the way, the constants as well as the variable declarations must match C double, if you want to see more precision than what is expected according to FLT_DIG, e.g.
use iso_c_binding
....
real(c_double) p1=99999.90_c_double ....

There are ifort-specific options to promote default real constants to equivalent of C double constants, but standard portable methods are as easy to explain and less likely to break.

0 Kudos
Reply