What is a c++ formula I can use to change a Binary number into the single precision floating point number?
Floating-Point
Collapse
X
-
Binary numbers have no decimal portion. You should be able to:
[code=cpp]
float var = -125;
float var1 = 0x165;
[/code]
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.Originally posted by weaknessforcatsBinary numbers have no decimal portion. You should be able to:
[code=cpp]
float var = -125;
float var1 = 0x165;
[/code]
Please note: a string containing 110110101010 is not a binary number.Comment
-
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:
[code=cpp]
#include <iostream>
using namespace std;
#include <string>
#include <sstream>
int main()
{
string str("123.45");
float result;
stringstream ss;
ss << str;
ss >> result;
cout << result << endl;
}
[/code]
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.Comment
-
Originally posted by weaknessforcatsA 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:
[code=cpp]
#include <iostream>
using namespace std;
#include <string>
#include <sstream>
int main()
{
string str("123.45");
float result;
stringstream ss;
ss << str;
ss >> result;
cout << result << endl;
}
[/code]
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
100000000011000 000001100110000 00
There is a string now I need a floating point formula in c++ to translate that.Comment
-
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.Comment
-
Extending wfc example:Originally posted by dru103079Yes Here is an example
100000000011000 000001100110000 00
There is a string now I need a floating point formula in c++ to translate that.
[code=cpp]
#include <iostream>
using namespace std;
#include <string>
#include <sstream>
#include <bitset>
int main()
{
string str("1000000000 110000000011001 1000000");
bitset<32> result;
stringstream ss;
ss << str;
ss >> result;
cout << result.to_ulong () << endl;
}
[/code]
AdrianComment
-
Now THAT is using your head. I didn't think about that solution.Originally posted by AdrianHbitset<32> result;Comment
-
Originally posted by AdrianHYeah, 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 !
-TomComment
Comment