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; Still not sure what you're on about, but it's just bits. You can set any of them. Or you zero ...

  1. #61
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Still not sure what you're on about, but it's just bits. You can set any of them. Or you zero them out, both as you like. You can hide some bits in there if you want, of course, but they'll also be part of the number.
    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.

  2. #62
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by -EquinoX- View Post
    the 1. x x x x x x

    where 1 is the hidden bit
    Do you mean the sign bit? If you've already extracted it, then you know whether the number is supposed to be positive or negative. You can then multiply appropriately.

    Edit: or do you mean the implicit 1 in front of the mantissa? It's not stored with the number (hence implicit), so you can't extract it. The way to check if it's supposed to be there is to extract the exponent: if the exponent is 0, the number is denormalized so there's no implicit 1.xxx, otherwise there is.
    Last edited by tabstop; 03-03-2008 at 11:08 AM.

  3. #63
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    is there anyway to append bits in C?? because the exponent is located between bits 23-30 and I need to get that bits and transformed it into hexadecimal.. how would I do this?

    and how would I move one bit to the other?? if I add one it will add an integer 1 not a bit
    Last edited by -EquinoX-; 03-03-2008 at 11:12 AM.

  4. #64
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    You can convert it or print it as hex.
    printf("%x", mantissa)
    For example.

    You move bits using shift >> and <<. As in my code I posted.
    You can set bits using binary OR.
    You can zero bits using a combination of binary AND and XOR:
    Code:
    char c = 0xFF;
    c & ~(0x8); // Zero fifth bit
    Last edited by Elysia; 03-03-2008 at 11:14 AM.
    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. #65
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    here's the code to find the exponent, is this right??

    Code:
    #include <stdio.h>
    #define SIGN_BIT (1U << 31)
    #define EXPONENT (11111111U << 31)
    
    int main()
    {
      float first = 56.43;
      int *second;
      unsigned int sign;
      unsigned int mantissa;
    
      second = (int *) &first;
    
      sign = (*second) & SIGN_BIT; /* note one ampersand for bitwise-and */
      sign = sign >> 31;
    
      mantissa = ((*second) << 1)  & EXPONENT;
      mantissa = mantissa >> 23;
    
      printf("&#37;u\n", sign);
      printf("%u\n", mantissa);
    
      return 0;
    }

  6. #66
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Exponent is correct. Mantissa is way off.
    I'll help you, though. The easiest way to construct a proper mask to use is first to write down, in binary, the entire mask. I'll take the sign bit as example:
    10000000000000000000000000000000 (31th bit signed, rest 0).
    Use Windows Calculator (you are using Windows, I hope?), select scientific mode, select binary, past your mask there.
    Select hex and copy your new mask.
    Doing that gives me 80000000.
    So I do nTemp & 0x80000000 and I get the sign bit. The same hold true for the other parts as well.
    Then shift into position.
    Last edited by Elysia; 03-03-2008 at 11:34 AM.
    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. #67
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    oh I am sorry, I meant exponent here

  8. #68
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    11111111U is not eight consecutive bits. If you want eight consecutive bits, the easiest way (I think) is to use hex notation: 0xff. Also note that you don't want to shift 31 places, because the exponent is not 31 places away from where you start.

  9. #69
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    And exponent is also way off, as tabstop notes. I find it easiest to just construct the mask directly instead of shifting, but it's up to you. You could also just construct a mask from 1111 1111 and shift it into place.
    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.

  10. #70
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so is this right now:

    Code:
    #include <stdio.h>
    
    int main()
    {
      float first = -10.000000;
      int *second;
      unsigned int sign;
      unsigned int exponent;
      unsigned int mantissa;
      second = (int *) &first;
    
      sign = (*second) & 0x80000000; /* note one ampersand for bitwise-and */
      sign = sign >> 31;
    
      exponent = ((*second) << 1) & 0xff000000;
      exponent = exponent >> 23;
    
      mantissa = ((*second) << 9) & 0xfffffe00;
      mantissa = mantissa >> 9;
    
      printf("&#37;u\n", sign);
      printf("%u\n", exponent);
      printf("%x\n", mantissa);
    
      return 0;
    }
    Last edited by -EquinoX-; 03-03-2008 at 11:55 AM.

  11. #71
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by -EquinoX- View Post
    so is this right now:

    Code:
    #include <stdio.h>
    
    int main()
    {
      float first = -10.000000;
      int *second;
      unsigned int sign;
      unsigned int exponent;
      unsigned int mantissa;
      second = (int *) &first;
    
      sign = (*second) & 0x80000000; /* note one ampersand for bitwise-and */
      sign = sign >> 31;
    
      exponent = ((*second) << 1) & 0xff000000;
      exponent = exponent >> 23;
    
      mantissa = ((*second) << 9) & 0xfffffe00;
      mantissa = mantissa >> 9;
    
      printf("%u\n", sign);
      printf("%u\n", exponent);
      printf("%x\n", mantissa);
    
      return 0;
    }
    You don't ever ever ever ever ever ever ever ever ever ever want to shift *second.

    As to your bit masks, do you know how to convert binary to hex? Let me take a completely arbitrary example: let's suppose I want to use the bitmask 11000110001100011000110001100011. I would break it up into groups of four:
    1100 0110 0011 0001 1000 1100 0110 0011. I can then turn each group of four into a single hex digit: 12 6 3 1 8 12 6 3 -> 0xc6318c63. So write down your bitmask for exponent and mantissa and convert it to hex.

  12. #72
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    oh so we don't want to shift second?

  13. #73
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Exponent is right. You did that excellent.
    Mantissa is correct, by chance I believe.
    Exponent is not correct.
    You don't need to shift the actual number. Shift the mask or construct the correct mask in the first place.

    Quote Originally Posted by tabstop View Post
    So write down your bitmask for exponent and mantissa and convert it to hex.
    It's far easier to just use a calculator to do the job. And there's nothing wrong with it, either.
    Windows provides an excellent tool for this. I'm sure other OSes do too.

    Quote Originally Posted by -EquinoX- View Post
    oh so we don't want to shift second?
    I'd avoid it, if I were you. You're essentially destroying original data by doing that.
    In this case, you can get away with it, but next time you might not be so lucky.
    Last edited by Elysia; 03-03-2008 at 12:05 PM.
    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.

  14. #74
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I've revised the code so that I won't have to change second, let me know if I messed up.

    Code:
    #include <stdio.h>
    
    int main()
    {
      float first = -10;
      int *second;
      unsigned int sign;
      unsigned int exponent;
      unsigned int mantissa;
      second = (int *) &first;
    
      sign = (*second) & 0x80000000; /* note one ampersand for bitwise-and */
      sign = sign >> 31;
    
      exponent = (*second) & 0x7F800000;
      exponent = exponent << 1
      exponent = exponent >> 23;
    
      mantissa = (*second) & 0x7FFFFF;
      mantissa = mantissa << 9;
      mantissa = mantissa >> 9;
    
      printf("&#37;u\n", sign);
      printf("%u\n", exponent);
      printf("%x\n", mantissa);
    
      return 0;
    }
    and how do I directly eliminate the excess -127 in the exponent??

  15. #75
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Well done. You have the right mask, but you need to stop needlessly shifting around the numbers.
    Shift them right so that the first bit in the number becomes the 0th bit in the whole number.
    Shift it into the right place and you don't have to worry about "excess" data. It will be as it should be.
    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.

Page 5 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