|
! Programmer的夢靨: Results are compiler/compile-option dependent
!---------------------------------------------------------------------
! 冼鏡光,使用浮點數最最基本的觀念,DCview達人部落格,2008年5月12日
! http://blog.dcview.com/article.php?a=Az0HYgNrBDU%3D
!---------------------------------------------------------------------
! 最常見而且最常用的數學運算定律有三個:交換律(commutative law),
! 結合律(associative law),與分配律(distributive law)。
! 在浮點數運算時,只有交換律成立,結合律與分配律都會出問題。
!---------------------------------------------------------------------
PROGRAM round_5
IMPLICIT NONE
INTEGER, PARAMETER :: rp=SELECTED_REAL_KIND(p=6)
REAL(KIND=rp) :: R, x0, x1, x2, x3, x4, x5
INTEGER :: i
!-------------------------------------
R = 3.0_rp
x0 = 0.5_rp
x1 = x0
x2 = x0
x3 = x0
x4 = x0
x5 = x0
DO i = 0, 1000
WRITE(*,'(1X,I4,2X,5G14.7)') i, x1, x2, x3, x4, x5
x1 = (R+1.0_rp)*x1 - R*(x1*x1)
x2 = (R+1.0_rp)*x2 - (R*x2)*x2
x3 = ((R+1.0_rp) - R*x3)*x3
x4 = R*x4 + (1.0_rp-R*x4)*x4
x5 = x5 + R*(x5-x5*x5)
IF( MOD(i,20) == 0 ) READ(*,*)
END DO
END PROGRAM round_5
|
|