Maths

Math functions

Methods

(static) average(…numbers) → {number}

Calculates a average/mean of all givens numbers

Parameters:
NameTypeAttributesDescription
numbersnumber<repeatable>

the numbers to average

Returns:
Type: 
number
Examples
average(1); // 1
average(1,2); // 1.5
average(5,4,3,6,7,3,1,8); // 4.625
average(1,2,-5,2.4,-6.5,0.5); // -0.9333333333333332
average(1,2,3,4,5,6,7,8,9); // 5
average(-1,2,3,4,5,6,7,8,-9); // 2.7777777777777777
average(...[1,2,3,4,5,6]); // 3.5

(static) clamp(value, minopt, maxopt) → {number}

If the value is greater than the maximum, returns the maximum. If the value is less than the minimum, returns the minimum. If not, return the passed value.

Parameters:
NameTypeAttributesDefaultDescription
valuenumber

The value to check

minnumber<optional>
0

Minimum value

maxnumber<optional>
1

Maximum value

Returns:

The fixed value

Type: 
number
Examples
clamp(-10, 0, 100); // 0
clamp(0, 0, 100); // 0
clamp(50, 0, 100); // 50
clamp(100, 0, 100); // 100
clamp(200, 0, 100); // 100

(static) degreesToRadians(d) → {number}

Return the givens value in radians

Parameters:
NameTypeDescription
dnumber

the degrees value to convert in radians

Returns:

converted the given degrees in radian

Type: 
number
Examples
degreesToRadians(0) // 0
degreesToRadians(90) // 1.5707963267948966
degreesToRadians(500) // 8.726646259971648

(static) divideEvenly(minopt, maxopt, numberDivisionsopt) → {Array.<number>}

Returns a array with the dived evenly between the two numbers

Parameters:
NameTypeAttributesDefaultDescription
minnumber<optional>
0

The minimum value to return.

maxnumber<optional>
10

The maximum value to return.

numberDivisionsnumber<optional>
5

The number of divisions to use.

Returns:

An array of evenly spaced numbers.

Type: 
Array.<number>
Examples
divideEvenly(); // [0, 2.5, 5, 7.5, 10]
divideEvenly(0, 10, 2); // [0, 10]
divideEvenly(-45, 45, 3); // [-45, 0, 45]

(static) division(…numbers) → {number}

Calculates a division of all givens numbers

Parameters:
NameTypeAttributesDescription
numbersnumber<repeatable>

the numbers to division

Returns:
Type: 
number
Examples
division(1); // 1
division(1,2); // 0.5
division(1000,10) // 100
division(20,10,5); // 0.4
division(1,2,-5,2.4,-6.5,0.5); // 0.012820512820512822

(static) factorial(number) → {number}

The factorial of a non-negative​ number is computed as the product of all integers between 1 and the number itself

Parameters:
NameTypeDescription
numbernumber

the number to factoring

Returns:

the result

Type: 
number
Examples
factorial(0); // 0
factorial(1); // 1
factorial(1.2); // 0.23999999999999994
factorial(4); // 24
factorial(6); // 720
factorial(10); // 3628800

(static) getPositionWithAngleDistance(angle, distance, originopt) → {Point}

Calculates de position (x,y) of a object based in the angle and distance

Parameters:
NameTypeAttributesDescription
anglenumber

the angle to evaluate

distancenumber

the distance to evaluate

originObject<optional>

origin position to analyze

Properties
NameTypeAttributesDefaultDescription
xnumber<optional>
0

x axis value

ynumber<optional>
0

y axis value

Returns:

position {x:number,y:number} the x and y position

Type: 
Point
Example
getPositionWithAngleDistance(10, 100); // { x: -83.90715290764524, y: -54.40211108893698 }

(static) invertedLerp(value, start, end) → {number}

Determines where a value lies between two points.

Parameters:
NameTypeDescription
valuenumber

The point within the range you want to calculate.

startnumber

The start of the range.

endnumber

The end of the range.

Returns:

value between 0 and 1, representing where the "value" parameter falls within the range defined by start and end

Type: 
number
Example
invertedLerp(-10, 0, 10); // 0
invertedLerp(0, 0, 10); // 0
invertedLerp(5, 0, 10); // 0.5
invertedLerp(10, 0, 10); // 1
invertedLerp(100, 0, 10); // 1

(static) isDecimal(number) → {boolean}

Checks if a number is a decimal by verifying if it has a fractional part.

Parameters:
NameTypeDescription
numbernumber

The number to check.

Returns:

Returns true if the number is a decimal, false otherwise.

Type: 
boolean
Example
isDecimal(1.5); // true
isDecimal(1); // false

(static) isEven(number) → {boolean}

Check is the given number is a EVEN number

Parameters:
NameTypeDescription
numbernumber

The number to check

Returns:
Type: 
boolean
Example
isEven(1); // false
isEven(2); // true

(static) isMultipleOf(dividend, divisor) → {boolean}

Check is a number is multiple of other

Parameters:
NameTypeDescription
dividendnumber

the number check is multiple

divisornumber

divisor number

Returns:
Type: 
boolean
Example
isMultipleOf(1, 2); // false
isMultipleOf(1, 1); // true
isMultipleOf(10, 2); // true
isMultipleOf(0, 2); // true
isMultipleOf(0, 0); // false
isMultipleOf(7, 21); // false
isMultipleOf(100, 21); // false
isMultipleOf(15, 5); // true
isMultipleOf(14, 5); // false

(static) isNarcissisticNumber(n) → {boolean}

A Narcissistic Number is a number of length l in which the sum of its digits to the power of l is equal to the original number. If this seems confusing, refer to the example below.

Ex: 153, where l = 3 ( the number of digits in 153 ) 13 + 53 + 33 = 153

Parameters:
NameTypeDescription
nnumber

number to check if a narcissistic

Returns:
Type: 
boolean
Example
isNarcissistic(153); // true
isNarcissistic(1); // true
isNarcissistic(435); // false
isNarcissistic(370); // true
isNarcissistic(324); // false
isNarcissistic(371); // true
isNarcissistic(4328); // false
isNarcissistic(407); // true
isNarcissistic(3248); // false

(static) isOdd(number) → {boolean}

Check is the given number is a ODD number

Parameters:
NameTypeDescription
numbernumber

The number to check

Returns:
Type: 
boolean
Example
isOdd(1); // true
isOdd(2); // false

(static) isValidNumber(number)

Check if the given value is a valid number

Parameters:
NameTypeDescription
numbernumber

number to validate

Returns:

a boolean

Example
isValidNumber(42);  // true
isValidNumber(0);   // true
isValidNumber(-1);  // true
isValidNumber(3.14);// true
isValidNumber(NaN); // false
isValidNumber(null);// false
isValidNumber();    // false
isValidNumber("42"); // false

(static) lerp(value, min, max) → {number}

Return the value between 2 values based on a given percentage (decimal midpoint) using linear interpolation.

Parameters:
NameTypeDescription
valuenumber

The decimal value used for interpolation

minnumber

The minimum value

maxnumber

The maximum value

See
Returns:

The result of the interpolation

Type: 
number
Example
lerp(0, 0, 100); // returns 0
lerp(0.5, 0, 100); // returns 50
lerp(1, 0, 100); // returns 100

(static) map(value, fromRangeStart, fromRangeEnd, toRangeStart, toRangeEnd) → {number}

Re-maps a number from one range to another

Parameters:
NameTypeDescription
valuenumber

The number to be re-mapped

fromRangeStartnumber

The start of the range the number is currently in

fromRangeEndnumber

The end of the range the number is currently in

toRangeStartnumber

The start of the range the number should be mapped to

toRangeEndnumber

The end of the range the number should be mapped to

Returns:
  • The re-mapped number
Type: 
number
Example
// map(value, fromRangeStart, fromRangeEnd, toRangeStart, toRangeEnd)
map(-10, 0, 100, 0, 1000) // returns -100
map(0, 0, 100, 0, 1000) // returns 0
map(10, 0, 100, 0, 1000) // returns 100
map(50, 0, 100, 0, 1000) // returns 500
map(1000, 0, 100, 0, 1000) // returns 10000

(static) median(…numbers) → {number}

Returns the median of the givens numbers

Parameters:
NameTypeAttributesDescription
numbersnumber<repeatable>

the numbers to get the median

Returns:
Type: 
number
Examples
median(1); // 1
median(1,2); // 1.5
median(1,2,3,4); // 2.5
median(1,2,3,4,5); // 3
median(...[1,2,3,4]); // 2.5

(static) mode(args) → {*}

Returns the most repeated element in an array

Parameters:
NameTypeDescription
argsArray

the elements to get the mode

Returns:
Type: 
*
Example
mode([1, 2, 2, 3, 4]); // 2
mode(["apple", "banana", "banana", "cherry"]); // "banana"

(static) multiplication(…numbers) → {number}

