By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

Floating-Point

P: 9
What is a c++ formula I can use to change a Binary number into the single precision floating point number?
May 31 '07 #1
Share this Question
Share on Google+
9 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Binary numbers have no decimal portion. You should be able to:

Expand|Select|Wrap|Line Numbers
  1. float var = -125;
  2.  
  3. float var1 = 0x165;
  4.  
Please note: a string containing 110110101010 is not a binary number.
May 31 '07 #2

P: 9
Binary numbers have no decimal portion. You should be able to:

Expand|Select|Wrap|Line Numbers
  1. float var = -125;
  2.  
  3. float var1 = 0x165;
  4.  
Please note: a string containing 110110101010 is not a binary number.
Then what formula would you use to translate a 32 bit string to single precision floating point.
May 31 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
A 32 bit string would have 4 characters.

Do you mean a 32 byte string??

Do you have an example string??

Maybe "123.45"???

Perhaps you could try:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. #include <string>
  4. #include <sstream>
  5.  
  6. int main()
  7. {
  8.      string str("123.45");
  9.      float result;
  10.      stringstream ss;
  11.      ss << str;
  12.      ss >> result;
  13.      cout << result << endl;
  14. }
  15.  
The thing is, unless you are involved in some deep Computer Science effort involving number theory, these issues seldom arise in the real world. The conversion is handled by the << operator of stringstream.

stringstream is a template (a text item) so you could look in there with your editor and see what it does.
May 31 '07 #4

P: 9
A 32 bit string would have 4 characters.

Do you mean a 32 byte string??

Do you have an example string??

Maybe "123.45"???

Perhaps you could try:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. #include <string>
  4. #include <sstream>
  5.  
  6. int main()
  7. {
  8.      string str("123.45");
  9.      float result;
  10.      stringstream ss;
  11.      ss << str;
  12.      ss >> result;
  13.      cout << result << endl;
  14. }
  15.  
The thing is, unless you are involved in some deep Computer Science effort involving number theory, these issues seldom arise in the real world. The conversion is handled by the << operator of stringstream.

stringstream is a template (a text item) so you could look in there with your editor and see what it does.

Yes Here is an example

10000000001100000000110011000000

There is a string now I need a floating point formula in c++ to translate that.
Jun 4 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
Write a loop.

Start at the right end of the string.

If a 1 add 2^0 to your total

Move left.

If a 1 add 2^1 to your total

Move left.

If a 1 add 2^2 to your total.
etc...

The result will be an integer.

You cannot produce a floating point value from arithmetic involving integers.

If you need a float value, be prepared for data loss. float has only 6 significant figures. double has 15.
Jun 4 '07 #6

AdrianH
Expert 100+
P: 1,251
Yes Here is an example

10000000001100000000110011000000

There is a string now I need a floating point formula in c++ to translate that.
Extending wfc example:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. #include <string>
  4. #include <sstream>
  5. #include <bitset>
  6.  
  7. int main()
  8. {
  9.      string str("10000000001100000000110011000000");
  10.      bitset<32> result;
  11.      stringstream ss;
  12.      ss << str;
  13.      ss >> result;
  14.      cout << result.to_ulong() << endl;
  15. }
  16.  

Adrian
Jun 4 '07 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
bitset<32> result;
Now THAT is using your head. I didn't think about that solution.
Jun 4 '07 #8

AdrianH
Expert 100+
P: 1,251
Now THAT is using your head. I didn't think about that solution.
Yeah, I didn't know about bitset till some time in the last month. Someone told me about it here on TSDN.


Adrian
Jun 5 '07 #9

P: 21
Yeah, I didn't know about bitset till some time in the last month. Someone told me about it here on TSDN.


Adrian

Dang that's some nice solutions ! :)
It really pays of reading a lot of threads, you learn so much out of them.
It really surprises me that C++ can do all that. Quite the nice language :) Though you have to think about those special tricks when the problems that could require them arise, that's allways equally hard sadly enough, though that's my own experience ;)

Cheers adrian !
-Tom
Jun 5 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.