Memory representation of float data type in c

(Both in Turbo c compiler and Linux gcc compiler)
Float numbers are stored in exponential form i.e.

(Mantissa)*10^ (Exponent)

Here * indicates multiplication and ^ indicates power.
In memory only Mantissa and Exponent is stored not *, 10 and ^.
Total size of float data type: 32 bit
Those bits are used in following manner:
Exponent bit: 8
Mantissa bit: 24
Mantissa is signed number, so 24 bit are used as:
Mantissa_sign bit: 1
Mantisaa_data bit: 23
For only mantissa:
Mantissa_sign bit will zero if number is positive and Mantissa_sign bit will one if number is negative.
Exponent is also signed number, So 8 bit are used as:
Exponent_sign bit: 1
Exponent_data bit: 7

Following figure illustrate how floating point number is stored in memory.






Five important rules:

Rule 1: To find the mantissa and exponent, we convert data into scientific form.
Rule 2: Before the storing of exponent, 127 is added to exponent.
Rule 3: Exponent is stored in memory in first byte from right to left side.
Rule 4: If exponent will negative number it will be stored in 2’s complement form.
Rule 5: Mantissa is stored in the memory in second byte onward from right to left side.
Example:

Memory representation of:

         float a = -10.3f;


For this you have to follow following steps:

step1: convert the number (10.3) into binary form
Binary value of 10.3 is: 1010.0100110011001100110011001100110011…

step2: convert the above binary number in the scientific form. Scientific form of 1010.0100110011001100110011001100110011…=
1.01001001100110011001100 11001100110011…*10^3
Note: First digit i.e. 1, decimal point symbol, base of power i.e. 10, power symbol ^ and multiplication symbol * are not stored in the memory.

Step3: find exponent and mantissa and signed bit
Mantissa_data bit in binary = 0100100 11001100 11001101           
                      (Only first 23 bit from left side)
Mantissa_sign bit: 1  (Since it is a negative number)
Exponent in decimal: 3
Question:
Why we have taken right most bit of mantissa_data bit one instead of zero?

Step 5: Add 127 in the exponent and convert in the binary number form.
(Why 127? since size of exponent_data bit is 7 and maximum possible number in seven bit will 1111111 in binary or 127 in decimal)

Exponent= 127+3=130
Binary value of 130 in eight bit: 1000001 0
Exponent_data bit: 1000001
   (Take first seven bit from left side)
Exponent_sign bit: 0 (Take rightmost bit)

Step 6: Now store the Mantissa_data bit, Mantissa_sign bit, Exponent_data bit and Exponent_sign bit at appropriate location as shown in the following figure. 





Note: Mantissa_data bits are stored from left to right while Exponent_data bits are stored from right to left.

How to check above memory representation is correct?

Answer:
We will take one char pointer and visit each byte of a float number and observe the output.
C program:
#include<stdio.h>
int main(){
    int i;
    float f=-10.3f;
    char *p=(char *)&f;
    for(i=0;i<4;i++)
         printf("%d   ",*p++);
    return 0;
}
Output: -51 -52 36 -63
Explanation:
Binary value of -51 in eight bit: 11001101
Binary value of -52 in eight bit: 11001100
Binary value of 36 in eight bit:  00100100
Binary value of -63 in eight bit: 11000001

9 comments:

Anonymous said...

thank you for putting the matter with a beautiful description.

Anonymous said...



.Can you please help me answering this question in C?.

.In C, what number of digits can be accurately stored in a float?.

a. 6
b. 38
c. An unlimited number
d. 4

sandhya said...

why is mantissa bit 1 instead of zero

Unknown said...
This comment has been removed by the author.
Unknown said...

how come the Mantissa_data bit in binary = 0100100 11001100 11001101 and not 0100100 11001100 11001100

from where did this '1' came at the last.
please explain.....

Unknown said...

In the first diagram the exponent sign is in the 8th position of fourth byte in the mantissa bit
but in the second example which has been demonstrated with example the exponent sign bit in the 1st position of second byte of the mantissa bit.. which is correct?

Unknown said...

what about -5.3f the binary format is 0101.0100110011001100110011001100110011… in this case if it is converted in scientific form it will be 0.1010100110011001100110011001100110011… * 10^3....the mantissa bit is 0 which is positive but it should be negative.... what is the memory representation of the above? pls help

MANOJ KUMAR SAHU said...

The number of digits present after decimal in float is 6

Anonymous said...

if suppose exponent is 2 in some case then,

Exponent= 127+2=129
Binary value of 129 in eight bit: 1000000 1
Exponent_data bit: 1000000 (Take first seven bit from left side)

Exponent_sign bit: 1 (Take rightmost bit)


so how can exponent_sign bit be 1 since it is not a negative no.