cfloating-point

How are floating point numbers stored in memory?


I've read that they're stored in the form of mantissa and exponent

I've read this document but I could not understand anything.


Solution

  • To understand how they are stored, you must first understand what they are and what kind of values they are intended to handle.

    Unlike integers, a floating-point value is intended to represent extremely small values as well as extremely large. For normal 32-bit floating-point values, this corresponds to values in the range from 1.175494351 * 10^-38 to 3.40282347 * 10^+38.

    Clearly, using only 32 bits, it's not possible to store every digit in such numbers.

    When it comes to the representation, you can see all normal floating-point numbers as a value in the range 1.0 to (almost) 2.0, scaled with a power of two. So:

    So, what is needed to encode this, as efficiently as possible? What do we really need?

    This is encoded as follows, according to the IEEE-754 floating-point standard.

    In addition to the normal floating-point values, there are a number of special values:

    Finally, the following is a handful of concrete examples (all values are in hex):