Inaccurate Logarithm in Python

This is to be expected with computer arithmetic. It is following particular rules, such as IEEE 754, that probably don’t match the math you learned in school.

If this actually matters, use Python’s decimal type.

Example:

from decimal import Decimal, Context
ctx = Context(prec=20)
two = Decimal(2)
ctx.divide(ctx.power(two, Decimal(31)).ln(ctx), two.ln(ctx))

Leave a Comment