计算 [⚠️ need proofread]
用于计算和处理数字值的模块。
这些定义是calc模块的一部分,不会默认导入。除了下面列出的功能之外,calc模块还定义了常量pi、tau、e、inf和nan。 [⚠️]
函数
abs自始可用
Calculates the absolute value of a numeric value.
展开示例
#calc.abs(-5) \
#calc.abs(5pt - 2cm) \
#calc.abs(2fr) \
#calc.abs(decimal("-342.440"))

value
The value whose absolute value to calculate.
pow自始可用
Raises a value to some exponent.
展开示例
#calc.pow(2, 3) \
#calc.pow(decimal("2.5"), 2)

base
The base of the power.
If this is a decimal, the exponent can only be an integer.
exponent
The exponent of the power.
exp0.5.0起可用
Raises a value to some exponent of .
展开示例
#calc.exp(1)

exponent
The exponent of the power.
sqrt自始可用
Calculates the square root of a number.
展开示例
#calc.sqrt(16) \
#calc.sqrt(2.5)

value
The number whose square root to calculate. Must be non-negative.
root0.11.0起可用
Calculates the real th root of a number.
If the number is negative, then must be odd.
展开示例
#calc.root(16.0, 4) \
#calc.root(27.0, 3)

radicand
The expression to take the root of.
index
The value of .
sin自始可用
Calculates the sine of an angle.
When called with an integer or a float, they will be interpreted as radians.
展开示例
#calc.sin(1.5) \
#calc.sin(90deg)

angle
The angle whose sine to calculate.
cos自始可用
Calculates the cosine of an angle.
When called with an integer or a float, they will be interpreted as radians.
展开示例
#calc.cos(1.5) \
#calc.cos(90deg)

angle
The angle whose cosine to calculate.
tan自始可用
Calculates the tangent of an angle.
When called with an integer or a float, they will be interpreted as radians.
展开示例
#calc.tan(1.5) \
#calc.tan(90deg)

angle
The angle whose tangent to calculate.
asin自始可用
Calculates the arcsine of a number.
展开示例
#calc.asin(0) \
#calc.asin(1)

value
The number whose arcsine to calculate. Must be between and .
acos自始可用
Calculates the arccosine of a number.
展开示例
#calc.acos(0) \
#calc.acos(1)

value
The number whose arccosine to calculate. Must be between and .
atan自始可用
Calculates the arctangent of a number.
展开示例
#calc.atan(0) \
#calc.atan(1)

value
The number whose arctangent to calculate.
atan20.3.0起可用
Calculates the four-quadrant arctangent of a coordinate.
The four-quadrant arctangent of is defined as the argument of the complex number .
Returns an angle between -180deg and 180deg.
Note that this function accepts , not .
展开示例
#calc.atan2(1, 1) \
#calc.atan2(-2, -3)

x
The coordinate.
y
The coordinate.
sinh自始可用
Calculates the hyperbolic sine of a hyperbolic angle.
The hyperbolic sine of is defined as follows:
展开示例
#calc.sinh(0) \
#calc.sinh(1.5)

value
The hyperbolic angle whose hyperbolic sine to calculate.
cosh自始可用
Calculates the hyperbolic cosine of a hyperbolic angle.
The hyperbolic cosine of is defined as follows:
展开示例
#calc.cosh(0) \
#calc.cosh(1.5)

value
The hyperbolic angle whose hyperbolic cosine to calculate.
tanh自始可用
Calculates the hyperbolic tangent of a hyperbolic angle.
The hyperbolic tangent of is defined as follows:
展开示例
#calc.tanh(0) \
#calc.tanh(1.5)

value
The hyperbolic angle whose hyperbolic tangent to calculate.
asinh0.15.0起可用
Calculates the inverse hyperbolic sine of a number.
The inverse hyperbolic sine of is defined as follows:
展开示例
#calc.asinh(0) \
#calc.asinh(1)

value
The number whose inverse hyperbolic sine to calculate.
acosh0.15.0起可用
Calculates the inverse hyperbolic cosine of a number.
The inverse hyperbolic cosine of is defined as follows:
展开示例
#calc.acosh(1) \
#calc.acosh(2.5)

value
The number whose inverse hyperbolic cosine to calculate. Must be greater than or equal to .
atanh0.15.0起可用
Calculates the inverse hyperbolic tangent of a number.
The inverse hyperbolic tangent of is defined as follows:
展开示例
#calc.atanh(0) \
#calc.atanh(0.5)

value
The number whose inverse hyperbolic tangent to calculate. Must be between and (exclusive).
log自始可用
Calculates the logarithm of a number.
If the base is not specified, the logarithm is calculated in base ten.
展开示例
#calc.log(100)

