tags:

views:

98

answers:

2

For ex.

double size = 10.35;

i should get

value = 1035;

exponent = -2;

so when i re calculate i will get 10.35.

i.e 1035 * 10^-2 = 10.35;

Please help me. Thanks in advance

+2  A: 

In general this is not possible since the fractional part of a double is stored in powers-of-2, and might or might not match powers-of-10.

For example: When looking at powers-of-2 vs powers-of-3: Just like 1/2 == 2^-1 == 5 * 10^-1 has a match, 1/3 == 3^-1 == ?? does not have a match.

However, you can approximate it.

It would have an answer if you would ask for powers-of-2. In that case you can just look at the double representation (see IEEE-754 here) and extract the right bits.

Rutger Nijlunsing
A: 

Very simplistically (in C#):

        double size = 10.36;
        int power = 0;
        while (size != (int)size)
        {
            size *= 10.0;
            power--;
        }
        Console.WriteLine("{0} * 10 to the {1}", size, power);

Though I'm sure with a bit more thought a more elegant solution can be found.

This doesn't go the other way where you've got a large number (103600 say) and want to get the smallest value to some power (1036 * 10^2).

ChrisF