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; Ah, but this is C99 or C++. Here should be C89 compatible code: Code: #define bitmask(i) (1U << i) int ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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 08: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
    21,444
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 08:28 AM.

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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 08: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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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

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