Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a decimal number in scientific notation to IEEE 754

I've read a few texts and threads showing how to convert from a decimal to IEEE 754 but I am still confused as to how I can convert the number without expanding the decimal (which is represented in scientific notation)

The number I am particularly working with is 9.07 * 10^23, but any number would do; I will figure out how to do it for my particular example.

like image 719
Xecure Avatar asked Oct 18 '25 08:10

Xecure


1 Answers

Converting a number from a decimal string to binary IEEE is fairly straight-forward if you know how to do IEEE floating-point addition and multiplication. (or if you're using any basic programming language like C/C++)

There's a lot of different approaches to this, but the easiest is to evaluate 9.07 * 10^23 directly.

First, start with 9.07:

9.07 = 9 + 0 * 10^-1 + 7 * 10^-2

Now evaluate 10^23. This can be done by starting with 10 and using any powering algorithm.

Then multiply the results together.

Here's a simple implementation in C/C++:

double mantissa = 9;
mantissa += 0 / 10.;
mantissa += 7 / 100.;

double exp = 1;
for (int i = 0; i < 23; i++){
    exp *= 10;
}

double result = mantissa * exp;

Now, going backwards (IEEE -> to decimal) is a lot harder.

Again, there's also a lot of different approaches. Here's the easiest one I can think of it.

I'll use 1.0011101b * 2^40 as the example. (the mantissa is in binary)

First, convert the mantissa to decimal: (this should be easy, since there's no exponent)

1.0011101b * 2^40 = 1.22656 * 2^40

Now, "scale" the number such that the binary exponent vanishes. This is done by multiplying by an appropriate power of 10 to "get rid" of the binary exponent.

1.22656 * 2^40 = 1.22656 * (2^40 * 10^-12) * 10^12
               = 1.22656 * (1.09951) * 10^12
               = 1.34861 * 10^12

So the answer is:

1.0011101b * 2^40 = 1.34861 * 10^12

In this example, 10^12 was needed to "scale away" the 2^40. Determining the power of 10 that is needed is simply equal to:

power of 10 = (power of 2) * log(2)/log(10)
like image 195
Mysticial Avatar answered Oct 22 '25 03:10

Mysticial