Typst Fumadocs

Calculation

Documentation for the calc functions.

Module for calculations and processing of numeric values.

These definitions are part of the calc module and not imported by default. In addition to the functions listed below, the calc module also defines the constants pi, tau, e, and inf.

Calculates the absolute value of a numeric value.

Loading compiler...
#calc.abs(
  value
) -> any

Parameters

Prop

Type

Raises a value to some exponent.

Loading compiler...
#calc.pow(
  base,
  exponent
) -> int float decimal

Parameters

Prop

Type

Raises a value to some exponent of e.

Loading compiler...
#calc.exp(
  exponent
) -> float

Parameters

Prop

Type

Calculates the square root of a number.

Loading compiler...
#calc.sqrt(
  value
) -> float

Parameters

Prop

Type

Calculates the real nth root of a number.

If the number is negative, then n must be odd.

Loading compiler...
#calc.root(
  radicand,
  index
) -> float

Parameters

Prop

Type

Calculates the sine of an angle.

When called with an integer or a float, they will be interpreted as radians.

Loading compiler...
#calc.sin(
  angle
) -> float

Parameters

Prop

Type

Calculates the cosine of an angle.

When called with an integer or a float, they will be interpreted as radians.

Loading compiler...
#calc.cos(
  angle
) -> float

Parameters

Prop

Type

Calculates the tangent of an angle.

When called with an integer or a float, they will be interpreted as radians.

Loading compiler...
#calc.tan(
  angle
) -> float

Parameters

Prop

Type

Calculates the arcsine of a number.

Loading compiler...
#calc.asin(
  value
) -> angle

Parameters

Prop

Type

Calculates the arccosine of a number.

Loading compiler...
#calc.acos(
  value
) -> angle

Parameters

Prop

Type

Calculates the arctangent of a number.

Loading compiler...
#calc.atan(
  value
) -> angle

Parameters

Prop

Type

Calculates the four-quadrant arctangent of a coordinate.

The arguments are (x, y), not (y, x).

Loading compiler...
#calc.atan2(
  x,
  y
) -> angle

Parameters

Prop

Type

Calculates the hyperbolic sine of a hyperbolic angle.

Loading compiler...
#calc.sinh(
  value
) -> float

Parameters

Prop

Type

Calculates the hyperbolic cosine of a hyperbolic angle.

Loading compiler...
#calc.cosh(
  value
) -> float

Parameters

Prop

Type

Calculates the hyperbolic tangent of a hyperbolic angle.

Loading compiler...
#calc.tanh(
  value
) -> float

Parameters

Prop

Type

Calculates the logarithm of a number.

If the base is not specified, the logarithm is calculated in base 10.

Loading compiler...
#calc.log(
  value,
  base: float
) -> float

Parameters

Prop

Type

Calculates the natural logarithm of a number.

Loading compiler...
#calc.ln(
  value
) -> float

Parameters

Prop

Type

Calculates the factorial of a number.

Loading compiler...
#calc.fact(
  number
) -> int

Parameters

Prop

Type

Calculates a permutation.

Returns the k-permutation of n, or the number of ways to choose k items from a set of n with regard to order.

Loading compiler...
#calc.perm(
  base,
  numbers
) -> int

Parameters

Prop

Type

Calculates a binomial coefficient.

Returns the k-combination of n, or the number of ways to choose k items from a set of n without regard to order.

Loading compiler...
#calc.binom(
  n,
  k
) -> int

Parameters

Prop

Type

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.

Loading compiler...
#calc.gcd(
  a,
  b
) -> int

Parameters

Prop

Type

Calculates the least common multiple of two integers.

Loading compiler...
#calc.lcm(
  a,
  b
) -> int

Parameters

Prop

Type

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.

Loading compiler...
#calc.floor(
  value
) -> int

Parameters

Prop

Type

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.

Loading compiler...
#calc.ceil(
  value
) -> int

Parameters

Prop

Type

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.

Loading compiler...
#calc.trunc(
  value
) -> int

Parameters

Prop

Type

Returns the fractional part of a number.

If the number is an integer, returns 0.

Loading compiler...
#calc.fract(
  value
) -> int float decimal

Parameters

Prop

Type

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.

Loading compiler...
#calc.round(
  value,
  digits: int
) -> int float decimal

Parameters

Prop

Type

Clamps a number between a minimum and maximum value.

Loading compiler...
#calc.clamp(
  value,
  min,
  max
) -> int float decimal

Parameters

Prop

Type

Determines the minimum of a sequence of values.

Loading compiler...
#calc.min(
  values
) -> any

Parameters

Prop

Type

Determines the maximum of a sequence of values.

Loading compiler...
#calc.max(
  values
) -> any

Parameters

Prop

Type

Determines whether an integer is even.

Loading compiler...
#calc.even(
  value
) -> bool

Parameters

Prop

Type

Determines whether an integer is odd.

Loading compiler...
#calc.odd(
  value
) -> bool

Parameters

Prop

Type

Calculates the remainder of two numbers.

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.

Loading compiler...
#calc.rem(
  dividend,
  divisor
) -> int float decimal

Parameters

Prop

Type

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.

Loading compiler...
#calc.div-euclid(
  dividend,
  divisor
) -> int float decimal

Parameters

Prop

Type

This calculates the least nonnegative remainder of a division.

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.

Loading compiler...
#calc.rem-euclid(
  dividend,
  divisor
) -> int float decimal

Parameters

Prop

Type

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.

Loading compiler...
#calc.quo(
  dividend,
  divisor
) -> int

Parameters

Prop

Type

Calculates the p-norm of a sequence of values.

Loading compiler...
#calc.norm(
  p: float,
  values
) -> float

Parameters

Prop

Type

On this page