tags:

views:

119

answers:

5

hi all, i need to convert decimal number into 64 bit binary float value. if u know any algorithm or anything about it then plz help.

A: 

First google result with the search phrase "64 bit binary float number".

http://en.wikipedia.org/wiki/Floating_point

Victor Hurdugaci
A: 
Decimal decimalNumber = 1234;
Float binaryFloatValue = decimalNumber;
Ian Boyd
The question is tagged C++.
AndiDog
Then i'll leave it to someone else to translate the types into the appropriate C
Ian Boyd
C++ doesn't _have_ a decimal type. I'm not sure about C, but that's not the question either.
MSalters
"C++ doesn't have a decimal type", which points back to the original question.
Ian Boyd
A: 

Assuming you mean a decimal stored inside a string, atof would be a standard solution to convert that to a double value (which is a 64-bit floating point number on the x86 architecture).

std::string s = "0.4";
double convertedValue = atof(s.c_str());

Or similar for C strings:

const char *s = "0.4";
double convertedValue = atof(s);

But if you mean integer number by "decimal number", then just write

int yourNumber = 100;
double convertedValue = yourNumber;

and the value will automatically be converted.

AndiDog
+2  A: 

Use boost::lexical_cast.

double num = lexical_cast<double>(decimal);
Andreas Bonini
+1 Inefficient but simple.
MSalters
A: 

Value casting from a string to double can be implemented by boost::lexical_cast. Type casting from int to double is a part of C++:

double d = (double)i;

It was already mentioned in the previous replies.

If you are interested to know how this casting is implemented, you may refer the sources of the C standard library your compiler is using given that the sources are provided and no floating point co-processor is used for this purpose. Many embedded target compilers do this work "manually" if no floating point co-processor is available.

For the binary format description, please see Victor's reply

Serge C