作者sea010461 (sin of bleed)
看板Fortran
標題[問題]關於arithmetic overflow
時間Mon Sep 30 10:27:52 2013
最近開始學習寫fortran照著書上打入某個fortran
用gfortran compile給出錯誤訊息
------------------------
ch0504_2.f90:5.38:
real , parameter :: Light_Year=9.46*10**12
1
Error: Arithmetic overflow at (1)
ch0504_2.f90:17.28:
Light_minute = Light_Year/(365.25 * 24.0 * 60.0)
1
--------------------------
可是用ifort compile會過。
讓我很困惑,同樣的寫法,用不一樣的compiler給出不一樣的結果,
要用哪種寫法才不會發生這樣的事呢?
書上的code如下
program ch0504
implicit none
real :: Light_Minute, Distance, Elapse
integer :: Minute, Second
real , parameter :: Light_Year=9.46*10**12
! Light_year : Distance travelled by light
! in one year in km
! Light_minute : Distance travelled by light
! in one minute in km
! Distance : Distance from sun to earth in km
! Elapse : Time taken to travel a
! distance (Distance) in minutes
! Minute : integer number part of elapse
! Second : integer number of seconds
! equivalent to fractional part of elapse
!
Light_minute = Light_Year/(365.25 * 24.0 * 60.0)
Distance = 150.0 * 10 ** 6
Elapse = Distance / Light_minute
Minute = Elapse
Second = (Elapse - Minute) * 60
print *,' Light takes ' , Minute,' Minutes'
print *,' ' , Second,' Seconds'
print *,' To reach the earth from the sun'
end program ch0504
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 128.138.41.222
1F:推 terryys:10**12是一個整數,所以會overflow 09/30 10:53
2F:→ terryys:可以用9.46e12 或者9.46*10.0**12之類的,只要不是整數 09/30 10:55
3F:→ sea010461:感謝感謝!!成功了 09/30 11:02