如何克服Fortran 90中的整数溢出? [英] how to overcome integer overflow in fortran 90?
问题描述
我在fortran 90中存在整数溢出问题.整数的最大范围是10位数.但是,我要处理范围为1E13的整数.那么如何避免这种情况呢?
hi i am having some problem with integer over flow in fortran 90. The maximum range of integer number is 10 digits.but, I want to deal with integer of the range 1E13. So what is the way of avoiding this?
推荐答案
将变量定义为
integer(kind=8) :: intVar
要使用可移植的代码,请使用selected_int_kind函数.就您而言
To have a portable code, use the function selected_int_kind. In your case
integer, parameter, k14 = selected_int_kind(14)
integer(kind=k14) :: intVar
selected_int_kind(r)返回最小整数类型的种类值,该类型值可以表示从-10 ^ r(不包括)到10 ^ r(不包括)的所有值,这就是我使用14的原因.有关selected_int_kind的更多详细信息,请参见 http://gcc.gnu.org/onlinedocs/gfortran/SELECTED_005fINT_005fKIND.html .请记住,selected_int_kind可能返回-1,如果没有整数类型可以容纳所选范围.
selected_int_kind(r) returns the kind value of the smallest integer type that can represent all values ranging from -10^r (exclusive) to 10^r (exclusive, that is why I used 14). For more details on selected_int_kind, See http://gcc.gnu.org/onlinedocs/gfortran/SELECTED_005fINT_005fKIND.html. Keep it mind that selected_int_kind might return -1, ff there is no integer kind that accommodates the chosen range.
这篇关于如何克服Fortran 90中的整数溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!