C Programming - Floating Point Issues - Discussion

Discussion Forum : Floating Point Issues - General Questions (Q.No. 3)
3.
If the binary eauivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000 0000 0000 0000, what will be the output of the program (on intel machine)?
#include<stdio.h>
#include<math.h>
int main()
{
    float a=5.375;
    char *p;
    int i;
    p = (char*)&a;
    for(i=0; i<=3; i++)
        printf("%02x\n", (unsigned char)p[i]);
    return 0;
}
40 AC 00 00
04 CA 00 00
00 00 AC 40
00 00 CA 04
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
117 comments Page 4 of 12.

Shilpa M Raj said:   1 decade ago
Normalize:

Given Floating point Number = 5.375

In binary:
5 = (101) and
0.375 = (011)
{ 0.375*2 = 0.750 --> 0
0.750*2 = 1.500 --> 1
0.500*2 = 1.000 --> 1 }

i.e., 5.375 = 101.011

For single precision representation, decimal point is shifted by 2 times.
i.e., 1.01011*2^2 = 1.01011*100 where 2 is true exponent.

According to IEEE, the representation has three fields:
----------------------------
| S | E | F |
----------------------------
S is one bit representing the sign of the number (0/1 & hidden bit)
E is an 8-bit biased integer representing the exponent
F is an unsigned integer

For single precision representation (the emphasis in this class)
n = 23
bias = 127

2 is the true exponent. For the standard form, it needs to be in 8-bit, biased-127 representation.

2
+127
-----
129

129 in 8-bit, unsigned representation is 1000 0001

The mantissa stored (F) is the stuff to the right of the radix point in the normalized form. We need 23 bits of it.

010110 00000000000000000

Put it all together (and include the correct sign bit):

S E F
0 1000 0001 010110 00000000000000000

The values are often given in hex, so here it is

0100 0000 1010 1100 0000 0000 0000 0000
0x 4 0 A C 0 0 0 0

Chaitanya@rgukt said:   1 decade ago
Can any please explain How to do normalization of floating point?

Preeti said:   1 decade ago
Thank you preeti:).

Shyam said:   1 decade ago
Understand the memory you will understand it easy

Used is little endian type of memory storage i.e., higher byte at lower address

Data:40 AC 00 00
100:40
101:AC
102:00
103:00

Now pointer to memory location points to 103
If it is used as character pointer it grabs a byte as char pointer is capable of handling only value of its size

On the other end if a integer pointer is used it would give output same as it is


For detailed view try this example,

int x=Ox12345678;
char *cp;
short int *sip;
int *ip;
ip=sip=cp=&x;
printf("%x %x %x",*cp,*sip,*ip);

O/P:0x78 0x5678 0x12345678

Nazia said:   1 decade ago
The format string used for each of the prints is %02X, which I've always interpreted as 'print the supplied int as a hexadecimal value with at least two digits'.

Suvidha said:   1 decade ago
In the statement p= (char*) &a; we are doing type casting since p is a char pointer you cannot assign float variable to it a pointer variable can hold the address of another variable only if the variable has the same data type of that as pointer variable.

Bhaskar said:   1 decade ago
Binary equivalent of 5.375 in normalised form is

0100 -> 4
0000 -> 0

1010 -> A
1100 -> C

0000 -> 0
0000 -> 0

0000 -> 0
0000 -> 0

Since the PC's (intel processors) use " LITTLE ENDIAN" byte order, the higher order byte of number is stored in lowest address. The result is written from bottom to top.

Ragini said:   1 decade ago
Thanks preeti.

Er.Nitesh mathur said:   1 decade ago
Preethi is wrong because. Defination is given by preethi is totally wrong.

" Big ENDIAN" byte order-> the higher order byte of number is stored in lowest address. The result is written from top to bottom.

" LITTLE ENDIAN"-> byte order, the higher order byte of number is stored in higher address. The result is written from bottom to top.

Chithra said:   1 decade ago
Thanks preethi and pradeep.


Post your comments here:

Your comments will be displayed after verification.