Sunday, October 7, 2012

Behavior of variable data in c and floating point data representation


Data Type 

Format Specifier

Size( In BYTES)


Range
Integral Data

Unsigned Char

%c
1 (8 bits)
to 255
Signed Char (or) Char

%c
1 (8 bits)
-128 to 127
Unsigned Short Int

%hu
2 (16 bits)
to 65535
Signed Short Int (or)
Short Int

%hd
2 (16 bits)
-32768 to 32767
Unsigned Long Int

%lu
4 (32 bits)
to 4294967295
Signed Long Int (or)
Long Int

%ld
4 (32 bits)
-2147483648 to 2147483647
Unsigned Long Long Int

%llu
8 (64 bits)
to 18446744073709551615
Signed Long Long Int (or)
Long Long Int

%lld
8 (64 bits)
-9223372036854775808 to 
9223372036854775807
Real Data

Float

%f , %e
4 (32 bits)
2^-149 to 2^127

Double

%lf , %e
8 (64 bits)
2^-1074 to 2^1023
Long Double
%Lf , %e 




IEEE 754 Standard Representation Of Floating Point Data :


Float( 32 bits)
Double(64 bits)

Sign
1 bit
1 bit

Exponent
8 bits
11 bits

Mantissa/Significand
23 bits
52 bits

Bias Value
127 (0x3f)
1023 (0x3ff)


Eg : 

1. Let Us Try To Represent 20.4 in Floating Point Representation:
                                                                      
20.4 = 10100.0110011001100.... = 10100.0110011001100110011
                                                                                                                          
According To IEEE 754 Standard Representation 20.4 = 01000110011001100110011 *  E ^ (4)

Note : - Only One 1 before the decimal point .

If 20.4 Is Declared As Float.

So , Here Exponent = Bias Value For Float + 4 = 127+4 = 131 = 10000011.
                                                  
                 Significand0110011001100110011

So , 20.4 = 0 10000011 01000110011001100110011 , As Represented Below 


IEEE 754 Standard To Represent Real Data

Note :1

Fixed Point Representation Of Real Data  : 123.456.

Floating Point Representation Of Real Data  : 1.23456 * 10^(2)

Fixed Point Representation Limits The Range Of Real Data To ( 2^ -31 To 2^32 ) Where As Floating Point Representation Facilitates It To Store A Range Of ( 2^ -149 To 2^ 127 ).

Note :2

Its Always Recommended To Use Appropriate Format Specifiers While Handling Data , Such that We Can Avoid Undefined/Unexpected Data .