Mathematical Functions (BigFloat)
| Function | Where |
|---|---|
add, subtract, multiply, divide | decimo.bigfloat.arithmetics |
sqrt, exp, ln | decimo.bigfloat.exponential |
root, cbrt | decimo.bigfloat.exponential |
hypot | decimo.bigfloat.exponential |
exp2, exp10, log2, log10, log | decimo.bigfloat.exponential |
expm1, log1p | decimo.bigfloat.exponential |
power | decimo.bigfloat.power |
sin, cos, tan | decimo.bigfloat.trigonometric |
cot, csc, sec | decimo.bigfloat.trigonometric |
arcsin, arccos, arctan | decimo.bigfloat.trigonometric |
arctan2 | decimo.bigfloat.trigonometric |
sinh, cosh, tanh | decimo.bigfloat.hyperbolic |
arcsinh, arccosh, arctanh | decimo.bigfloat.hyperbolic |
pi, ln2, ln10, e | decimo.bigfloat.constants |
fma, remainder, fmod | decimo.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.