mantissa and exponent in base 10

Jason Swails jason.swails at gmail.com
Wed Oct 6 20:23:47 EDT 2010


On Wed, Oct 6, 2010 at 6:54 PM, Steven D'Aprano <
steve-REMOVE-THIS at cybersource.com.au> wrote:

> I want the mantissa and decimal exponent of a float, in base 10:
>
> mantissa and exponent of 1.2345e7
>

Perhaps not the prettiest, but you can always use string manipulations:

def frexp_10(decimal):
   parts = ("%e" % decimal).split('e')
   return float(parts[0]), int(parts[1])

You could also create your own mathematical function to do it

def frexp_10(decimal):
   import math
   logdecimal = math.log10(decimal)
   return 10 ** (logdecimal - int(logdecimal)), int(logdecimal)

Testing the timings on those 2 functions for the number 1.235323e+09 on my
machine had the math-version at about 3x faster than the string version
(admittedly it was a single conversion... who knows what the general answer
is).  The math module was loaded at the top of the program, so that didn't
add to the time of the math-execution.

Good luck!
Jason

=> (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?
>
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Jason M. Swails
Quantum Theory Project,
University of Florida
Ph.D. Graduate Student
352-392-4032
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101006/5e866a2e/attachment-0001.html>


More information about the Python-list mailing list