# Can anyone explain representation of float in memory?

This is not duplicate question as I read the previous question.

Can anyone help me in understanding `how float values are stored in the memory` .

My doubt is here float values contain '`.'` (`for example 3.45`) how the `'.'` will be represented in the memory?

Can anyone please clarify me with a diagram?

• How about the least expected source, Wikipedia? en.wikipedia.org/wiki/Floating_point#Internal_representation – 9000 Oct 21 '13 at 6:45
• And you can add the main article: IEEE floating point – mouviciel Oct 21 '13 at 7:47
• If you're like me, and you like to learn by playing with things, putting in inputs and receiving outputs, etc, check this site out: binaryconvert.com/convert_double.html – KChaloux Oct 21 '13 at 19:50
• There are a WIDE variety of floating-point formats, all different. The IEEE floating point is the most common nowadays, but it is not the only one. When I was undergrad, I had to learn the CDC 6600 floating-point format, and it had some advantages over IEEE, the biggest being 48 bits of mantissa for single-precision. IEEE is limited to about 24 bits of mantissa for single-precision, which is why every introductory numerical methods class these days tells the students "Always use double, not float." – John R. Strohm Aug 30 '18 at 15:13
• See floating-point-gui.de and remember that URL – Basile Starynkevitch Aug 31 '18 at 10:30

The decimal point is not explicitly stored anywhere; that's a display issue.

The following explanation is a simplification; I'm leaving out a lot of important details and my examples aren't meant to represent any real-world platform. It should give you a flavor of how floating-point values are represented in memory and the issues associated with them, but you will want to find more authoritative sources like What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Start by representing a floating-point value in a variant of scientific notation, using base 2 instead of base 10. For example, the value 3.14159 can be represented as

```
0.7853975 * 22

```

0.7853975 is the significand, a.k.a. the mantissa; it's the part of the number containing the significant digits. This value is multiplied by the base 2 raised to the power of 2 to get 3.14159.

Floating-point numbers are encoded by storing the significand and the exponent (along with a sign bit).

A typical 32-bit layout looks something like the following:

`````` 3 32222222 22211111111110000000000
1 09876543 21098765432109876543210
+-+--------+-----------------------+
| |        |                       |
+-+--------+-----------------------+
^    ^                ^
|    |                |
|    |                +-- significand
|    |
|    +------------------- exponent
|
+------------------------ sign bit
``````

Like signed integer types, the high-order bit indicates sign; 0 indicates a positive value, 1 indicates negative.

The next 8 bits are used for the exponent. Exponents can be positive or negative, but instead of reserving another sign bit, they're encoded such that 10000000 represents 0, so 00000000 represents -128 and 11111111 represents 127.

The remaining bits are used for the significand. Each bit represents a negative power of 2 counting from the left, so:

```
01101 = 0 * 2-1 + 1 * 2-2 + 1 * 2-3 + 0 * 2-4 + 1 * 2-5
= 0.25 + 0.125 + 0.03125
= 0.40625

```

Some platforms assume a "hidden" leading bit in the significand that's always set to 1, so values in the significand are always between [0.5, 1). This allows these platforms to store values with a slightly greater precision (more on that below). My example doesn't do this.

So our value of 3.14159 would be represented as something like

```
0 10000010 11001001000011111100111
^     ^               ^
|     |               |
|     |               +--- significand = 0.7853975...
|     |
|     +------------------- exponent = 2 (130 - 128)
|
+------------------------- sign = 0 (positive)

value= -1(sign) * 2(exponent) * (significand)
value= -10 * 22 * 0.7853975...
value= 3.14159...

```

Now, something you'll notice if you add up all the bits in the significand is that they don't total 0.7853975; they actually come out to 0.78539747. There aren't quite enough bits to store the value exactly; we can only store an approximation. The number of bits in the significand determines the precision, or how many significant digits you can store. 23 bits gives us roughly 6 decimal digits of precision. 64-bit floating point types offer enough bits in the significand to give roughly 12 to 15 digits of precision. But be aware that there are values that cannot be represented exactly no matter how many bits you use. Just as values like 1/3 cannot be represented in a finite number of decimal digits, values like 1/10 cannot be represented in a finite number of bits. Since values are approximate, calculations with them are also approximate, and rounding errors accumulate.

The number of bits in the exponent determines the range (the minimum and maximum values you can represent). But as you move towards your minimum and maximum values, the size of the gap between representable values increases. That is, if you can't exactly represent values between 0.785397 and 0.785398, then you can't exactly represent values between 7.85397 and 7.85398 either, or values between 78.5397 and 78.5398, or values between 785397.0 and 785398.0. Be careful when multiplying very large (in terms of magnitude) numbers by very small numbers.

• "but instead of reserving another sign bit" What you're describing is the exact behavior of a signed integer. – Simon Dec 7 '17 at 19:41

The `.` is not stored at all. First, you should understand engineering notation, which has a fixed-precision factor and an integer exponent: `1` is 1.0 · 100 = `1.0E0`, 2 is `2.0E0`, 10 is `1.0E1` etc. This allows for very short notation of large numbers. One billion is `1.0E9`. The factor before the `E` is usually notated as a fixed-precision number: `1.00000E9`. A result from this is that the number one billion and one = 1,000,000,001 and one billion are both the same in this notation, when the precision isn't large enough. Also note that the factor never needs a leading zero. Instead, the exponent can be decremented until that is no longer the case.

In memory, a floating point number is represented similarly: One bit has the sign, some bits form the factor as a fixed-precision number (“mantissa”), the remaining bits form the exponent. Significant differences to base-10 engineering notation is that of course now the exponent has base 2. The exact size of each part depends on the exact floating-point standard you are using.

• This is "Scientific notation". "Engineering notation" is when the exponent is restricted to multiples of 3. – Clement J. Oct 21 '13 at 7:02
• That base 2 is used is very important. It determines which values can be stored exactly and which aren't, and even if you can't be bothered to develop an intuition for which values (I know I can't) you should at least remember that decimal digits are a completely useless way of thinking about floats. – user7043 Oct 21 '13 at 7:11
• @delnan: If it helps, each bit in the mantissa is half that of the higher bit. So, floats can store sums of negative powers of two: 1/2, 1/4, 1/8, 1/16, 1/32, 1/64, 1/128, and so on, to the limit of the mantissa. So the epsilon in a 32-bit `float` is `2^-22 * exponent`, or about 1/4194304. – greyfade Oct 21 '13 at 20:02