Thread: how to convert decimal to floating point number

  1. #46
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    To get the sign bit (untested, so I may have done something stupid, apart from not actually writing main and all the rest):
    Code:
    #define SIGN_BIT (1 << 31)
    float first = -56.43;
    int *second;
    unsigned int sign;
    
    second = (int *) &first;
    sign = (*second) & SIGN_BIT; /* note one ampersand for bitwise-and */

  2. #47
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by xuftugulus View Post
    Why use a pointer? The general rule for working with bits is that unsigned types are the best to use even though it really doesn't matter, it is much better when viewed hexadecimally.
    Using binary AND or float is illegal. So is shifting.

    Quote Originally Posted by tabstop View Post
    To get the sign bit (untested, so I may have done something stupid, apart from not actually writing main and all the rest):
    Hey now... are we supposed to spoil the solution?
    Meh. Well, I figure you can get the mantissa and exponent yourself, -EquinoX-.
    It's an assignment, after all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #48
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Elysia View Post
    Hey now... are we supposed to spoil the solution?
    Meh. Well, I figure you can get the mantissa and exponent yourself, -EquinoX-.
    It's an assignment, after all.
    I figured that getting the mantissa was "different enough".

  4. #49
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    True enough.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #50
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by xuftugulus View Post
    Why use a pointer? The general rule for working with bits is that unsigned types are the best to use even though it really doesn't matter, it is much better when viewed hexadecimally.
    Code:
        float number = 56.73f;
        unsigned int sign_mask = 0x80000000;  /* Bit 31 */
        /* Alternative option */
        unsigned int sign_mask = (1U << 31);  /* The same as above */
        unsigned int sign_bit;
    
        sign_bit = number & sign_mask;
        /* sign_mask is : 1000 0000 0000 0000 0000 0000 0000 0000
        in binary therefore, sign_bit is either all zeros or like sign mask in binary,
        which is a large number of course. */
        printf("%#08x\n", sign_mask);
        printf("%#08x\n", sign_bit);
    Try this see what it outputs. Binary and hexademical arithmetic are not taught in school usually but they are not that tough to understand... Numbers are absolute quantities that can be represented in various ways.
    your code generates an error:

    error: invalid operands to binary &

    when trying to add the number with the mask

  6. #51
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can't use binary & with floats. Your original solution works. Use an integer type:
    float d = -56.43f;
    uint32_t nTemp = *(uint32_t*)&d;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #52
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay I tried another code:

    Code:
    #include <stdio.h>
    #define SIGN_BIT (1U << 31)
    
    int main()
    {
      float first = -56.43;
      int *second;
      unsigned int sign;
    
      second = (int *) &first;
      sign = (*second) & SIGN_BIT; /* note one ampersand for bitwise-and */
    
      printf("&#37;u\n", sign);
      return 0;
    }
    however it does not print a 1 or 0 instead of a very large number

  8. #53
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because there is a difference between what you have:
    10000000000000000000000000000000
    And how 1 or 0 is represented:
    00000000000000000000000000000001
    OR
    10000000000000000000000000000000
    Shift the bit into correct position.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #54
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so it should be 1U >> 31 then??

  10. #55
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, read again. After you use binary AND, your sign has the value of
    10000000000000000000000000000000
    So you need to shift that left-most bit so it becomes
    00000000000000000000000000000001
    THEN you have your answer.
    So shift the variable sign.

    The define SIGN_BIT is the mask. You use it to extract the bit you want, so you shouldn't modify that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #56
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    oh yeah I forgot about that, now I am going to try to create the code for the mantissa and exponent and post here if I have problems

  12. #57
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'll admit that I started fiddling with it, too. Though I've already solved it, I'll just show how I extracted the sign bit since you figured that one out already (it's fun! ):

    Code:
    float d = -56.43f;
    uint32_t nTemp = *(uint32_t*)&d;
    uint8_t Sign = (nTemp & 0x80000000) >> (23 + 8);
    Good luck!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #58
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    oh one more question, is there any way in C so that when I extract the mantissa I can have the hidden bit in the value??

  14. #59
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What do you mean "hidden bit"?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #60
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    the 1. x x x x x x

    where 1 is the hidden bit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to read a digit of a floating point number?????
    By spicy_centipede in forum C Programming
    Replies: 15
    Last Post: 07-14-2007, 11:43 AM
  2. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 12:56 PM
  3. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  4. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM