Thread: how to convert decimal to floating point number

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, but this is C99 or C++.
    Here should be C89 compatible code:
    Code:
    #define bitmask(i) (1U << i)
    
    int main(void)
    {
    	/* The number of bits in an int */
    	const int intSize = 31;
    
    	/* The decimal number to convert */
    	int value;
    	int i;
    
    	char bit = '0';
    
    	printf("Please enter a decimal number to be converted: ");
    	scanf("&#37;d", &value);
    
    	/* Print the bits of the decimal value */
    	for (i = intSize; i >= 0; --i)
    	{
    		/* Assume bitmask(i) is unset */
    		bit = '0';
    
    		/* Pick the character to print */
    		if (value & bitmask(i))
    		{
    			bit = '1';
    		}
    
    		putchar(bit);
    	}
    
    	putchar('\n');
    }
    Don't forget to include stdio.h.
    Last edited by Elysia; 03-03-2008 at 09:25 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.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, I am guessing that it does not recognise the // comments. Change them to the /* */ style comments and see what happens, or maybe specify std=c99.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    oh it works now.. I guess it's just the comments using // that doesn't work in my compiler, but I was able to fix it now..

    I don't however understand this part:

    #define bitmask(i) (1U << i)

    can someone explain this to me??

    and one more thing the code only works for integer numbers how about if I have a double value that I want to convert such as 1234.83?
    Last edited by -EquinoX-; 03-03-2008 at 09:28 AM.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Shift 1 left i times. Shifting is an essential part of converting between bases.
    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. #20
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    how do I shift in C?

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    n >> 2;
    Shift n right 2 times.
    n << 2;
    Shift n left 2 times.
    N is the variable you wish to shift and 2 is the number of times you wish to shift, so replace them as appropriate.

    Well, I'd stay away from trying to convert floating points because they aren't represented as numbers, unlike integers.
    You could modify the example to work with doubles, but you wouldn't get a sane output.
    Last edited by Elysia; 03-03-2008 at 09:40 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. #22
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    how do I shift in C?
    << shifts bits left and >> shifts bits right.

    00000001 left shifted by 3 is 00001000. 00001000 right shifted by 3 is back to 00000001. But make sure that you only shift by a nonnegative amount and that the number you're shifting is unsigned. That way you don't have any weird problems.

  8. #23
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yes, my assignment here is actually to create a code that will extract from a floating point number to it's mantissa and exponent

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, that's an entirely different thing. That is not binary. And so you know, a number cannot be represented as binary. It must be a string or you would get weird results.
    But I suggest you take a look at how floating points layout are, in binary terms. That's the first step.
    Then you can extract them using binary operators. Specifically and.
    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. #25
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    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.

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then go ahead and use binary operator AND to extract them.
    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.

  12. #27
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    and now lets use an example to do this, say I have a number −118.625 which is an IEEE floating point number and I want to convert that to the mantissa, sign bit, and exponent. First I know that the sign bit is 1 because it's negative and now the number.. this is where I get confused, especially doing this in C because 118.625 should be converted first into it's binary value then normalized and how would I do this??

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Btw, you're describing a float, not a double.
    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. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, do you want the actaul mantissa and exponent?

    if so, you need to get the float to overlay an integer of same length, then use AND to get the portions out.

    If you want something else, please clarify what you actually want.

    --
    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.

  15. #30
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yes I want the actual mantissa and exponent from a floating point number

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