Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Initializing double precision

h_amini
Beginner
641 Views

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

0 Kudos
4 Replies
Steven_L_Intel1
Employee
641 Views

.004 is a "default real" (that is, single precision) constant. What you want instead is .004D0.

0 Kudos
h_amini
Beginner
641 Views

.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".

0 Kudos
Steven_L_Intel1
Employee
641 Views

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).

0 Kudos
TimP
Honored Contributor III
641 Views
Quoting - h.amini

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.

0 Kudos
Reply