- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
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.
コピーされたリンク
3 返答(返信)
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
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.
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
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.
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
Thanks!
This isn't explicitly explained in the Fortran book I have.
This isn't explicitly explained in the Fortran book I have.
