Extracting the exponent from scientific notation

3.3k Views Asked by At

I have a bunch of numbers in scientific notation and I would like to find their exponent. For example:

>>> find_exp(3.7e-13)
13

>>> find_exp(-7.2e-11)
11

Hence I only need their exponent ignoring everything else including the sign.

I have looked for such a way in python but similar questions are only for formatting purposes.

1

There are 1 best solutions below

0
On BEST ANSWER

Common logarithm is what you need here, you can use log10 + floor:

from math import log10, floor

def find_exp(number) -> int:
    base10 = log10(abs(number))
    return abs(floor(base10))

find_exp(3.7e-13)
# 13

find_exp(-7.2e-11)
# 11