how to convert decimal to floating point number

This is a discussion on how to convert decimal to floating point number within the C Programming forums, part of the General Programming Boards category; So what have you got so far as to your code? -- Mats...

  1. #31
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what have you got so far as to your code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #32
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    The bits are already stored in the number. Otherwise machines would not be able to understand it. To extract the specific values you would use both bitwise & (AND) and binary shifts (<<, >>).
    You would have to construct a mask to isolate the parts of the number that form the mantissa and the exponent.
    yes I know how floating points are layed out in binary the 31st bit is the sign bit and 23-31 is the mantissa and 0-23 is the exponent.
    Wrong here, the 32nd bit is the exponent but bits are indexed 0..31 in a 32 bit number.
    [31 SIGN][30-23 EXPONENT][22-0 MANTISSA]
    Now for the masking part. A mask is a series of 1111 in a machine word usually positioned at the right place so that when AND'ed with a number they zero out all the bits we don't care about.
    If you want the sign bit, where should the 1's be in a 32 bit unsigned integer mask and how many of them also?
    If you can answer that question for the EXPONENT and the MANTISSA i might guide you to code it in C.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #33
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay let me get this clear first, say I have a float in C which is 56.43 the 56.43 are already stored in the computer and all I need to do is to extract them by the masking techniques right??

    so for a 32 bit if I want the sign bit it should be on the 31 bit?

  4. #34
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Yes, it would. The data is always stored raw in memory, but is represented as something else depending on the type.
    So all you do is extract the appropriate bits.
    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. #35
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay I get it, so therefore I don't have to convert the floating point to a binary first and then find the mantissa and so on. I just have to extract the right one because it's already layed out that way in the memory?

  6. #36
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by -EquinoX- View Post
    okay let me get this clear first, say I have a float in C which is 56.43 the 56.43 are already stored in the computer and all I need to do is to extract them by the masking techniques right??

    so for a 32 bit if I want the sign bit it should be on the 31 bit?
    Yes, that's about it.

    If you use 56.43, you should get the result sign: 0, exponent 0x84 (132 decimal), and 0x61b852 as the mantissa.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #37
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay so I got the idea now, how do I do the AND sign at?? do I have to find the location of the memory where 56.43 is stored?? using pointers?? 56.43 is a stored in memory as a 32 bits right??

  8. #38
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by -EquinoX- View Post
    okay so I got the idea now, how do I do the AND sign at?? do I have to find the location of the memory where 56.43 is stored?? using pointers?? 56.43 is a stored in memory as a 32 bits right??
    If it's a float, yes. And one way to get to the float is by pointer, another solution is to overlay it with a union of integer and float.

    Also, you can avoid the AND-business altogether and use a bitfield struct - although that will make it more compiler dependant.

    --
    Mats
    Last edited by matsp; 03-03-2008 at 09:53 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #39
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    pointer seems to be a great idea say that I do this in C:

    float number = 56.87

    int* point = &number

    then point will point to the address of the sign bit of the float?

  10. #40
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably need a cast to make the compiler happy, but yes, that's the idea.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #41
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I just wrote this simple code:

    Code:
    #include <stdio.h>
    
    int main()
    {
      float number = 56.73;
      int* sign_pointer = (int*) &number;
      int temp = *sign_pointer;
    
      printf("&#37;p\n", sign_pointer);
      printf("%d\n", temp);
      return 0;
    }
    I am trying to get the sign bit here, but somehow it gets a very large number

  12. #42
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Of course, you're just putting the whole data into temp.
    This is what -56.43 is in binary (sign, exponent, mantissa):
    1 10000100 11000011011100001010010
    So number containers
    1 10000100 11000011011100001010010
    Then the value of number is assigned to temp, so temp becomes
    1 10000100 11000011011100001010010

    You need to use binary and to extract the bits you want. Then you need to shift the bits into the correct position to get a valid result.
    Btw, consider using a negative number. So when you get 1, you know that you got the right bit, since the sign bit is set when the number is negative.
    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. #43
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    could you give me an example of using this and binary to extract the bits in C code, because I don't quite get it. an example might really help.. thanks

  14. #44
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Binary AND works with a mask.
    It's simple, really...
    The bitwise AND operator (&) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
    Quote from MSDN.

    So say you have this
    Code:
    char c = 0xFF;
    char mask = 0x8;
    char c2 = c & mask;
    This sample will extract the 5th bit. But if you want to use it, you want it in the correct position, because the number will be 0x8, not 0 or 1.
    Code:
    c2 >>= 4;
    This code shifts the number right 4 times, back into its rightful position as bit 0. Thus it will be 0 or 1.
    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. #45
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    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("&#37;#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.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Page 3 of 7 FirstFirst 1234567 LastLast
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, 11:56 AM
  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

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