问题
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?
回答1:
define your variable as
integer(kind=8) :: intVar
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) 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.
来源:https://stackoverflow.com/questions/23126363/how-to-overcome-integer-overflow-in-fortran-90