Constants
The module provides high-precision mathematical constants (42 digits):Configuration
MATH_SCALE - Sets decimal precision for floating-point operations (default: 10)BC wrapper
math::has_bc()
Check ifbc is available for floating-point operations.
Returns: 0 if available, 1 otherwise
math::bc(expr, [scale])
Execute abc expression with specified precision.
Parameters:
expr- BC expression to evaluatescale- Decimal places (optional, defaults to MATH_SCALE)
Basic integer arithmetic
Pure bash implementations requiring no external dependencies.math::abs(n)
math::abs(n)
n- Integer value
math::min(a, b)
math::min(a, b)
a- First valueb- Second value
math::max(a, b)
math::max(a, b)
a- First valueb- Second value
math::clamp(n, min, max)
math::clamp(n, min, max)
n- Value to clampmin- Minimum boundmax- Maximum bound
math::clampf(n, min, max, [scale])
math::clampf(n, min, max, [scale])
n- Value to clampmin- Minimum boundmax- Maximum boundscale- Decimal places (optional)
math::div(dividend, divisor)
math::div(dividend, divisor)
dividend- Number to dividedivisor- Number to divide by
math::mod(dividend, divisor)
math::mod(dividend, divisor)
dividend- Number to dividedivisor- Modulus
math::pow(base, exponent)
math::pow(base, exponent)
base- Base numberexponent- Power to raise to
math::sum(n1, n2, ...)
math::sum(n1, n2, ...)
- Variable number of integers
math::product(n1, n2, ...)
math::product(n1, n2, ...)
- Variable number of integers
Floating-point operations
Requiresbc for calculations. All functions support optional scale parameter.
math::floor(n)
math::floor(n)
n- Number to floor
math::ceil(n)
math::ceil(n)
n- Number to ceiling
math::round(n, [decimal_places])
math::round(n, [decimal_places])
n- Number to rounddecimal_places- Precision (optional, default 0)
math::sqrt(n, [scale])
math::sqrt(n, [scale])
n- Numberscale- Decimal places (optional)
math::log(n)
math::log(n)
n- Number
math::log2(n)
math::log2(n)
n- Number
math::log10(n)
math::log10(n)
n- Number
math::logn(value, base)
math::logn(value, base)
value- Numberbase- Logarithm base
math::exp(n)
math::exp(n)
n- Exponent
math::powf(base, exponent)
math::powf(base, exponent)
base- Base numberexponent- Power (can be fractional)
Trigonometry
All angles in radians unless noted. Requiresbc.
math::sin(radians)
math::sin(radians)
math::cos(radians)
math::cos(radians)
math::tan(radians)
math::tan(radians)
math::asin(value)
math::asin(value)
value- Value in range [-1, 1]
math::acos(value)
math::acos(value)
value- Value in range [-1, 1]
math::atan(value)
math::atan(value)
value- Any number
math::atan2(y, x)
math::atan2(y, x)
y- Y coordinatex- X coordinate
math::deg_to_rad(degrees)
math::deg_to_rad(degrees)
math::rad_to_deg(radians)
math::rad_to_deg(radians)
Percentage and ratio functions
math::percent(part, total, [scale])
math::percent(part, total, [scale])
part- Partial valuetotal- Total valuescale- Decimal places (optional, default 2)
math::percent_of(percent, total, [scale])
math::percent_of(percent, total, [scale])
percent- Percentagetotal- Total valuescale- Decimal places (optional)
math::percent_change(old, new, [scale])
math::percent_change(old, new, [scale])
old- Original valuenew- New valuescale- Decimal places (optional)
Interpolation and mapping
math::lerp(a, b, t, [scale])
math::lerp(a, b, t, [scale])
a- Start valueb- End valuet- Interpolation factor (clamped to 0-1)scale- Decimal places (optional)
math::lerp_unclamped(a, b, t, [scale])
math::lerp_unclamped(a, b, t, [scale])
a- Start valueb- End valuet- Interpolation factor (not clamped)scale- Decimal places (optional)
math::map(value, in_min, in_max, out_min, out_max, [scale])
math::map(value, in_min, in_max, out_min, out_max, [scale])
value- Input valuein_min- Input range minimumin_max- Input range maximumout_min- Output range minimumout_max- Output range maximumscale- Decimal places (optional)
math::normalize(value, min, max, [scale])
math::normalize(value, min, max, [scale])
value- Input valuemin- Range minimummax- Range maximumscale- Decimal places (optional)
Number theory and combinatorics
math::gcd(a, b)
math::gcd(a, b)
a- First integerb- Second integer
math::lcm(a, b)
math::lcm(a, b)
a- First integerb- Second integer
math::is_even(n)
math::is_even(n)
math::is_odd(n)
math::is_odd(n)
math::is_prime(n)
math::is_prime(n)
n- Integer to check
math::factorial(n)
math::factorial(n)
n- Non-negative integer
math::fibonacci(n)
math::fibonacci(n)
n- Index (0-based)
math::int_sqrt(n)
math::int_sqrt(n)
n- Non-negative integer
math::choose(n, k)
math::choose(n, k)
n- Total itemsk- Items to choose
math::permute(n, k)
math::permute(n, k)
n- Total itemsk- Items to arrange
math::digit_sum(n)
math::digit_sum(n)
n- Integer (sign ignored)
math::digit_count(n)
math::digit_count(n)
n- Integer (sign ignored)
math::digit_reverse(n)
math::digit_reverse(n)
n- Integer
math::is_palindrome(n)
math::is_palindrome(n)
n- Integer (sign ignored)
Unit conversion
math::unitconvert(from, to, value, [scale])
Universal unit conversion dispatcher supporting temperature, length, mass, volume, speed, pressure, energy, power, digital storage, time, and angles. Parameters:from- Source unit (case-insensitive)to- Target unit (case-insensitive)value- Value to convertscale- Decimal places (optional)
Supported conversions
Temperature
Temperature
Length
Length
Mass
Mass
Volume
Volume
Speed
Speed
Pressure
Pressure
Energy
Energy
Power
Power
Digital storage
Digital storage
Time
Time
Angle
Angle