[問題]關於arithmetic overflow

看板Fortran作者 (sin of bleed)時間11年前 (2013/09/30 10:27), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
最近開始學習寫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

09/30 10:53, , 1F
10**12是一個整數,所以會overflow
09/30 10:53, 1F

09/30 10:55, , 2F
可以用9.46e12 或者9.46*10.0**12之類的,只要不是整數
09/30 10:55, 2F

09/30 11:02, , 3F
感謝感謝!!成功了
09/30 11:02, 3F
文章代碼(AID): #1IIE6g0Q (Fortran)
文章代碼(AID): #1IIE6g0Q (Fortran)