Re: Converting binary floating point numbers to decimal?



On May 8, 6:18 pm, mike3 <mike4...@xxxxxxxxx> wrote:
Hi.

I was working on a program for a computer, which would be able to
manipulate floating point numbers of arbitrary size, and of course
they'd be binary, expresed as multiples of powers of 2. But what if I
need to display such a number in decimal? Ie. how do you convert the
expression

x.xxxxxxxxxxxxx * 10^n (x = bit, 10 = 2 (decimal))

to

x.xxxxxxxxxxxxx * 10^n (x = digit, 10 = 10 (decimal))?

I figured one could take the significand, convert it to decimal
(integer part (one bit) becomes 0 or 1, and fractional part is
converted separately), then multiply the full result of that by 5
(10/2). But what about the _exponent_? If we have, say 1.1000 x 10^11
(bin), we could start with 1.1000, turn that to decimal by first
converting the integer part "1" (bin) to decimal, giving 1, then
converting the fraction by multiplying by 10 (decimal) or 1010
(binary) repeatedly, giving a sequence of digits:

binary 0.1000 x 1010 = 101.0000, int part = 101 (bin) = 5 (dec)
binary 0.0000 x 1010 = 0.0000, int part = 0 (bin) = (dec)

and we stop here (we'll just keep getting zeroes, it's a terminating
decimal and it terminates at this point. Besides, we can only get
log10(2^4) decimal digits anyway due to precision limits in this
example (4 bits of fraction, 1 bit of integer). What do you do with
that "~0.204th of a digit", anyway? Just treat it like a real digit
and round it accordingly?), giving the decimal fraction as 0.5. Adding
the integer part, 1, we get 1.5 (dec) = 1.1000 (bin). But we have that
pesky exponential 10^11 (bin) = 2^3 (dec) = 8 (dec) to deal with.
Although here we can cheat and just multiply 1.5 * 8 to get 12 (or 1.2
x 10^1 (dec)) as the decimal equivalent of 1.1000 x 10^11 (bin), what
if, say, we have an exponent of 32 bits, which can go up to over four
BILLION, and there's no way it would be practical to store such a
gigantic number to do our decimal conversions. (like if we want to
display the floating point number
1.110011010101111000111010100001 x 10^1101110101000001 (bin)? (32-bit
mantissa, 16-bit exponent. Storing the full decimal expansion of
2^56641 is NOT efficient, you know!)?) There has to be a trick. What
is it? Thanks for any help. I couldn't seem to find anything anywhere
else about this.

Well I'm sure there's some clever algorithm that language developers
use, but here's a sort of brute force approach that works in
scientific
notation.

Your original number is a * 2^b.
You want to write it as c * 10^d.

2^b = [10^log10(2)] ^ b = 10^[b * log10(2)]

So you could start by calculating d1 = b*log10(2). But that
is not a whole number, and in scientific notation c*10^d,
you want a whole number d. So you need to separate d1 into
its integer and fractional parts.

d = floor(d1)
dfrac = d1 - d

So now we have 2^b = 10^d1 = 10^(d + dfrac)
= 10^d * (10^dfrac)

Thus a*2^b = [a * 10^dfrac] * 10^d, or c = a * 10^dfrac

Now you have your number in base-10 scientific notation.

This method requires a log10 calculation and a
10^x calculation, so it's fairly calculation intensive. Of
course, log10(2) is a constant so you could pre-store that.

- Randy

.



Relevant Pages

  • Re: Converting binary floating point numbers to decimal?
    ... expresed as multiples of powers of 2. ... giving a sequence of digits: ... example (4 bits of fraction, ... 10^x calculation, ...
    (sci.math)
  • Converting binary floating point numbers to decimal?
    ... manipulate floating point numbers of arbitrary size, ... converting the integer part "1" to decimal, giving 1, then ... giving a sequence of digits: ... example (4 bits of fraction, ...
    (sci.math)
  • Re: Poll: Are PCs Turing Machines?
    ... > In my example, I don't even care about the calculation itself, only ... This is false UNLESS the condition is "I need to see all the digits". ... Instead we can imagine that in Heaven, the Riemann Hypothesis is solved ... If the Turing Abomination (a Turing machine executing such inelagant ...
    (sci.math)
  • Re: Hexadecimal Numbers
    ... My problem comes where I am converting decimal to hexadecimal. ... Di are integers between 0 and B-1 inclusive, called the digits. ... We can identify this form to the quotient of N divided by B: ... Notice that we don't convert directly from a representation in base B1 ...
    (comp.programming)
  • Re: Change of base
    ... easily be dealt with by first converting them to base ten). ... Consider conversion to a rational base. ... The radix point lies between ap+1 and ap+2. ... To obtain a fraction to this base, ...
    (sci.math.num-analysis)