- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks!
This isn't explicitly explained in the Fortran book I have.
This isn't explicitly explained in the Fortran book I have.
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