Skip to content
Advertisement

Decimal logs in Sympy

I want to solve an expression in SymPy

lg(10/b^3) = lg10-3lg5 = 1-3lgb (lgb = 5 => b = 10^5) = 1-15 = -14

But then I try to code it like this

b = 10**5
expression = log(10/(b**3), 10)
expression

The result isn’t correct.

Advertisement

Answer

You’ve got the result with ten digits, and not the decimal logarithm. Documentation says,

In SymPy, as in Python and most programming languages, log is the natural logarithm, also known as ln. SymPy automatically provides an alias ln = log in case you forget this.

You can divide result by log(10) or define your own function, like this:

class log10(Function):
    @classmethod
    def eval(cls, x):
        return log(x)/log(10)

Anyway, to get the desired result, you should use either simplify(expr).evalf() or N(expr):

>>> expr = log(10/b**3)/log(10)
>>> expr
-32.2361913019166/log(10)
>>> N(expr)
-14.0000000000000
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement