- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page