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

problem with REAL*8 in Compaq visual fortran for windows 6.6.0 2000

jschirli
Beginner
595 Views
I have a simple test for you to carried out
Program test
implicit Real*8 (a-h,o-z)

x=.5d0-.45d0
write (*,*) x
stop
end program test
you are supose to get .050000000000000 as your answer but instead you get 4.999999999999999E-2 ...
Following this unexpected answer I have two questions:
Why isn't it accurate?
The second what can I do in order to have an accurate value, knowing that this fault result later on my full program in total false answer.
0 Kudos
3 Replies
mrvivaldi
Beginner
595 Views
Looks right to me.
.5d0 and .45 d0 cannot be expressed exactly in the internal floating point format.
Can Only get close. Therefore any Arimetic performed can also only get close. Ever here about the engineer and the mathematician.
0 Kudos
TimP
Honored Contributor III
595 Views

Close. 0.5d0 does have exact representation both in decimal and binary format, but 0.45d0could match toony 15 or16 decimal significant figures. The compiler is not permitted to short cut the conversions to binary format implied in the posted example.

If you're thinking in decimal, my high school training way back indicated that the notation 0.45 implies 2 decimal significant figures accuracy. Subtract that from 0.5 and you have 0.05 with 1 significant figure accuracy. So don't display so many garbage digits, and everybody should be happy.

0 Kudos
randystonesifer
Beginner
595 Views

If this type of precision/round-off issue is a real problem for you, check out Mathematica. I believe that it has the capabilities you would need.

Randy

0 Kudos
Reply