# Getting bitsize of a real kind type

**URL:** <https://fortran-lang.discourse.group/t/getting-bitsize-of-a-real-kind-type/2113>\
**Category:** Help\
**Created:** [October 18, 2021, 10:24pm UTC](https://fortran-lang.discourse.group/t/getting-bitsize-of-a-real-kind-type/2113 "2021-10-18T22:24:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [October 18, 2021, 10:24pm UTC](https://fortran-lang.discourse.group/t/getting-bitsize-of-a-real-kind-type/2113/1 "2021-10-18T22:24:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [October 19, 2021, 12:45am UTC](https://fortran-lang.discourse.group/t/getting-bitsize-of-a-real-kind-type/2113/3 "2021-10-19T00:45:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [October 20, 2021, 11:17am UTC](https://fortran-lang.discourse.group/t/getting-bitsize-of-a-real-kind-type/2113/4 "2021-10-20T11:17:34Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [October 20, 2021, 11:46am UTC](https://fortran-lang.discourse.group/t/getting-bitsize-of-a-real-kind-type/2113/5 "2021-10-20T11:46:57Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [October 20, 2021, 1:00pm UTC](https://fortran-lang.discourse.group/t/getting-bitsize-of-a-real-kind-type/2113/6 "2021-10-20T13:00:37Z")

</div>

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:

```auto
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)

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [October 21, 2021, 6:53am UTC](https://fortran-lang.discourse.group/t/getting-bitsize-of-a-real-kind-type/2113/7 "2021-10-21T06:53:43Z")

</div>

`DIGITS() ` - that one I have forgotten. Thanks @themos
