math

Mathematical functions and constants.

Table of contents

  1. Overview
  2. Constants
  3. Basic Functions
    1. math.abs(x)
    2. math.floor(x) / math.ceil(x) / math.round(x)
    3. math.min(x, y) / math.max(x, y)
    4. math.sqrt(x)
    5. math.pow(x, y) / math.exp(x)
    6. math.log(x) / math.log10(x) / math.log2(x)
  4. Trigonometric Functions
    1. Basic Trigonometry
    2. Inverse Trigonometry
  5. Hyperbolic Functions
    1. Inverse Hyperbolic
  6. Advanced Functions
    1. math.fmod(x, y)
    2. math.modf(x)
    3. math.hypot(x, y)
    4. math.ldexp(m, e)
    5. math.frexp(x)
  7. Type Checking
    1. math.is_nan(x)
    2. math.is_inf(x)
    3. math.is_finite(x)
  8. Example Usage

Overview

The math module provides mathematical functions and constants. It must be explicitly imported:

const math = import("math");
print(math.PI);

Constants

Constant Value Description
math.PI 3.14159… π (pi)
math.E 2.71828… e (Euler’s number)

Basic Functions

math.abs(x)

Absolute value.

print(math.abs(-5));   // 5
print(math.abs(3.14)); // 3.14

math.floor(x) / math.ceil(x) / math.round(x)

Rounding functions.

print(math.floor(3.7));  // 3
print(math.ceil(3.2));   // 4
print(math.round(3.5));  // 4

math.min(x, y) / math.max(x, y)

Return minimum or maximum of two numbers.

print(math.min(5, 10));  // 5
print(math.max(5, 10));  // 10

math.sqrt(x)

Square root.

print(math.sqrt(16));  // 4
print(math.sqrt(2));   // 1.41421...

math.pow(x, y) / math.exp(x)

Power and exponential.

print(math.pow(2, 8));   // 256
print(math.exp(1));      // 2.71828... (e^1)

math.log(x) / math.log10(x) / math.log2(x)

Logarithms.

print(math.log(math.E));  // 1 (natural log)
print(math.log10(100));      // 2
print(math.log2(8));         // 3

Trigonometric Functions

Basic Trigonometry

print(math.sin(math.PI / 2));   // 1
print(math.cos(0));                 // 1
print(math.tan(math.PI / 4));   // 1

Inverse Trigonometry

print(math.asin(1));      // π/2
print(math.acos(0));      // π/2
print(math.atan(1));      // π/4
print(math.atan2(y, x));  // atan(y/x) with proper quadrant

Hyperbolic Functions

print(math.sinh(x));
print(math.cosh(x));
print(math.tanh(x));

Inverse Hyperbolic

print(math.asinh(x));
print(math.acosh(x));
print(math.atanh(x));

Advanced Functions

math.fmod(x, y)

Floating-point modulo.

print(math.fmod(7.5, 2.0));  // 1.5

math.modf(x)

Split number into integer and fractional parts.

let int_part, frac_part = math.modf(3.14);
// int_part = 3, frac_part = 0.14

math.hypot(x, y)

Euclidean distance: sqrt(x² + y²).

print(math.hypot(3, 4));  // 5

math.ldexp(m, e)

Multiply by power of 2: m × 2^e.

print(math.ldexp(0.5, 3));  // 0.5 * 2^3 = 4

math.frexp(x)

Extract mantissa and exponent.

let mantissa, exp = math.frexp(8);
// mantissa = 0.5, exp = 4 (since 8 = 0.5 * 2^4)

Type Checking

math.is_nan(x)

Check if value is NaN (Not a Number).

let nan = 0 / 0;
print(math.is_nan(nan));  // true

math.is_inf(x)

Check if value is infinite.

print(math.is_inf(1 / 0));  // true

math.is_finite(x)

Check if value is finite (not NaN or infinity).

print(math.is_finite(42));  // true

Example Usage

const math = import("math");

// Calculate circle area
let radius = 5;
let area = math.PI * math.pow(radius, 2);
print("Area: " + tostring(area));

// Distance between two points
let dx = 3;
let dy = 4;
let distance = math.hypot(dx, dy);
print("Distance: " + tostring(distance));  // 5

// Angle calculation
let angle = math.atan2(dy, dx);
print("Angle: " + tostring(angle) + " radians");

Copyright © 2025 behl Project. Distributed under MIT License.

This site uses Just the Docs, a documentation theme for Jekyll.