Skip to main content

Math

The Math module provides comprehensive mathematical operations, constants, and utility functions for Soul. It includes basic arithmetic operations, trigonometric functions, logarithmic functions, statistical operations, and random number generation capabilities.

Mathematical Constants

The Math module provides commonly used mathematical constants:

PI - Circle constant π

The ratio of a circle’s circumference to its diameter:

E - Euler’s number

The base of natural logarithms:

TAU - Circle constant τ

The ratio of a circle’s circumference to its radius (2π):

EPSILON - Machine epsilon

The smallest representable positive number:

Basic Mathematical Operations

abs - Absolute value

Returns the absolute value of a number:

sign - Sign of number

Returns the sign of a number (-1, 0, or 1):

max - Maximum value

Returns the largest of two or more numbers:

min - Minimum value

Returns the smallest of two or more numbers:

clamp - Clamp value between bounds

Restricts a value to be within a specified range:

Rounding Operations

ceil - Round up

Returns the smallest integer greater than or equal to a number:

floor - Round down

Returns the largest integer less than or equal to a number:

round - Round to nearest integer

Returns the nearest integer, rounding half away from zero:

Power and Root Operations

pow - Power function

Returns base raised to the power of exponent:

sqrt - Square root

Returns the square root of a number:

Logarithmic and Exponential Functions

log - Natural logarithm

Returns the natural logarithm (base e) of a number:

exp - Exponential function

Returns e raised to the power of a number:

Trigonometric Functions

sin - Sine function

Returns the sine of an angle (in radians):

cos - Cosine function

Returns the cosine of an angle (in radians):

tan - Tangent function

Returns the tangent of an angle (in radians):

Modulo Operations

mod - Modulo operation

Returns the floating-point remainder of x divided by y:

Number Testing Functions

isPositive - Check if positive

Tests if a number is positive:

isNegative - Check if negative

Tests if a number is negative:

isZero - Check if zero

Tests if a number is zero:

Random Number Generation

random - Generate random number

Generates a random number between 0 and 1 (exclusive):

seed - Set random seed

Sets the seed for the random number generator:

Practical Examples

Distance Calculation

Angle Conversions

Statistical Calculations

Geometric Calculations

Random Number Utilities

Trigonometric Applications

Financial Calculations

Best Practices

  1. Handle edge cases: Always check for invalid inputs (negative numbers for sqrt, zero for division)
  2. Use appropriate precision: Be aware of floating-point precision limitations
  3. Choose the right function: Use appropriate mathematical functions for your use case
  4. Angle units: Remember that trigonometric functions use radians, not degrees
  5. Random seeding: Set seeds for reproducible random sequences when needed
The Math module provides a comprehensive set of mathematical functions and constants for Soul, enabling complex mathematical computations, statistical analysis, and geometric calculations with precision and reliability.