Thread: how to convert decimal to floating point number

  1. #76
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
      mantissa = mantissa << 9;
      mantissa = mantissa >> 9;
    In particular the above seems very pointless...

    --
    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. #77
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    exponent = exponent << 1
    exponent = exponent >> 23;
    This is also wrong. Why would you shift the exponent left when you want it at the beginning, to the right? If you remove the left shift, it's correct.
    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.

  3. #78
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay I fixed all the errors I have, now how do I translated the mantissa into hexadecimals. like for instance if it's -10 then it should be 0xa00000 in the mantissa
    Last edited by -EquinoX-; 03-03-2008 at 01:20 PM.

  4. #79
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your bits are in the wrong position, that's why it seems you are getting "excess."
    You can use my function for debugging:

    Code:
    void PrintBits(uint32_t nNumber)
    {
    	char Binary[sizeof(nNumber) * 8 + 1] = {0};
    	char Binary2[sizeof(nNumber) * 8 + 1] = {0};
    	for (int i = sizeof(nNumber) * 8 - 1; i >= 0; i--)
    	{
    		if (nNumber & (0x1 << i))
    			Binary[i] = '1';
    		else
    			Binary[i] = '0';
    	}
    	for (int i = sizeof(nNumber) * 8 - 1, j = 0; i >= 0; i--, j++) Binary2[j] = Binary[i];
    	for (int i = 0; i < sizeof(nNumber) * 8; i++)
    	{
    		if (i == 1) cout << " ";
    		else if (i == 1 + 8) cout << " ";
    		cout << Binary2[i];
    	}
    	cout << "\n";
    }
    It will print all bits of a number. Make sure there are no "0" to the right-most, because if there it, you didn't shift it right.
    Shift it all the way to the right. And be careful not to shift too much.
    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. #80
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    Code:
      mantissa = mantissa << 9;
      mantissa = mantissa >> 9;
    In particular the above seems very pointless...

    --
    Mats
    Depending on the size of the mantissa variable, it could potentially set some of the higher bits to zero. I doubt that is the intention though.

  6. #81
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by -EquinoX- View Post
    okay I fixed all the errors I have, now how do I translated the mantissa into hexadecimals. like for instance if it's -10 then it should be 0xa00000 in the mantissa
    No, for -10, you should get:

    Mantissa 0x00200000
    Exponent 0x0082
    Sign 0x01
    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. #82
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    Depending on the size of the mantissa variable, it could potentially set some of the higher bits to zero. I doubt that is the intention though.
    But only necessary if there wasn't an AND on the line before that already had removed any "unwanted" bits, right?

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

  8. #83
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think what brewbuck referred to was if you extracted a number such as
    1111 11111 0000 0000
    Shift left 9 times and we get:
    1000 0000 0000 0000
    Shift right 9 times and we get:
    0000 0001 0000 0000
    Unintentionally destroyed data.
    But it depends on the size. It it was 32-bit, obviously this wouldn't happen.
    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.

  9. #84
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yes I am doing this on a 32 bit machine

  10. #85
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I mean the size of the variable. My example was of a 16-bit variable.
    But it goes to show that you should be careful about shifting the original data since you can unintentionally destroy parts of it.
    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.

  11. #86
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by Elysia View Post
    Your bits are in the wrong position, that's why it seems you are getting "excess."
    You can use my function for debugging:

    Code:
    void PrintBits(uint32_t nNumber)
    {
    	char Binary[sizeof(nNumber) * 8 + 1] = {0};
    	char Binary2[sizeof(nNumber) * 8 + 1] = {0};
    	for (int i = sizeof(nNumber) * 8 - 1; i >= 0; i--)
    	{
    		if (nNumber & (0x1 << i))
    			Binary[i] = '1';
    		else
    			Binary[i] = '0';
    	}
    	for (int i = sizeof(nNumber) * 8 - 1, j = 0; i >= 0; i--, j++) Binary2[j] = Binary[i];
    	for (int i = 0; i < sizeof(nNumber) * 8; i++)
    	{
    		if (i == 1) cout << " ";
    		else if (i == 1 + 8) cout << " ";
    		cout << Binary2[i];
    	}
    	cout << "\n";
    }
    It will print all bits of a number. Make sure there are no "0" to the right-most, because if there it, you didn't shift it right.
    Shift it all the way to the right. And be careful not to shift too much.

    which one is wrong?? I am sure that not all is wrong right?

  12. #87
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, as I mentioned, for the number -10.0, you should get:

    Mantissa 0x00200000
    Exponent 0x0082
    Sign 0x01

    If you don't, then there's a bug somewhere in the code. Look at the results you are getting and compare them to these. If they don't match, then they're wrong.
    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. #88
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by -EquinoX- View Post
    Code:
      int* sign_pointer = (int*) &number;
      int temp = *sign_pointer;
    
      printf("%p\n", sign_pointer);
    %p takes a void *.
    Code:
    printf("%p\n", (void *) sign_pointer);

  14. #89
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    as I said the hidden value, which is 1. x x x x x is included here

    my other question is that say that I have an unsigned int 126 and I want to substract that with the int 127 so the result will be -1, how would I do this??
    Last edited by -EquinoX-; 03-03-2008 at 05:24 PM.

  15. #90
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by -EquinoX- View Post
    as I said the hidden value, which is 1. x x x x x is included here

    my other question is that say that I have an unsigned int 126 and I want to substract that with the int 127 so the result will be -1, how would I do this??
    I usually use "-" to subtract things. Make sure that the result variable is signed. You shouldn't need to cast your unsigned variable, I don't think.

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