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 1 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

Piya said:   1 decade ago
Little Endian means that the lower order byte of the number is stored in memory at the lowest address, and the higher order byte is stored at the highest address. That is, the little end comes first.

For example, a 4 byte, 32-bit integer.


Byte3 Byte2 Byte1 Byte0.


will be arranged in memory as follows:


Base_Address+0 Byte0.
Base_Address+1 Byte1.
Base_Address+2 Byte2.
Base_Address+3 Byte3.


Example :Intel processors use "Little Endian" byte order.


"Big Endian" means that the higher order byte of the number is stored in memory at the lowest address, and the lower order byte at the highest address. The big end comes first.


Base_Address+0 Byte3.
Base_Address+1 Byte2.
Base_Address+2 Byte1.
Base_Address+3 Byte0.


Motorola, Solaris processors use "Big Endian" byte order.

Venkatesh said:   1 decade ago
#include<stdio.h>
int main()
{
int i=1; // i=0001 it will store in memory like this 00000000 00000000 00000000 00000001
char *p; // p is declared as a char pointer
p=(char*)&i; //here integer is typecasting to char i.e. 00000001
00000000
00000000
00000000 and pointer is always points to starting address.
if(*p)
printf("=====little-endian);
else
printf("=====big-endian);
return 0;
}

Pradeep said:   1 decade ago
Yeah she is correct but in the end only.

Take one more example:

Say we have to store 0102 0304H in memory (where H : for hexadecimal code).

Say initial memory address is 100H. Then

Memory Address | Big endian | Little Endian
100 : | 01 | 04
101 : | 02 | 03
102 : | 03 | 02
103 : | 04 | 01


and in BIG Endian MSB(most significant bit) is stored at lowest address location (as in my example 01 is the MSB) while Little Endian format follows the exactly opposite trend.

So now as the PC's (intel processors) use " LITTLE ENDIAN" byte order, The result is written from bottom to top.

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

Praful said:   1 decade ago
I think this value stored in this manner.

0100 0000 1010 1100 0000 0000 0000 0000 which is 40ac0000.


float taken 4 byte in memory in program p=(char*)&a casting this to char type nw p(it char pointer its point to i byte value) point to lower byte of the a(5.375) which is 0000 0000.

p[0]-> contain the fist lower byte data 00(because print bye the hexa format specifier withe 2 width)
p[1]-> contain 00
p[2]-> contain ac
p[3]-> contain 40

Then output become ...0000ac40.

Prabhudeva said:   1 decade ago
Foating point base conversion of 5.375 would be
101.01100 00000000 00000000 00000000 00000000 00000000 00000000 10000000.

(char*) will be make it as a character array.
5.375 = 101.01100 00000000 00000000 00000000 00000000 00000000 00000000 10000000.

And coz of unsigned char, it wud be treated as,
p[0] = 00000000 00000000.
p[1] = 00000000 00000000.
p[2] = 10101100 00000000.
p[2] = 00000000 10000000.

=> in hexadecimal form (%02x) :
p[0] = 00.
p[1] = 00.
p[2] = ac.
p[3] = 40.

Gaurav Kumar said:   1 decade ago
Floating point base conversion of 5.375 would be
101.01100 00000000 00000000 00000000 00000000 00000000 00000000 10000000

(char*) will be make it as a character array.
5.375 = 101.01100 00000000 00000000 00000000 00000000 00000000 00000000 10000000

and bcoz of unsigned char, it wud be treated as,
p[0] = 00000000 00000000
p[1] = 00000000 00000000
p[2] = 10101100 00000000
p[2] = 00000000 10000000

=> in hexadecimal form (%02x) :
p[0] = 00
p[1] = 00
p[2] = ac
p[3] = 40

Vishal pandey said:   1 decade ago
Foating point base conversion of 5.375 would be
101.01100 00000000 00000000 00000000 00000000 00000000 00000000 10000000

(char*) will be make it as a character array.
5.375 = 101.01100 00000000 00000000 00000000 00000000 00000000 00000000 10000000

nd coz of unsigned char, it wud be treated as,
p[0] = 00000000 00000000
p[1] = 00000000 00000000
p[2] = 10101100 00000000
p[2] = 00000000 10000000

=> in hexadecimal form (%02x) :
p[0] = 00
p[1] = 00
p[2] = ac
p[3] = 40

Binit said:   1 decade ago
Binary equivalent of 5 = 101 & 0.375 = 011
so 5.375= 101.011

Normalization means in the left side of decimal should only have one integer example: normalisation of 101.345 = 1.01345* 100

So normalized form is 1.01011*100

So according to IEEE notation 5.375 can be represented as
0100 0000 1010 1100 this can be expressed as 40AC0000
but intel follows little endianess so ans will be

C). 0000AC40


Post your comments here:

Your comments will be displayed after verification.