Thread: how to convert decimal to floating point number

  1. #91
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so say that I have an unsigned integer :

    unsigned int exponent = 126;
    exponent = exponent - 127 will result -1?? I tried this and it gives me a really big number instead

  2. #92
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Quote Originally Posted by -EquinoX- View Post
    so say that I have an unsigned integer :

    unsigned int exponent = 126;
    exponent = exponent - 127 will result -1?? I tried this and it gives me a really big number instead
    Keyword "unsigned" integer. That means that ALL unsigned integers are POSITIVE. Going less than 0 is an overflow(actually, that might not be the correct term. it's more of an underflow). It acts the same as if you add too much to a number except with subtraction.
    Don't quote me on that... ...seriously

  3. #93
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay and say that I want to convert the unsigned int to an int can I do that so that I will be able to do this?

  4. #94
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Quote Originally Posted by -EquinoX- View Post
    okay and say that I want to convert the unsigned int to an int can I do that so that I will be able to do this?
    Yes. You can convert an unsigned int to signed. In fact, the operations (add, subtraction) are the same for unsigned and signed. So the result you get as far as the actual binary data stored in memory from the operation 0 - 1 on signed integers is the same as you would get with unsigned.
    Code:
    unsigned int u = 0 - 5;
    int i = 0 - 5;
    If you looked at the binary memory for both i and u, they would look identical. So the conversion from unsigned to sign is really just a matter of how the program interprets the memory.
    Code:
    unsigned int u = 0 - 5;
    int i = (int)u;
    i would now hold the value -5
    Last edited by Brad0407; 03-03-2008 at 05:59 PM. Reason: code fix
    Don't quote me on that... ...seriously

  5. #95
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay just one more issue.. you know that a mantissa is always precedeed with the 1. 0100101001 and I can't anyway extract the 1 in front of the fraction because it's actually not inside the 1, how do I append the extra one to the 0100101001?? do I shift it right first and then do an or with 1000000000 (masking technique) ?? the problem is if I do this I would lose bit 0 from the mantissa (the right most bit in the mantissa). I need to append the string so that it becomes 24 bit..

  6. #96
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by -EquinoX- View Post
    okay just one more issue.. you know that a mantissa is always precedeed with the 1. 0100101001 and I can't anyway extract the 1 in front of the fraction because it's actually not inside the 1, how do I append the extra one to the 0100101001?? do I shift it right first and then do an or with 1000000000 (masking technique) ?? the problem is if I do this I would lose bit 0 from the mantissa (the right most bit in the mantissa). I need to append the string so that it becomes 24 bit..
    It's not there. So when you print, just print a "1." first, and then print the actual number.

  7. #97
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    well no, after I got the one I would like to convert all that together into a hex

  8. #98
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    A possible solution....

    Code:
    #include <stdio.h>
    
    void PrintTheBits(long lInput, int iStart, int iEnd)
    {
    	unsigned int uiMask = 1 << iStart;
    	unsigned int uiEndMask = 1 << iEnd;
    	while (uiMask >= uiEndMask)
    	{   
    		putchar ((uiMask & lInput) ? '1' : '0');
    		uiMask >>= 1;
    	}
    }
    
    int main(void)
    {
    	float fInput = 56.43f;    
    	long  lLongValue;
    	lLongValue = *((long *)(&fInput));
    	PrintTheBits (lLongValue, 31, 31);
    	putchar ('.');
    	PrintTheBits (lLongValue, 30, 23);
    	putchar ('.');
    	PrintTheBits (lLongValue, 22,  0);
    	putchar ('\n');
    	return 0;
    }

  9. #99
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by -EquinoX- View Post
    well no, after I got the one I would like to convert all that together into a hex
    If you want to print the bits that are there then print the bits that are there as a hex integer using %x, which will print as hex.

    If you want to print "what it means", then remember: the bits that you have come after the binary point, and the implicit one comes before, so it's really "1."001010011100 or whatever. (You can also print that in hex as 0x1.29c or so.)

    There is no context in which you would want to add the implicit one as a bit in front and then print the result as a hex integer.

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