AutolangDocs
std/math

Math

The Math class provides a comprehensive set of static methods and constants for mathematical calculations. Thanks to method overloading, many of these functions seamlessly accept both Int and Float types. You must import this module using @import("std/math").

Constants & Basic Operations

The Math class includes standard constants like PI and E. It also provides basic utilities for absolute values, minimum/maximum comparisons, and rounding operations. Note that rounding functions always return an Int.

@import("std/math") println("Constant PI: ${Math.PI}") println("Constant E: ${Math.E}") // Absolute values println("Abs(-15): ${Math.abs(-15)}") println("Abs(-4.2): ${Math.abs(-4.2)}") // Min / Max println("Max(10, 20): ${Math.max(10, 20)}") println("Min(5.5, 3.2): ${Math.min(5.5, 3.2)}") // Rounding println("Round(3.6): ${Math.round(3.6)}") // 4 println("Floor(3.8): ${Math.floor(3.8)}") // 3 println("Ceil(3.1): ${Math.ceil(3.1)}") // 4

Powers, Roots & Trigonometry

You can easily perform exponentiation, calculate square roots, and use trigonometric functions. Trigonometric functions expect their arguments in radians.

@import("std/math") // Powers and Roots println("3 squared: ${Math.pow(3, 2)}") println("Square root of 16: ${Math.sqrt(16)}") println("Square root of 2: ${Math.sqrt(2.0)}") // Trigonometry val angle = Math.PI / 2.0 println("Sin(PI/2): ${Math.sin(angle)}") println("Cos(0): ${Math.cos(0)}")

Random Generation

Generating random numbers is straightforward. You can generate a random float between 0 and 1, or specify a precise range for either integers or floats.

@import("std/math") // Float between 0.0 and 1.0 val percent = Math.random() println("Random percent: ${percent}") // Random Integer in range (inclusive) val diceRoll = Math.random(1, 6) println("You rolled a ${diceRoll}!") // Random Float in range val temp = Math.random(36.0, 38.0) println("Body temperature: ${temp}")

Reference

Constants

  • Math.PI: Float - The ratio of a circle's circumference to its diameter (approx. 3.14159).
  • Math.E: Float - The base of the natural logarithm (approx. 2.71828).

Basic Operations

  • Math.round(value: Float): Int - Rounds to the nearest integer.
  • Math.floor(value: Float): Int - Rounds down to the nearest integer.
  • Math.ceil(value: Float): Int - Rounds up to the nearest integer.
  • Math.trunc(value: Float): Int - Truncates the decimal part, returning the integer part.
  • Math.abs(value: Int|Float) - Returns the absolute (positive) value.
  • Math.min(a, b) - Returns the smaller of two values (supports Int and Float).
  • Math.max(a, b) - Returns the larger of two values (supports Int and Float).
  • Math.fmod(num1: Float, num2: Float): Float - Returns the floating-point remainder of num1 / num2.

Powers & Roots

  • Math.pow(base: Int|Float, exp: Int|Float) - Returns the base raised to the power of the exponent.
  • Math.sqrt(value: Int|Float): Float - Returns the square root of the value.
  • Math.exp(value: Float): Float - Returns Euler's number e raised to the power of the value.
  • Math.log(value: Float): Float - Returns the natural logarithm (base e) of the value.

Trigonometry (Angles in Radians)

  • Math.sin(value: Int|Float): Float - Returns the sine of the angle.
  • Math.cos(value: Int|Float): Float - Returns the cosine of the angle.
  • Math.tan(value: Int|Float): Float - Returns the tangent of the angle.

Random Generation

  • Math.random(): Float - Returns a pseudo-random float between 0.0 and 1.0.
  • Math.random(min: Int, max: Int): Int - Returns a random integer between min and max (inclusive).
  • Math.random(min: Float, max: Float): Float - Returns a random float between min and max.