value
The number whose logarithm to calculate. Must be strictly positive.
base默认值:10.0
The base of the logarithm. May not be zero.
ln0.5.0起可用
Calculates the natural logarithm of a number.
展开示例
#calc.ln(calc.e)

value
The number whose logarithm to calculate. Must be strictly positive.
erf0.15.0起可用
Applies the error function to a number.
The value of the error function at is defined as follows:
展开示例
#calc.erf(0.2)

value
The number at which to calculate the error function.
fact0.3.0起可用
Calculates the factorial of a number.
展开示例
#calc.fact(5)

number
The number whose factorial to calculate. Must be non-negative.
perm0.3.0起可用
Calculates a permutation.
Returns the -permutation of , or the number of ways to choose items from a set of with regard to order, defined as follows:
展开示例
#calc.perm(5, 3)

base
The value of : The number of items to choose from. Must be non-negative.
numbers
The value of : The number of items to choose. Must be non-negative.
binom0.3.0起可用
Calculates a binomial coefficient.
Returns the -combination of , or the number of ways to choose items from a set of without regard to order, defined as follows:
展开示例
#calc.binom(10, 5)

n
The value of : The numbers of items to choose from. Must be non-negative.
k
The value of : The number of items to choose. Must be non-negative.
gcd0.3.0起可用
Calculates the greatest common divisor of two integers.
This will error if the result of integer division would be larger than the maximum 64-bit signed integer.
展开示例
#calc.gcd(7, 42)

a
The first integer.
b
The second integer.
lcm0.3.0起可用
Calculates the least common multiple of two integers.
展开示例
#calc.lcm(96, 13)

a
The first integer.
b
The second integer.
floor自始可用
Rounds a number down to the nearest integer.
If the number is already an integer, it is returned unchanged.
Note that this function will always return an integer, and will error if the resulting float or decimal is larger than the maximum 64-bit signed integer or smaller than the minimum for that type.
展开示例
#calc.floor(500.1)
#assert(calc.floor(3) == 3)
#assert(calc.floor(3.14) == 3)
#assert(calc.floor(decimal("-3.14")) == -4)

value
The number to round down.
ceil自始可用
Rounds a number up to the nearest integer.
If the number is already an integer, it is returned unchanged.
Note that this function will always return an integer, and will error if the resulting float or decimal is larger than the maximum 64-bit signed integer or smaller than the minimum for that type.
展开示例
#calc.ceil(500.1)
#assert(calc.ceil(3) == 3)
#assert(calc.ceil(3.14) == 4)
#assert(calc.ceil(decimal("-3.14")) == -3)

value
The number to round up.
trunc0.3.0起可用
Returns the integer part of a number.
If the number is already an integer, it is returned unchanged.
Note that this function will always return an integer, and will error if the resulting float or decimal is larger than the maximum 64-bit signed integer or smaller than the minimum for that type.
展开示例
#calc.trunc(15.9)
#assert(calc.trunc(3) == 3)
#assert(calc.trunc(-3.7) == -3)
#assert(calc.trunc(decimal("8493.12949582390")) == 8493)

value
The number to truncate.
fract0.3.0起可用
Returns the fractional part of a number.
If the number is an integer, returns 0.
展开示例
#calc.fract(-3.1)
#assert(calc.fract(3) == 0)
#assert(calc.fract(decimal("234.23949211")) == decimal("0.23949211"))

value
The number to truncate.
round自始可用
Rounds a number to the nearest integer.
Half-integers are rounded away from zero.
Optionally, a number of decimal places can be specified. If negative, its absolute value will indicate the amount of significant integer digits to remove before the decimal point.
Note that this function will return the same type as the operand. That is, applying round to a float will return a float, and to a decimal, another decimal. You may explicitly convert the output of this function to an integer with int, but note that such a conversion will error if the float or decimal is larger than the maximum 64-bit signed integer or smaller than the minimum integer.
In addition, this function can error if there is an attempt to round beyond the maximum or minimum integer or decimal. If the number is a float, such an attempt will cause float.inf or -float.inf to be returned for maximum and minimum respectively.
展开示例
#calc.round(3.1415, digits: 2)
#assert(calc.round(3) == 3)
#assert(calc.round(3.14) == 3)
#assert(calc.round(3.5) == 4.0)
#assert(calc.round(3333.45, digits: -2) == 3300.0)
#assert(calc.round(-48953.45, digits: -3) == -49000.0)
#assert(calc.round(3333, digits: -2) == 3300)
#assert(calc.round(-48953, digits: -3) == -49000)
#assert(calc.round(decimal("-6.5")) == decimal("-7"))
#assert(calc.round(decimal("7.123456789"), digits: 6) == decimal("7.123457"))
#assert(calc.round(decimal("3333.45"), digits: -2) == decimal("3300"))
#assert(calc.round(decimal("-48953.45"), digits: -3) == decimal("-49000"))

