Passing floats from fortran to .Net
We are migrating an application from VB6 to .net c#.
The application uses a fortran dll to perform some calculations. The fortran is calle开发者_如何学God from our code (VB6 and c#) and we notice that there seems to be some differences in a floating point variable fetched in c# than vb6.
So for example if we have the following fortran code:
subroutine FloatTest (delta)
!ms$attributes DLLEXPORT, ALIAS: 'FloatTest ' :: FloatTest
!ms$attributes REFERENCE :: delta
real*4 delta
delta = 1.0/3.0
END
Any idea why do we get a floating point value of 0.333333343 in c# instead of the 0.3333333 we get in VB6?
Thanks. Niro
The result is accurate. The fortran code is using 4 byte floating point (aka float or single), a format that can store only has 6.5 significant digits. Printing more just produces random noise digits. Use real*8 to improve the accuracy to 15 significant digits. Or modify the print statement to show no more than 6 digits.
You're using limited precision arithmetic. Binary numbers can only approximate 1/3
.
It appears that C# is configured to print more decimal places, since both results agree in the first 7 digits.
The value printed by C# "seems wrong" to you, but it is in fact more accurate than the value printed by VB.
精彩评论