Most efficient way to find order of magnitude of float in python

12,067

An easy way to do it would be to take the log of the number, and then floor the result of the log, like this:

import math
def orderOfMagnitude(number):
    return math.floor(math.log(number, 10))
Share:
12,067
fazan
Author by

fazan

I only wrote something because I wanted a badge.

Updated on June 04, 2022

Comments

  • fazan
    fazan almost 2 years

    I have to make some iterative calculation and I need to find the order of magnitude (that bit after e in scientific notation) of my float number every time. Currently I'm doing that with a loop, but it's eating up a lot of time and my calculations take forever.

    while n<1.0:
        order=order-1
        n=n*10
    

    Is there some function that I can use to just swiftly extract this info from the way python stores float numbers? I was taught that floating point numbers store the value and the exponent separately.

    Edit: it's not a duplicate because I'm asking for the most efficient method. The one proposed with the logarithm is actually three times slower than the loop.

    • Aaron
      Aaron over 5 years
      math.floor(math.log10(n))
    • pvy4917
      pvy4917 over 5 years
    • fazan
      fazan over 5 years
      oh yes, I'm an idiot thanks xq
    • fazan
      fazan over 5 years
      actually, this just makes the code slower
    • Eric Postpischil
      Eric Postpischil over 5 years
      I understand your grief about the speed issue not being addressed. However, it is a bit surprising that a single call to what is, hopefully, an optimized routine would be slower than a loop. This suggests there may be other issues involved, in which case providing a reproducible example so others could measure and investigate might help. Additionally, both methods are prone to floating-point rounding errors, so neither may truly return the mathematically correct magnitude. It could be useful to know whether that matters to your application, and why or why not.
    • Eric Postpischil
      Eric Postpischil over 5 years
      (Additionally, Python may not be the best choice when speed is a requirement. A solution may be to use another language rather than to try to optimize Python code.)
    • fazan
      fazan over 5 years
      @EricPostpischil I have to use python because it's a homework assignment. For the same reason, I'm a bit reluctant to show my code. Long story short, I have to calculate a probability and ridiculously small numbers appear so I remove the orders of magnitude before multiplying them again (I manually print out the correct order of magnitude with the end result). The loop presented above is nested in a bigger loop that iterates 10^10 times. It needs roughly an hour to calculate on my pc and I worry my professor just won't tolerate that kind of time.
    • fazan
      fazan over 5 years
      In the bigger loop there is literally just one float division and one float multiplication, so I doubt that can be optimised in any way.
    • Eric Postpischil
      Eric Postpischil over 5 years
      Your situation is an example of the XY problem. Your actual problem X is your attempted solution for the homework is too slow, but the question Y you asked about is faster calculation of the order of magnitude. There might be a better algorithm than you are using to solve the homework, one that does not encounter the tiny-number problem in the first place, or one might mitigate the tiny-number problem in a way other than calculating the order of magnitude. You should ask about the actual problem X.
    • fazan
      fazan over 5 years
      @EricPostpischil ahaha, I'm pretty sure you're right! I just want to divide two stupidly big numbers that are nowhere near the same order of magnitude, so I decided to split it into factors of more manageable fractions
  • fazan
    fazan over 5 years
    actually, if I replace the loop with this, the whole code takes three times longer