|
"Wenlong" wrote in message <k9bcps$ids$1@newscl01ah.mathworks.com>...
> I need to convert a decimal number (can be both positive or negative, and has a long string after decimal point) into binary. And at some point, I have to convert the binary strings back to decimal numbers.
- - - - - - - -
If the decimal number is not an integral multiple of a (positive or negative) power of 2, it cannot be converted to a binary fraction without error - that is, the binary fraction would have infinitely many digits. If you have determined the maximum number of fractional binary digits that you can make use of, just multiply your decimal number by the corresponding power of 2, round it to the nearest integer, convert that to a binary integer, and then shift the binary point back in the binary string by that power.
Roger Stafford
|