Is there a way to determine the number of bits/bytes of a real kind value representation? The bit_size
intrinsic works on integer types only and the storage_size
may be (and really gets) affected by the memory alignment requirements. Its definition says that it returns the size occupied in memory by a single array element of the type of its argument. Which, e.g., for 10-bytes reals available in x86_64 architecture, gives 128 bits, not 80.
The answer depends on what you intend to use that information for. Do you want to know how much memory a thing takes up? If so you do want storage_size
. If you want to know how much precision a number can have, use the inquiry functions like precision
, maxexponent
, etc.
I would rather want to get precision, ie. number of bits carrying information, not memory occupation. I know the inquiry functions that you have mentioned but I guess there is no such function which would give number of bits for the exponent and mantissa and/or whole number. The inquiry functions return information in rather human-readable format (as max decimal exponent, number of decimal (!) significant digits etc.)
DIGITS(1.0) gives you the number of significant digits in REAL (p in the real number model of Fortran), which is the number of significant bits if RADIX(1.0) is 2 (which is the case for the binary IEEE modes). Nothing equivalent for the exponent but you have MINEXPONENT(1.0) and MAXEXPONENT(1.0).
Fortran is a high level programming language, so it can be difficult to obtain answers to low level details such as the number of bits in the mantissa of a real, etc. If you will be content with code that will work on machines with a known real number representation, such as IEEE 32-bit reals on little endian machines, consider the following code:
program sigbit
! locate least significant exponent bit of little endian IEEE real.
implicit none
integer i
real x, y
!
x = 1.0
y = 2.0
do i=0, 30
if(btest(transfer(x,i), i) .neqv. btest(transfer(y,i),i))then
print *,'Mantissa has ',i+1,' bits'
print *,'Exponent has ',31-i,' bits'
exit
endif
end do
end program
On the other hand, it is easier to remember 1, 8, (1)+23 as (sign), (biased exponent), (implicit integer part of mantissa), (fractional part of mantissa)
DIGITS()
- that one I have forgotten. Thanks @themos