mantissa and exponent in base 10

Dave Angel davea at
Thu Oct 7 13:11:37 CEST 2010

  On 2:59 PM, Steven D'Aprano wrote:
> I want the mantissa and decimal exponent of a float, in base 10:
> mantissa and exponent of 1.2345e7
> =>  (1.2345, 7)
> (0.12345, 8) would also be acceptable.
> The math module has a frexp() function, but it produces a base-2 exponent:
>>>> math.frexp(1.2345e7)
> (0.73581933975219727, 24)
> Have I missed a built-in or math function somewhere?
First point is that this is a conversion, since the float is stored 
internally using a binary exponent.  So anything you do with logs might 
just give roundoff errors, one way or another.  I'd therefore suggest 
converting the number explicitly, using whatever approach is appropriate 
to the further processing of the number.

If you want the version that will display, then convert it to a string, 
and parse that.

If you want to do further processing, use the decimal module to convert 
it.  Once it's converted, there's an as_tuple() method that will give 
you an exact representation of the original value.  That may not be the 
same mantissa as you'd get with the original, and for numbers like 
9.99999999  or whatever, this could be significant.


More information about the Python-list mailing list