3

In my algorithm I want to evaluate if the current integer n happens to be closest to the antilogarithm of any positive multiple of 0.1, so 0.1, 1.0, 1.1, 7.9, 21.5 etc.

Antilog? AntiLog(x) = 10x

I found an online calculator that finds the antilog here: http://ncalculators.com/number-conversion/anti-log-logarithm-calculator.htm but nowhere I could find any examples of doing this in Python, or any other programming language.

If I can't use Python to find the antilog of a series of numbers I would have to resort to storing a list of antilog values in my program, for performance I'm might consider that anyways but nevertheless it would be great to figure out how to do this in code.

Update: With the code from first answer I was able to do it, this code fully demonstrates all I was trying to do:

#!/usr/bin/python
import sys
import math

for x in xrange(1, 1000000):
    target = round(math.log10(x),1)
    int_antilog = int(10**target+0.5) #fast round() alternative
    if (x == int_antilog):
        print 'do something at call ' + str(x)
4

2 回答 2

5

Python has a power operator that performs the exponentiation you want to do:

def antilog(x):
    return 10 ** x

Examples:

>>> antilog(0.1)
1.2589254117941673
>>> antilog(3)
1000
>>> inputs = [0.1, 1.0, 1.1, 7.9, 21.5]
>>> outputs = [antilog(x) for x in inputs]
>>> print outputs
[1.2589254117941673, 10.0, 12.589254117941675, 79432823.47242822, 3.1622776601683794e+21]

Round and convert to integers in your favourite way, and you'll be good to go.

于 2013-06-02T16:45:38.937 回答
1
于 2013-06-02T17:40:05.163 回答