Splitting scientific notation

Bruce M. Axtens bruce.axtens at gmail.com
Fri Mar 13 08:07:03 CET 2009


G'day everyone

I'd like some help with this: I want to be able to write a function 
which receives a number in scientific notation as a string and split out 
of it the coefficient and the exponent as separate items. I could just 
use a regular expression, but the incoming number may not be normalised 
and I'd prefer to be able to normalise and then break the parts out.

A colleague has got part way of an solution using VB6 but it's not quite 
there, as the transcript below shows.

    |cliVe> a = 1e6
    cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
    coeff: 10 exponent: 5

    cliVe> a = 1.1e6
    cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
    coeff: 1.1 exponent: 6

    cliVe> a = 123345.6e-7
    cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
    coeff: 1.233456 exponent: -2

    cliVe> a = -123345.6e-7
    cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
    coeff: 1.233456 exponent: -2

    cliVe> a = -123345.6e+7
    cliVe> ? "coeff: " & o.spt(a) & " exponent: " & o.ept(a)
    coeff: 1.233456 exponent: 12
    |

Any ideas?

Kind regards,
Bruce.



More information about the gmp-discuss mailing list