# Thread: floating point binary program, need help

1. ## floating point binary program, need help

Ok, I need to write a floating point binary program with an out put that looks like this:

Code:
```Signed bit = x
2nd part = xxxxxxxx
3rd part = xxxxxxxxxxxxxxxxxxxxxxx```
the 2nd part is the exponent and the 3rd is the mantissa.
I'm cheating a bit on it by using an if statement to derive the signed bit, so i only need two arrays, and less variables (i think):
Code:
```int x;
scanf("%f",x);
if(x < 0){
signed = 0;
}
else{signed=1;}```
ok, that's just a snippet, and its within a while loop, but you get the point. The problem i'm having isn't really code based, though any help with that is appreciated. My main problem is that i don't understand floating point binary very well. how the exponent and mantissa are derived from the original decimal number. I've looked at tutorials online but they were still a bit confusing, and my professor spends more time browsing the web or sleeping than he does teaching. Can anyone break it down simply for me? I understand converting decimal to bin, but i've never done it with floating point numbers, only integers. Please help.

2. Your code is flawed I think
Code:
`scanf("%f",x);`
you're asking scanf for a pointer to float, but x is an int.

The mantissa is a normalised binary number x in the range 0>x>=1. In your example you have 23 bits, so the LSB of mantissa is 1^-24 and the MSB is worth 0.5

the exponent is usually an offset binary no. So in your case I suspect the exponent is in the range 2^-128 to 2^127 (here ^ is raise to power) .

It would be useful to have a little more information on the format of the floating point that your teacher is using for this exercise.

There are standards for this sort of thing - perhaps a search on google for "IEEE floating point standard" will give you more info.

To convert from float x into the format you need I would do this way.
1. Get the sign bit, as you have, then remove this from x (so that x>=0)
2. Take log(base2) of x.
3. The integer part of this result is the exponent in base 2. (you may have to add a constant to this to allow for the offset (if any))
4. The fractional part is the most significant bits of the mantissa.

Does this help?

3. hi there:

i have been working on a similiar problem. i wanted to write a code that will show me what a number (int, float, unsigned int...) would look like in memory. i think my code works, take a look:

main()
{
float to_be_tested = 3.14;
unsigned char *cptr = (unsigned char *) &to_be_tested;
char string[33];
int i, n, c=0;

//n starts at 3 so it will pring as big endian format
for(n=3; n>=0; n--)
{
for(i=0; i<8; i++, c++)
{
sprintf(string+c, "%d", (cptr[n] & MASK) ? 1 : 0);
}
}

// print to screen
string[33] = (char)NULL;
printf("tested decimal value -> %f :: bit pattern ->\n", to_be_tested);
c=0;

for(n=0; n<4; n++)
{
for(i=0; i<8; i++, c++)
{
printf("%c", string[c]);
}
printf(" ");
}
printf("\n");
}

please correct me if its flawed

cheers

CHUN

4. It looks like you have a plan there chunlee, but the codes a bit rough.

Here's my pennyworth.

0. use code tags
1. use int main (void)
3.
Code:
```    // print to screen
string[33] = (char)NULL;```
string[33] isn't defined - string runs from [0] to [32]
also casting NULL to char is strange, and not guaranteed to be portable.
why not just say string[32] = 0;
``` test  ```