Mantu
Mantu

Reputation:

Conversion of Double to value digits and exponent

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

Upvotes: 1

Views: 240

Answers (3)

ssimmons
ssimmons

Reputation: 1

I had to do something very similar. Here's a solution in Python (it hasn't been tested very well):

def normalize(value, fdigits=2):
    """
    Convert a string representing a numerical value to value-digit/exponent form. 
    Round the fractional portion to the given number of digits.

    value    the value (string)
    fdigits  the number of digits to which to round the fractional 
             portion
    """

    # if empty string, return error
    if not value:
        return None

    # split value by decimal
    v = value.split('.')

    # if too many decimals, return error
    if len(v) > 2:
        return None

    # add empty string for fractional portion if missing
    elif len(v) == 1:
        v.append('')

    # assign whole and fractional portions
    (w, f) = v

    # pad fractional portion up to number of significant digits if necessary
    if len(f) < fdigits:
        f += ('0' * (fdigits - len(f)))

    # if the number of digits in the fractional portion exceeds the
    # number of digits allowed by fdigits
    elif len(f) > fdigits:
        # convert both portions to integers; use '0' for whole portion if missing
        (wi, fi) = (int(w or '0'), int(f[:fdigits]))

        # round up if first insignificant digit is gteq 5
        if int(f[fdigits]) >= 5:
            fi += 1

            # roll whole value up if fractional portion rounds to a whole
            if len(str(fi)) > fdigits:
                wi += 1
                fi = 0

        # replace the whole and fractional strings
        (w, f) = (str(wi), ("%0" + str(fdigits) + "d") % fi)

    # derive value digits and exponent
    n = w.lstrip() + f

    l = len(n)
    x = -fdigits

    n = n.rstrip('0')
    x += (l - len(n))

    # return value digits and exponent
    return (int(n), x)

Upvotes: 0

ChrisF
ChrisF

Reputation: 137148

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).

Upvotes: 0

Rutger Nijlunsing
Rutger Nijlunsing

Reputation: 5011

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.

Upvotes: 2

Related Questions