Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Mathematical Functions — Exponential and Logarithmic

Exponential (e^x)

print(Decimal("1").exp())                # e ≈ 2.718281828459045235360287471
print(Decimal("10").exp(precision=50))   # e^10 to 50 digits

Natural logarithm

print(Decimal("10").ln(precision=50))    # ln(10) to 50 digits

For repeated calls, a MathCache can be used to avoid recomputing cached constants:

from decimo.bigdecimal.exponential import MathCache

var cache = MathCache()
var r1 = x1.ln(100, cache)
var r2 = x2.ln(100, cache)  # Reuses cached ln(2) and ln(1.25)

Logarithm with arbitrary base

print(Decimal("100").log(base=Decimal("10")))  # 2
print(Decimal("8").log(base=2))                # 3

log, power, arctan2 and hypot take the other value by keyword: x.log(base=b), x.power(exponent=e), y.arctan2(other=x) and x.hypot(other=y). An Int converts to a Decimal, so a positional x.log(100) would read as base 100 while x.ln(100) means 100 digits; the keyword keeps the two apart. The positional form still works in this release with a deprecation warning, and goes in v0.18.0.

Base-10 logarithm

print(Decimal("1000").log10())  # 3 (exact for powers of 10)
print(Decimal("2").log10(precision=50))