Home > Blockchain >  How to convert to logarithmic scale and back?
How to convert to logarithmic scale and back?

Time:12-16

import math

def log_n_back(x, base):
  return math.pow(math.log(x, base), base)

x = 14
y = log_n_back(x, math.e)
print(y) # 13.983525601880974

y = log_n_back(x, 10)
print(y) # 3.9113921541997523

The one using math.e at least approximates the answer. But the one using 10 is just wrong.

Further context: I have to work with numbers between 1,000 and 100,000.

CodePudding user response:

Swap the order of the arguments to pow():

return math.pow(base, math.log(x, base))

Then it will do what you intended it to do, and results will be very much closer to what you want.

  • Related