455,514 Members | 1,741 Online
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
9 Replies

 Expert Mod 5K+ P: 9,197 Binary numbers have no decimal portion. You should be able to: Expand|Select|Wrap|Line Numbers float var = -125;   float var1 = 0x165;   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 float var = -125;   float var1 = 0x165;   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

 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 #include  using namespace std; #include  #include    int main() {      string str("123.45");      float result;      stringstream ss;      ss << str;      ss >> result;      cout << result << endl; }   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 #include  using namespace std; #include  #include    int main() {      string str("123.45");      float result;      stringstream ss;      ss << str;      ss >> result;      cout << result << endl; }   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

 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

 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 #include  using namespace std; #include  #include  #include    int main() {      string str("10000000001100000000110011000000");      bitset<32> result;      stringstream ss;      ss << str;      ss >> result;      cout << result.to_ulong() << endl; }   Adrian Jun 4 '07 #7

 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

 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