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 (BigFloat)

FunctionWhere
add, subtract, multiply, dividedecimo.bigfloat.arithmetics
sqrt, exp, lndecimo.bigfloat.exponential
root, cbrtdecimo.bigfloat.exponential
hypotdecimo.bigfloat.exponential
exp2, exp10, log2, log10, logdecimo.bigfloat.exponential
expm1, log1pdecimo.bigfloat.exponential
powerdecimo.bigfloat.power
sin, cos, tandecimo.bigfloat.trigonometric
cot, csc, secdecimo.bigfloat.trigonometric
arcsin, arccos, arctandecimo.bigfloat.trigonometric
arctan2decimo.bigfloat.trigonometric
sinh, cosh, tanhdecimo.bigfloat.hyperbolic
arcsinh, arccosh, arctanhdecimo.bigfloat.hyperbolic
pi, ln2, ln10, edecimo.bigfloat.constants
fma, remainder, fmoddecimo.bigfloat.ieee
from decimo.bigfloat.constants import pi
from decimo.bigfloat.exponential import exp, ln
from decimo.bigfloat.trigonometric import sin

print(pi(200).to_bigdecimal_rounded(60))
print(exp(BigFloat.from_int(1), 120))
print(ln(BigFloat.from_int(2), 120))
print(sin(BigFloat.from_string("1e10", 60), 53))

That last one is worth a word. Reducing an argument of 10^10 by pi/2 has to cancel its leading bits against pi, so the number of bits of pi needed is counted from the argument rather than fixed — sin(2^300) fetches three hundred bits of pi before the first bit of its answer is right. An argument whose leading bit sits beyond what a precision may be is refused rather than answered badly.

Each function except the constants and the four arithmetic ones is also a method. fma, remainder and fmod always take a precision, as they do as functions. The rest come in two forms. Without a precision it keeps this value’s own precision and rounds half to even. With a precision, and optionally a rounding mode, it behaves like the free function asked for those:

var x = BigFloat.from_int(2, 53)
print(x.sin())                         # 53 bits, half to even
print(x.sin(200))                      # 200 bits, half to even
print(x.sin(200, RoundingMode.ROUND_UP))
print(BigFloat.from_int(8, 53).log2())        # 3
print(BigFloat.from_int(27, 53).log(base=BigFloat.from_int(3, 53)))  # 3
print(BigFloat.from_int(27, 53).log(200, base=3).precision)  # 200

The four methods of two values, log, power, hypot and arctan2, take the other value by keyword: x.log(base=b), x.power(exponent=e), x.hypot(other=y) and y.arctan2(other=x). That keeps one rule for every method: a positional Int is a precision. Without a precision they keep the wider of the two precisions. The positional form, x.log(b), still works in this release with a deprecation warning, and goes in v0.18.0.