AutolangDocs
std/math

Math

The std/math module provides mathematical computing functions, rounding, boundary clamping, power and square root operations, and mathematical constants. Autolang supports both global helper functions and qualified Math namespace access.

Constants & Rounding Operations

Core mathematical constants PI and E can be accessed globally or via Math.PI and Math.E. Rounding methods support both integer and floating-point outputs.

@import("std/math") // 1. Mathematical constants println("PI: " + PI) println("E: " + Math.E) // 2. Arithmetic rounding println("Round 3.6: " + round(3.6)) // 4 println("Floor 3.8: " + floor(3.8)) // 3.0 println("Ceil 3.2: " + ceil(3.2)) // 4.0 // 3. Direct integer rounding val intVal = roundToInt(4.7) println("RoundToInt 4.7: " + intVal)

Extrema & Boundary Clamping

Evaluate maximum and minimum values, calculate absolute amounts, and clamp numbers within safe business boundaries using clamp() to prevent out-of-range arithmetic errors.

@import("std/math") // 1. Min, max, absolute and sign println("Min(3, 5): " + min(3, 5)) // 3 println("Max(10, 20): " + max(10, 20)) // 20 println("Abs(-10): " + abs(-10)) // 10 println("Sign(-50): " + Math.sign(-50)) // -1 println("Sign(50.0): " + Math.sign(50.0)) // 1.0 // 2. Extrema over argument collections val lowest = minOf(150, 45, 200) // 45 val highest = maxOf(10.5, 42.0, 31.8) // 42.0 println("Lowest: " + lowest) // 45 println("Highest: " + highest) // 42.0 // 3. Clamp value within bounds: clamp(val, min, max) val requestedDiscount = 35 val appliedDiscount = clamp(requestedDiscount, 5, 25) println("Applied discount (clamped): " + appliedDiscount)

Powers, Roots & Random Generation

Compute powers with pow(), square roots with sqrt(), and generate pseudo-random numbers in defined ranges.

@import("std/math") // 1. Powers and square roots println("2^3: " + pow(2, 3)) // 8 println("4.0^0.5: " + pow(4.0, 0.5)) // 2.0 println("sqrt(16.0): " + sqrt(16.0)) // 4.0 // 2. Random generation val randomRate = Math.random() // Float between 0.0 .. 1.0 val randomId = Math.random(1000, 9999) // Integer within range println("Random rate: " + randomRate) println("Random ID: " + randomId)

Function Reference

Mathematical Constants

  • PI / Math.PI: Mathematical constant Pi (approx. 3.14159...).
  • E / Math.E: Euler's number (approx. 2.71828...).

Arithmetic & Rounding Functions

  • abs(x) / Math.abs(x): Absolute value of integer or float.
  • min(a, b) / Math.min(a, b): Returns the smaller of two numbers.
  • max(a, b) / Math.max(a, b): Returns the larger of two numbers.
  • minOf(...) / maxOf(...): Finds minimum or maximum value among argument list.
  • clamp(value, min, max): Clamps value to fall within [min, max].
  • Math.sign(x): Returns sign of number (-1, 0, 1).
  • floor(x) / Math.floor(x): Rounds down to nearest integer.
  • ceil(x) / Math.ceil(x): Rounds up to nearest integer.
  • round(x) / Math.round(x): Rounds to nearest integer by arithmetic rule.
  • roundToInt(x: Float): Int: Rounds floating-point number directly to Int.
  • pow(base, exp) / Math.pow(base, exp): Computes base raised to power of exp.
  • sqrt(x) / Math.sqrt(x): Computes square root of x.
  • Math.random(): Float: Generates random float in range [0.0, 1.0).
  • Math.random(min: Int, max: Int): Int: Generates random integer in range [min, max].