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

Problem with REAL's

paul321
Beginner
412 Views
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 Kudos
3 Replies
bpichon
Beginner
412 Views

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.

0 Kudos
emc-nyc
Beginner
412 Views
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.
0 Kudos
paul321
Beginner
412 Views
Thanks!
This isn't explicitly explained in the Fortran book I have.
0 Kudos
Reply