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

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