How can I get the minimum and maximum exponent for 32- and 64-bit real numbers? I am doing some work to avoid underflows and overflows and would need to know those numbers.
I would also need the base for floating point numbers.
Is it possible in fortran to get the equivalent of ilmach
?
For non-zero reals, the numeric model looks like s*b^e*\sum_{k=1}^{p}f_k*b^{-k}
.
To get the value of the base b
use radix()
. The minimum and maximum values of the exponent e
can be found with exponent
combined with tiny
and huge
.
use, intrinsic :: iso_fortran_env, only : real32, real64
print 1, "real32", RADIX(1._real32), EXPONENT(TINY(1._real32)), EXPONENT(HUGE(1._real32))
print 1, "real64", RADIX(1._real64), EXPONENT(TINY(1._real64)), EXPONENT(HUGE(1._real64))
1 FORMAT (A," has radix ", I0, " with exponent range ", I0, " to ", I0, ".")
end
The function range()
returns the range of exponents. The intrinsic function huge()
returns the maximum allowable number for a given numeric kind. From that you can see the exponent too by employing a logarithm. See also selected_real_kind()
.
The base for gfortran and other normal compilers is 2, but you can test it using radix()
. They may be some base 10 kinds in the future.
In the same manual I linked you will find other useful intrinsics like tiny(), precision(), epsilon(), spacing()
, you can follow the links in "See also:".