Mathematics library of the Chaos language. You can install this spell with:
occultist install math
and import it with:
import math
Returns the mathematical constant
kaos> math.pi()
3.14159
Returns the mathematical constant
kaos> math.e()
2.71828
Returns the mathematical constant
kaos> math.golden_ratio()
1.61803
Returns the positive infinity inf
constant.
kaos> math.inf()
inf
Returns the not-a-number nan
constant.
kaos> math.nan()
nan
Returns the cosine of x
, in degrees.
kaos> math.cos(60)
0.5
Returns the sine of x
, in degrees.
kaos> math.sin(30)
0.5
Returns the tangent of x
, in degrees.
kaos> math.tan(45)
1
Returns the arc cosine of x
, in degrees.
kaos> math.acos(0.5)
60
Returns the arc sine of x
, in degrees.
kaos> math.asin(0.5)
30
Returns the arc tangent of x
, in degrees.
kaos> math.atan(1.0)
45
Returns the arc tangent of x
(x-coordinate) and y
(y-coordinate) in degrees. Two parameters version of atan()
kaos> math.atan2(-10.0, 10.0)
135
Returns the hyperbolic cosine of x
.
kaos> math.cosh(0.693147)
1.25
Returns the hyperbolic sine of x
.
kaos> math.sinh(0.693147)
0.75
Returns the hyperbolic tangent of x
.
kaos> math.tanh(0.693147)
0.6
Returns the nonnegative area hyperbolic cosine of x
.
kaos> math.acosh(3.762196)
2
Returns the nonnegative area hyperbolic sine of x
.
kaos> math.asinh(3.626860)
2
Returns the nonnegative area hyperbolic tangent of x
.
kaos> math.atanh(0.761594)
1
Returns x
:
kaos> math.exp(5)
148.413
Returns the mantissa and exponent of x
:
kaos> import math
kaos> dict frexp = math.frexp(8)
kaos> frexp['mantissa']
0.5
kaos> frexp['exponent']
4
Returns the result of multiplying x
by 2
raised to the power of y
:
It's the inverse function of frexp()
kaos> math.ldexp(0.95, 4)
15.2
Returns the natural logarithm of x
:
kaos> math.ln(5.5)
1.70475
Returns the base-b
logarithm of x
:
kaos> math.log(81, 3)
4
Returns the natural logarithm of 1 + x
:
The result is more accurate than log(x)
when x
near zero.
kaos> math.log1p(1.0)
0.693147
Returns the base-2
logarithm of x
:
Generally more accurate than log(x, 2)
.
kaos> math.log2(1024.0)
10
Returns the base-10
logarithm of x
:
Generally more accurate than log(x, 10)
.
kaos> math.log10(1000)
3
Returns the integer and fractional parts of x
.
kaos> dict modf = math.modf(3.141593)
kaos> modf['integer']
3
kaos> modf['fraction']
0.141593
Returns x
, minus 1
:
kaos> math.expm1(1.0)
1.71828
Returns x
raised to the power of y
:
kaos> math.pow(2, 3)
8
Returns the square root of x
:
kaos> math.sqrt(1024)
32
Returns the cubic root of x
:
kaos> math.cbrt(27)
3
Returns the hypotenuse of a right-angled triangle whose legs are x
and y
:
kaos> math.hypot(3.0, 4.0)
5
Returns the error function value for x
.
kaos> math.erf(1.0)
0.842701
Returns the complementary error function value for x
.
kaos> math.erfc(1.0)
0.157299
Returns the gamma function at x
.
kaos> math.gamma(0.5)
1.77245
Returns the natural logarithm of the absolute value of
the gamma function
(log-gamma) at x
.
kaos> math.lgamma(0.5)
0.572365
Rounds x
upward, returning the smallest integral value that is not less than x
.
kaos> math.ceil(3.8)
4
kaos> math.ceil(-3.8)
-3
Rounds x
downward, returning the largest integral value that is not greater than x
.
kaos> math.floor(5.6)
5
kaos> math.floor(-5.6)
-6
Computes the remainder that results from performing integer division of x
by y
.
kaos> math.mod(23, 4)
3
Computes the remainder that results from performing floating-point division of x
by y
.
kaos> math.fmod(18.5, 4.2)
1.7
Rounds x
toward zero, returning the nearest integral value that is not larger in magnitude than x
.
kaos> math.trunc(3.8)
3
kaos> math.trunc(-3.8)
-3
Returns the integral value that is nearest to x
, with halfway cases rounded away from zero.
kaos> math.round(3.8)
4
kaos> math.round(-3.8)
-4
Returns the floating-point remainder of x / y
(rounded to nearest).
kaos> math.remainder(18.5, 4.2)
1.7
Returns the same as remainder()
, but also returns the quotient
.
kaos> dict remquo = math.remquo(10.3, 4.5)
kaos> remquo['remainder']
1.3
kaos> remquo['quotient']
2
Returns a value with the magnitude of x
and the sign of y
.
kaos> math.copysign(-10.0, -1.0)
-10
Returns the next representable value after x
in the direction of y
.
kaos> math.nextafter(0.0, 1.0)
3.6452e-4951
kaos> math.nextafter(0.0, -1.0)
-3.6452e-4951
Returns the absolute value of x
:
kaos> math.abs(3.1416)
3.1416
kaos> math.abs(-10.6)
10.6
Returns the factorial of x
. Returns -1
if x
is negative.
kaos> math.factorial(5)
120
kaos> math.factorial(-5)
-1
Returns whether x
is a finite value.
kaos> math.is_finite(0.0)
true
kaos> math.is_finite(1.0 / 0.0)
false
Returns whether x
is an infinity value (either positive infinity or negative infinity).
kaos> math.is_inf(0.0)
false
kaos> math.is_inf(1.0 / 0.0)
true
Returns whether x
is a nan
(not-a-number) value.
kaos> math.is_nan(0.0)
false
kaos> num nan_example = math.sqrt(-1.0)
kaos> nan_example
-nan
kaos> math.is_nan(nan_example)
true
Returns whether x
is a normal value: i.e., whether it is neither inf
(infinity), nan
(not-a-number), zero or subnormal.
kaos> math.is_normal(1.0)
true
kaos> math.is_normal(0.0)
false
kaos> math.is_normal(1.0 / 0.0)
false
Returns whether the sign of x
is positive.
kaos> math.is_positive(1.0)
true
kaos> math.is_positive(-1.0)
false
Returns whether the sign of x
is negative.
kaos> math.is_negative(1.0)
false
kaos> math.is_negative(-1.0)
true