- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi there
In the following program, "a" is initialized to 0.004, but after running the code, the result is:
a= 4.000000189989805E-003 which is wrong. Then I input ".004" and got a= 4.000000000000000E-003 (right), but when I input .07 the result is a= 7.000000000000001E-002 which is again wrong.
Any comment would be very much appreciated.
Please also let me know if there is a more precise variable than double precision or not.
Many thanks
Hamid
DOUBLE PRECISION a
a=.004
1 print*,'a=',a
read*,a
go to 1
end
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
.004 is a "default real" (that is, single precision) constant. What you want instead is .004D0.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
.004 is a "default real" (that is, single precision) constant. What you want instead is .004D0.
Many thanks for this, but what about inputting ".07".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It's not wrong. That's the closest approximation to .07 in double precision. Most decimal fractions cannot be exactly represented in binary floating point.
I suggest reading The Perils of Real Numbers (three-part series).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Please also let me know if there is a more precise variable than double precision or not.
ifort (and other Fortran 90 compilers) support a wider data type, often known as quad precision or real*16, at significant performance cost. The short introduction to this topic is in the ifort help, under Floating-point Representation.

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