Skip to content

jcore math

Julius Paffrath edited this page Jul 6, 2026 · 1 revision

Introduction

The math module offers functions for often used math functions.

max(n1: number, n2: number)

Returns the bigger number from two given numbers.

min(n1: number, n2: number)

Returns the smaller number from two given numbers.

absolute(n: number)

Returns absolute (positive) value of a given number representing its distance from zero.

power(base: number, exponent: number)

Returns the power of a base raised to an exponent using the efficient Binary Exponentiation (Square-and-Multiply) algorithm in O(log n) time.

root(x: number, n: number)

Returns the approximated n-th root of a radicand (x) using Newton's Method. Returns nil by

  • zero root (n == 0)
  • an even root of a negative number
  • a division by zero

This algorithm uses floating-point numbers, which inherently cause small precision errors due to the binary representation of decimal fractions under IEEE 754 standard. If you check the results of this, you are advides to use an approximation.

squareroot(x: number)

Returns the square root (2nd root) of a non-negative number using the internal implementation of root(x: number, n: number).

fibonacci(n: number)

Returns the calculated fibonacci number using a simple loop approach, which is not optimized (yet).

isPrime(n: number)

Returns true if a given number is a prime number. Implements an optimized implementation using wheel factorization.

Clone this wiki locally