This module contains mathematical constants and trigonometric functions written in kyro.

Include using

var math = use("lib:math");

PI

PI

The mathematical constant $\pi$ (pi), representing the ratio of the circumference of a circle to its diameter.

  • Value: 3.141592653589793

TWO_PI

TWO_PI

The mathematical constant $2\pi$ (tau), representing a full turn in radians.

  • Value: 6.283185307179586

sum

sum(args = [])

Calculates the sum of all numerical elements in a list.

  • Parameters:
    • args (List, optional): A list of numbers to be summed. Defaults to an empty list [].
  • Returns: (Number) The total sum of the elements.
var math = use("lib:math");

var values = [1, 2, 3, 4, 5];
echo math.sum(values); // 15

// Calling sum with no arguments defaults to an empty list
echo math.sum();       // 0

sin

sin(x = 0)

Calculates the sine of an angle $x$ (expressed in radians) using a Taylor series approximation.

  • Parameters:
    • x (Number, optional): The angle in radians. Defaults to 0.
  • Returns: (Number) The sine of $x$.
var math = use("lib:math");

echo math.sin(math.PI / 2.0); // 1
echo math.sin();              // 0

cos

cos(x = 0)

Calculates the cosine of an angle $x$ (expressed in radians) using a Taylor series approximation.

  • Parameters:
    • x (Number, optional): The angle in radians. Defaults to 0.
  • Returns: (Number) The cosine of $x$.
var math = use("lib:math");

echo math.cos(math.PI); // -1
echo math.cos();        // 1

tan

tan(x = 0)

Calculates the tangent of an angle $x$ (expressed in radians).

  • Parameters:
    • x (Number, optional): The angle in radians. Defaults to 0.
  • Returns: (Number) The tangent of $x$.
var math = use("lib:math");

echo math.tan(0.0); // 0
echo math.tan();    // 0

min

min(a, b)

Returns the smaller of two numbers.

  • Parameters:
    • a (Number): The first number.
    • b (Number): The second number.
  • Returns: (Number) The smaller value.
var math = use("lib:math");

echo math.min(10.0, 20.0); // 10

max

max(a, b)

Returns the larger of two numbers.

  • Parameters:
    • a (Number): The first number.
    • b (Number): The second number.
  • Returns: (Number) The larger value.
var math = use("lib:math");

echo math.max(10.0, 20.0); // 20

deg_to_rad

deg_to_rad(deg)

Converts an angle from degrees to radians.

  • Parameters:
    • deg (Number): The angle in degrees.
  • Returns: (Number) The angle in radians.
var math = use("lib:math");

echo math.deg_to_rad(180.0); // 3.141592653589793 (math.PI)

rad_to_deg

rad_to_deg(rad)

Converts an angle from radians to degrees.

  • Parameters:
    • rad (Number): The angle in radians.
  • Returns: (Number) The angle in degrees.
var math = use("lib:math");

echo math.rad_to_deg(math.PI); // 180

sqrt

sqrt(x)

Calculates the square root of a non-negative number.

  • Parameters:
    • x (Number): The input number. Must be greater than or equal to 0.
  • Returns: (Number) The calculated square root.
var math = use("lib:math");

echo math.sqrt(16.0); // 4

pow

pow(base, exp)

Calculates a base raised to an integer exponent power.

  • Parameters:
    • base (Number): The base value.
    • exp (Number): The integer exponent.
  • Returns: (Number) The result of $base^{exp}$.
var math = use("lib:math");

echo math.pow(2.0, 3.0);  // 8
echo math.pow(5.0, -1.0); // 0.2