Calculates the product of all given numbers

Parameters:
NameTypeAttributesDescription
numbersnumber<repeatable>

The numbers to be multiplied

Returns:

The product of all given numbers

Type: 
number
Example
multiplication(1, 2); // returns 2
multiplication(1, 2, -5, 2.4, -6.5, 0.5); // returns 78
multiplication(1, 2, 3, 4, 5, 6, 7, 8, 9, 0); // returns 0
multiplication(-1, 2, 3, 4, 5, 6, 7, 8, -9); // returns 362880
multiplication(...[-1, 2, 3, 4, 5, 6, 7, 8, -9]); // returns 362880

(static) negative(number) → {number}

Converts the given number to its negative equivalent, unless it's already negative or zero.

Parameters:
NameTypeDescription
numbernumber

The number to be converted

Returns:
  • The negative equivalent of the input number, or 0 if the input was 0.
Type: 
number
Example
negative(10) // -10
negative(0) // 0
negative(-5) // -5

(static) percentage(value, total) → {number}

Calculates the percentage of a given value in relation to a total value

Parameters:
NameTypeDescription
valuenumber

The value to be calculated as a percentage

totalnumber

The total value to be used as reference

Returns:

The calculated percentage

Type: 
number
Example
percentage(10, 100); // returns 10
percentage(40, 40); // returns 100
percentage(40, 20); // returns 200

(static) radiansToDegrees(r) → {number}

Converts a given radian value to degrees

Parameters:
NameTypeDescription
rnumber

The radian value to be converted to degrees

Returns:

The converted radian value in degrees

Type: 
number
Example
radiansToDegrees(1.58) // returns: 90.52733163067008
radiansToDegrees(2.5) // returns: 143.2394487827058

(static) range(start, end, stepopt, skipopt) → {Array.<number>}

Returns an array of numbers between the start and end parameters, incrementing by the step parameter. Optionally, the values within the specified skip range can be skipped.

Parameters:
NameTypeAttributesDefaultDescription
startnumber

The starting point of the range

endnumber

The ending point of the range

stepnumber<optional>
1

The increment value

skipArray.<{start: number, end: number}><optional>
[]

The range of values to skip

Returns:

An array of numbers

Type: 
Array.<number>
Example
range(1, 5); // [1, 2, 3, 4, 5]
range(0, 100, 10); // [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
range(0, 100, 100) // [0, 100]
range(1, 100, 100) // [1]
range(0, 10, 1, [{start:2, end:8}]) // [0, 1, 9, 10]
range(0, 10, 1, [{start:2, end:4}, {start:7, end:8}]) // [0, 1, 5, 6, 9, 10]

(static) roundNumber(num, maxOfDecimalsopt) → {number}

Rounds a number to a specified maximum number of decimals.

Parameters:
NameTypeAttributesDefaultDescription
numnumber

The number to round.

maxOfDecimalsnumber<optional>
2

The maximum number of decimals to round to. Defaults to 2 if not provided.

Throws:
    • If the num parameter is not a number.
    Type
    TypeError
    • If the maxOfDecimals parameter is not a number.
    Type
    TypeError
    • If the maxOfDecimals parameter is less than 0
    Type
    RangeError
Returns:
  • The rounded number.
Type: 
number
Examples
roundNumber(10.5555); // 10.56
roundNumber(10.5555, 1); // 10.6
roundNumber(10); // 10

(static) subtraction(…numbers) → {number}

Calculates a subtraction of all givens numbers

Parameters:
NameTypeAttributesDescription
numbersnumber<repeatable>

the numbers to subtraction

Returns:
Type: 
number
Examples
subtraction(1); // 1
subtraction(1,2); // -1
subtraction(1,2,-5,2.4,-6.5,0.5); // 7.6
subtraction(1,2,3,4,5,6,7,8,9,0); // -43
subtraction(-1,2,3,4,5,6,7,8,-9); // -27
subtraction(...[-1,2,3,4,5,6,7,8,-9]); // -27

(static) sum(…numbers) → {number}

Calculates a sum of all givens numbers

Parameters:
NameTypeAttributesDescription
numbersnumber<repeatable>

the numbers to sum

Returns:
Type: 
number
Examples
sum(1,1); // 2
sum(1,2,-5,2.4,-6.5); // -6.1
sum(1,2,3,4,5,6,7,8,9,0); // 45
sum(-1,2,3,4,5,6,7,8,-9); // 25
sum(...[-1,2,3,4,5,6,7,8,-9]); // 25