value
The number to round.
digits默认值:0
If positive, the number of decimal places.
If negative, the number of significant integer digits that should be removed before the decimal point.
clamp自始可用
Clamps a number between a minimum and maximum value.
展开示例
#calc.clamp(5, 0, 4)
#assert(calc.clamp(5, 0, 10) == 5)
#assert(calc.clamp(5, 6, 10) == 6)
#assert(calc.clamp(decimal("5.45"), 2, decimal("45.9")) == decimal("5.45"))
#assert(calc.clamp(decimal("5.45"), decimal("6.75"), 12) == decimal("6.75"))

value
The number to clamp.
min
The inclusive minimum value.
max
The inclusive maximum value.
min自始可用
Determines the minimum of a sequence of values.
展开示例
#calc.min(1, -3, -5, 20, 3, 6) \
#calc.min("typst", "is", "cool")

valuesany必需参数位置参数变长参数
The sequence of values from which to extract the minimum. Must not be empty.
max自始可用
Determines the maximum of a sequence of values.
展开示例
#calc.max(1, -3, -5, 20, 3, 6) \
#calc.max("typst", "is", "cool")

valuesany必需参数位置参数变长参数
The sequence of values from which to extract the maximum. Must not be empty.
even自始可用
Determines whether an integer is even.
展开示例
#calc.even(4) \
#calc.even(5) \
#range(10).filter(calc.even)

value
The number to check for evenness.
odd自始可用
Determines whether an integer is odd.
展开示例
#calc.odd(4) \
#calc.odd(5) \
#range(10).filter(calc.odd)

value
The number to check for oddness.
rem0.3.0起可用
Calculates the remainder of two numbers (signed modulo).
The value calc.rem(x, y) always has the same sign as x, and is smaller in magnitude than y.
This can error if given a decimal input and the dividend is too small in magnitude compared to the divisor.
展开示例
#calc.rem(7, 3) \
#calc.rem(7, -3) \
#calc.rem(-7, 3) \
#calc.rem(-7, -3) \
#calc.rem(1.75, 0.5)

dividend
The dividend of the remainder.
divisor
The divisor of the remainder.
div-euclid0.10.0起可用
Performs euclidean division of two numbers.
The result of this computation is that of a division rounded to the integer n such that the dividend is greater than or equal to n times the divisor.
This can error if the resulting number is larger than the maximum value or smaller than the minimum value for its type.
展开示例
#calc.div-euclid(7, 3) \
#calc.div-euclid(7, -3) \
#calc.div-euclid(-7, 3) \
#calc.div-euclid(-7, -3) \
#calc.div-euclid(1.75, 0.5) \
#calc.div-euclid(decimal("1.75"), decimal("0.5"))

dividend
The dividend of the division.
divisor
The divisor of the division.
rem-euclid0.10.0起可用
This calculates the least nonnegative remainder of a division (nonnegative modulo).
Warning: Due to a floating point round-off error, the remainder may equal the absolute value of the divisor if the dividend is much smaller in magnitude than the divisor and the dividend is negative. This only applies for floating point inputs.
In addition, this can error if given a decimal input and the dividend is too small in magnitude compared to the divisor.
展开示例
#calc.rem-euclid(7, 3) \
#calc.rem-euclid(7, -3) \
#calc.rem-euclid(-7, 3) \
#calc.rem-euclid(-7, -3) \
#calc.rem-euclid(1.75, 0.5) \
#calc.rem-euclid(decimal("1.75"), decimal("0.5"))

dividend
The dividend of the remainder.
divisor
The divisor of the remainder.
quo0.3.0起可用
Calculates the quotient (floored division) of two numbers.
Note that this function will always return an integer, and will error if the resulting number is larger than the maximum 64-bit signed integer or smaller than the minimum for that type.
展开示例
$ "quo"(a, b) &= floor(a/b) \
"quo"(14, 5) &= #calc.quo(14, 5) \
"quo"(3.46, 0.5) &= #calc.quo(3.46, 0.5) $

dividend
The dividend of the quotient.
divisor
The divisor of the quotient.
norm0.13.0起可用
Calculates the -norm of a sequence of values.
The -norm of is defined as follows:
展开示例
#calc.norm(1, 2, -3, 0.5) \
#calc.norm(p: 3, 1, 2)

p默认值:2.0
The value of . Must be greater than zero.
The default value of 2.0 corresponds to the Euclidean norm:
values
The sequence of values to calculate the -norm of. Returns 0.0 if empty.