如何知道整數變數的數字範圍?

0 views
Skip to first unread message

sjgau02

unread,
Oct 20, 2007, 1:03:29 PM10/20/07
to NTUST_F90
某位同學私下e-mail 問我的問題,
如何知道我目前所使用的 fortran compiler,
其整數型態的 default , 其所能夠儲存的 數字大小的範圍是多少?
其原理 又是什麼?

我把這個問題和答案公開,同時
我也鼓勵大家和 這位同學一樣,踴躍的提出問題,
就能夠獲得 解答,就能夠學習進步。

謝謝 這位同學。
答案將 儘快公佈。

sjgau02

unread,
Oct 20, 2007, 1:27:43 PM10/20/07
to NTUST_F90
! 如何知道 我目前所使用的 fortran compiler,
! 其整數型態的 default , 其所能夠儲存的 數字大小的範圍是多少?
! 其原理 又是什麼?

!dec$if (.false.)
以下為輸出,你可以看得出來,2- byte 的 integer no2,
當他大於 21870 的時候,下一輪的正確答案是 65610,
可是卻出來錯誤的答案 74

那是 integer*2 的範圍是 -32768 到 +32767
所以才會出錯。

no 變數被宣告為 integer no ,從下面的輸出
可以看的出來,和 no4 一樣。

no4 被宣告為 integer*4, 所以
由此可以知道:我目前所使用的 compiler
其 default 的整數為 integer*4

4- bytes integer 的範圍是
-2147483648 到 +2147483647

no= 30 , no2= 30 , no4= 30
no= 90 , no2= 90 , no4= 90
no= 270 , no2= 270 , no4= 270
no= 810 , no2= 810 , no4= 810
no= 2430 , no2= 2430 , no4= 2430
no= 7290 , no2= 7290 , no4= 7290
no= 21870 , no2= 21870 , no4= 21870
no= 65610 , no2= 74 , no4= 65610
no= 196830 , no2= 222 , no4= 196830
no= 590490 , no2= 666 , no4= 590490
no= 1771470 , no2= 1998 , no4= 1771470
no= 5314410 , no2= 5994 , no4= 5314410
no= 15943230 , no2= 17982 , no4= 15943230
no= 47829690 , no2= -11590 , no4= 47829690
no= 143489070 , no2= 30766 , no4= 143489070
no= 430467210 , no2= 26762 , no4= 430467210
no= 1291401630 , no2= 14750 , no4= 1291401630
no= -420762406 , no2= -21286 , no4= -420762406
Press any key to continue
!dec$end if

implicit none
integer no
integer*2 no2
integer*4 no4

no= 10
no2= 10
no4= 10

do while (no4 > 0)
no= no + no + no
no2= no2 + no2 + no2
no4= no4 + no4 + no4

print *, 'no= ', no, ', no2= ', no2, ', no4= ', no4
end do

end

Reply all
Reply to author
Forward
0 new messages