Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
告知
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.
29285 ディスカッション

Problem with REAL's

paul321
ビギナー
785件の閲覧回数
I have a problem with floating point values/real's:


program vartest

implicit none
DOUBLE PRECISION :: w,x,z
REAL :: y

w = 0.05
x = 0.05000000000000000000000
y = 0.05
z = sqrt(2.0)

write(*,*) w
write(*,*) x
write(*,*) y
write(*,*) z

end program vartest


Gives:
5.000000074505806E-002
5.000000074505806E-002
5.0000001E-02
1.41421353816986

Why doesn't it gives: 0.05 exactly, but a small variation to it? And the value of the root of 2 also has small difference to the value on my handheld calculator.
0 件の賞賛
3 返答(返信)
bpichon
ビギナー
785件の閲覧回数

Sorry BUT, first, learnFortran !!!!

You write : 0.05 is a single precision constant

This is correct : 0.05d0 for a double precision constant

In the same spirit SQRT(2.0) is computed in single precision

but SQRT(2.D0) is computed in double precision.

emc-nyc
ビギナー
785件の閲覧回数
This is in addition to bpichon said.

Not all rational numbers (like 0.05) have exact representations in a finite number of bits. As an analogy, how many decimal places does it take to exactly represent 1/7?

Delve into the documentation, looking for the way real numbers are approximated in floating point.
paul321
ビギナー
785件の閲覧回数
Thanks!
This isn't explicitly explained in the Fortran book I have.
返信