Math Macros¶
Built-in functions for mathematical operations, trigonometry, and calculations.
Loading Math Macros¶
Math macros are loaded by default in all questions. No additional require statement needed.
$result = sqrt(16); // Returns 4
$angle = sin(pi/2); // Returns 1
Arithmetic Functions¶
abs(x)¶
Absolute value.
$value = abs(-5); // Returns 5
$distance = abs($x1 - $x2);
round(x, decimals)¶
Round to specified decimal places.
$rounded = round(3.14159, 2); // Returns 3.14
$integer = round(3.14159, 0); // Returns 3
floor(x), ceil(x)¶
Round down or up to nearest integer.
$down = floor(3.7); // Returns 3
$up = ceil(3.2); // Returns 4
max(a, b, ...), min(a, b, ...)¶
Return maximum or minimum value.
$largest = max(5, 12, 3, 8); // Returns 12
$smallest = min(5, 12, 3, 8); // Returns 3
Exponential & Logarithmic¶
pow(base, exponent)¶
Raise to a power.
$squared = pow(5, 2); // Returns 25
$cube_root = pow(8, 1/3); // Returns 2
sqrt(x)¶
Square root.
$root = sqrt(16); // Returns 4
$root2 = sqrt(2); // Returns 1.414...
exp(x)¶
Euler's number (e) raised to power x.
$e_squared = exp(2); // Returns ~7.389
log(x), log10(x), ln(x)¶
Natural logarithm, base-10 logarithm, or natural log.
$natural = log(2.718); // Returns ~1
$base10 = log10(100); // Returns 2
$ln_value = ln(2.718); // Returns ~1
Trigonometric Functions¶
All angles are in radians. Convert from degrees using deg2rad().
sin(x), cos(x), tan(x)¶
Sine, cosine, tangent.
$angle_rad = deg2rad(45);
$sine = sin($angle_rad); // Returns ~0.707
$cosine = cos($angle_rad); // Returns ~0.707
$tangent = tan($angle_rad); // Returns 1
asin(x), acos(x), atan(x)¶
Inverse trigonometric functions (arcsine, arccosine, arctangent).
$angle_rad = asin(0.5); // Returns π/6 (30°)
$angle_deg = rad2deg($angle_rad); // Convert to degrees
deg2rad(degrees), rad2deg(radians)¶
Convert between degrees and radians.
$rad = deg2rad(90); // Returns π/2
$deg = rad2deg(pi/4); // Returns 45
Constants¶
pi¶
Mathematical constant π ≈ 3.14159...
$circumference = 2 * pi * $radius;
$area = pi * pow($radius, 2);
e¶
Euler's number ≈ 2.71828...
$value = exp(1); // Also equals e
$exponential = $a * pow(e, $b*$t); // Exponential decay/growth
Number Theory¶
gcd(a, b)¶
Greatest common divisor.
$divisor = gcd(12, 18); // Returns 6
lcm(a, b)¶
Least common multiple.
$multiple = lcm(4, 6); // Returns 12
factorial(n)¶
Factorial (n!).
$factorial = factorial(5); // Returns 120
mod(a, b) or $a % $b¶
Modulo (remainder after division).
$remainder = mod(17, 5); // Returns 2
$remainder = 17 % 5; // Also returns 2
Random Numbers¶
rand(min, max)¶
Random integer between min and max (inclusive).
$die_roll = rand(1, 6);
$coefficient = rand(-5, 5);
random(min, max)¶
Random decimal between min and max.
$decimal = random(0, 1);
$weight = random(2.5, 7.3);
Examples¶
Quadratic Formula¶
$a = 2;
$b = 7;
$c = 3;
$discriminant = pow($b, 2) - 4*$a*$c;
$x1 = (-$b + sqrt($discriminant)) / (2*$a);
$x2 = (-$b - sqrt($discriminant)) / (2*$a);
// $x1 = -0.5, $x2 = -3
Trigonometric Calculation¶
$angle_degrees = 60;
$angle_rad = deg2rad($angle_degrees);
$sine = sin($angle_rad); // ~0.866
$cosine = cos($angle_rad); // 0.5
$tangent = tan($angle_rad); // ~1.732
Distance Formula¶
$x1 = 3; $y1 = 4;
$x2 = 0; $y2 = 0;
$distance = sqrt(pow($x2 - $x1, 2) + pow($y2 - $y1, 2));
// Returns 5
Compound Interest¶
$principal = 1000;
$rate = 0.05; // 5% annual
$years = 3;
$n = 12; // Compounded monthly
$amount = $principal * pow(1 + $rate/$n, $n*$years);
// Returns ~1161.41
See Also¶
- String Macros — Text manipulation
- Array Macros — Array/list operations
- Format Macros — Number formatting and display
- Control Syntax — Variables and conditionals