floating point binary program, need help

This is a discussion on floating point binary program, need help within the C Programming forums, part of the General Programming Boards category; Ok, I need to write a floating point binary program with an out put that looks like this: Code: Signed ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    1

    Question 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. #2
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    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.

    You might find this thread relevant too: #define with a dot?

    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. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    23
    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;
    unsigned char MASK;
    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--)
    {
    MASK = 1<<7;
    for(i=0; i<8; i++, c++)
    {
    sprintf(string+c, "%d", (cptr[n] & MASK) ? 1 : 0);
    MASK>>=1;
    }
    }

    // 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. #4
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    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)
    2. don't forget your #include's
    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;
    4. Indent your code.

    I've compiled and run it here, and it produces bits, but I haven't checked them against expected values.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    23
    thanks for pointing out the mistakes

    have not used code tag before, will give it a try now.
    PHP Code:
    test 

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Replies: 7
    Last Post: 12-02-2007, 05:55 AM
  3. Floating point #'s, why so much talk about it?
    By scuzzo84 in forum C Programming
    Replies: 5
    Last Post: 09-20-2005, 05:29 PM
  4. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 08:53 PM
  5. Floating point numbers in a binary file
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 07-31-2003, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21