Modules@robinpath/math
math

@robinpath/math

0.1.2Node.jsPublic

Math utilities: clamp, round, random, statistics, factorial, GCD, LCM, prime check, and linear interpolation

Math

Math utilities: clamp, round, random, statistics, factorial, GCD, LCM, prime check, and linear interpolation

Package: @robinpath/math | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the math module when you need to:

  • Clamp a number between a minimum and maximum value -- Use math.clamp to perform this operation
  • Round a number to N decimal places -- Use math.round to perform this operation
  • Generate a random integer between min and max (inclusive) -- Use math.randomInt to perform this operation
  • Generate a random float between min and max -- Use math.randomFloat to perform this operation
  • Calculate the sum of an array of numbers -- Use math.sum to perform this operation

Quick Reference

FunctionDescriptionReturns
clampClamp a number between a minimum and maximum valueClamped number
roundRound a number to N decimal placesRounded number
randomIntGenerate a random integer between min and max (inclusive)Random integer
randomFloatGenerate a random float between min and maxRandom float
sumCalculate the sum of an array of numbersSum of all numbers
avgCalculate the average of an array of numbersAverage value
medianCalculate the median of an array of numbersMedian value
minFind the minimum value in an arrayMinimum value
maxFind the maximum value in an arrayMaximum value
percentageCalculate what percentage a value is of a totalPercentage value
factorialCalculate the factorial of a numberFactorial result
gcdCalculate the greatest common divisor of two numbersGreatest common divisor
lcmCalculate the least common multiple of two numbersLeast common multiple
isPrimeCheck if a number is primeTrue if the number is prime
lerpLinear interpolation between two valuesInterpolated value

Functions

clamp

Clamp a number between a minimum and maximum value

Module: math | Returns: number -- Clamped number

math.clamp 15 0 10
ParameterTypeRequiredDescription
valuenumberYesThe number to clamp
minnumberYesMinimum value
maxnumberYesMaximum value

round

Round a number to N decimal places

Module: math | Returns: number -- Rounded number

math.round 3.14159 2
ParameterTypeRequiredDescription
valuenumberYesThe number to round
decimalsnumberNoNumber of decimal places (default: 0)

randomInt

Generate a random integer between min and max (inclusive)

Module: math | Returns: number -- Random integer

math.randomInt 1 100
ParameterTypeRequiredDescription
minnumberYesMinimum value (inclusive)
maxnumberYesMaximum value (inclusive)

randomFloat

Generate a random float between min and max

Module: math | Returns: number -- Random float

math.randomFloat 0 1
ParameterTypeRequiredDescription
minnumberYesMinimum value
maxnumberYesMaximum value

sum

Calculate the sum of an array of numbers

Module: math | Returns: number -- Sum of all numbers

math.sum [1, 2, 3, 4, 5]
ParameterTypeRequiredDescription
numbersarrayYesArray of numbers

avg

Calculate the average of an array of numbers

Module: math | Returns: number -- Average value

math.avg [10, 20, 30]
ParameterTypeRequiredDescription
numbersarrayYesArray of numbers

median

Calculate the median of an array of numbers

Module: math | Returns: number -- Median value

math.median [1, 3, 5, 7, 9]
ParameterTypeRequiredDescription
numbersarrayYesArray of numbers

min

Find the minimum value in an array

Module: math | Returns: number -- Minimum value

math.min [5, 2, 8, 1]
ParameterTypeRequiredDescription
numbersarrayYesArray of numbers

max

Find the maximum value in an array

Module: math | Returns: number -- Maximum value

math.max [5, 2, 8, 1]
ParameterTypeRequiredDescription
numbersarrayYesArray of numbers

percentage

Calculate what percentage a value is of a total

Module: math | Returns: number -- Percentage value

math.percentage 25 200
ParameterTypeRequiredDescription
valuenumberYesThe value
totalnumberYesThe total

factorial

Calculate the factorial of a number

Module: math | Returns: number -- Factorial result

math.factorial 5
ParameterTypeRequiredDescription
nnumberYesNon-negative integer

gcd

Calculate the greatest common divisor of two numbers

Module: math | Returns: number -- Greatest common divisor

math.gcd 12 8
ParameterTypeRequiredDescription
anumberYesFirst number
bnumberYesSecond number

lcm

Calculate the least common multiple of two numbers

Module: math | Returns: number -- Least common multiple

math.lcm 4 6
ParameterTypeRequiredDescription
anumberYesFirst number
bnumberYesSecond number

isPrime

Check if a number is prime

Module: math | Returns: boolean -- True if the number is prime

math.isPrime 17
ParameterTypeRequiredDescription
nnumberYesThe number to check

lerp

Linear interpolation between two values

Module: math | Returns: number -- Interpolated value

math.lerp 0 100 0.5
ParameterTypeRequiredDescription
startnumberYesStart value
endnumberYesEnd value
tnumberYesInterpolation factor (0-1)

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Factorial is not defined for negative numbersCheck the error message for details
@desc "Clamp and validate result"
do
  set $result as math.clamp 15 0 10
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Multi-step Math workflow

Chain multiple math operations together.

@desc "Clamp, round, and more"
do
  set $r_clamp as math.clamp 15 0 10
  set $r_round as math.round 3.14159 2
  set $r_randomInt as math.randomInt 1 100
  print "All operations complete"
enddo

2. Safe clamp with validation

Check results before proceeding.

@desc "Clamp and validate result"
do
  set $result as math.clamp 15 0 10
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.2latest1 months ago
Install
$ robinpath add @robinpath/math

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.2
LicenseMIT
Unpacked Size4.8 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

utilities