Math functions
- Source
Methods
(static) average(…numbers) → {number}
Calculates a average/mean of all givens numbers
Name | Type | Attributes | Description |
---|---|---|---|
numbers | number | <repeatable> | the numbers to average |
- Source
- Type:
- number
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.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
value | number | The value to check | ||
min | number | <optional> | 0 | Minimum value |
max | number | <optional> | 1 | Maximum value |
- Source
The fixed value
- Type:
- number
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
Name | Type | Description |
---|---|---|
d | number | the degrees value to convert in radians |
- Source
converted the given degrees in radian
- Type:
- number
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
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
min | number | <optional> | 0 | The minimum value to return. |
max | number | <optional> | 10 | The maximum value to return. |
numberDivisions | number | <optional> | 5 | The number of divisions to use. |
- Source
An array of evenly spaced numbers.
- Type:
- Array.<number>
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
Name | Type | Attributes | Description |
---|---|---|---|
numbers | number | <repeatable> | the numbers to division |
- Source
- Type:
- number
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
Name | Type | Description |
---|---|---|
number | number | the number to factoring |
- Source
the result
- Type:
- number
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
Name | Type | Attributes | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
angle | number | the angle to evaluate | ||||||||||||||||
distance | number | the distance to evaluate | ||||||||||||||||
origin | Object | <optional> | origin position to analyze Properties
|
position {x:number,y:number} the x and y position
- Type:
- Point
getPositionWithAngleDistance(10, 100); // { x: -83.90715290764524, y: -54.40211108893698 }
(static) invertedLerp(value, start, end) → {number}
Determines where a value lies between two points.
Name | Type | Description |
---|---|---|
value | number | The point within the range you want to calculate. |
start | number | The start of the range. |
end | number | The end of the range. |
- Source
value between 0 and 1, representing where the "value" parameter falls within the range defined by start and end
- Type:
- number
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.
Name | Type | Description |
---|---|---|
number | number | The number to check. |
- Source
Returns true if the number is a decimal, false otherwise.
- Type:
- boolean
isDecimal(1.5); // true
isDecimal(1); // false
(static) isEven(number) → {boolean}
Check is the given number is a EVEN number
Name | Type | Description |
---|---|---|
number | number | The number to check |
- Source
- Type:
- boolean
isEven(1); // false
isEven(2); // true
(static) isMultipleOf(dividend, divisor) → {boolean}
Check is a number is multiple of other
Name | Type | Description |
---|---|---|
dividend | number | the number check is multiple |
divisor | number | divisor number |
- Source
- Type:
- boolean
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
Name | Type | Description |
---|---|---|
n | number | number to check if a narcissistic |
- Type:
- boolean
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
Name | Type | Description |
---|---|---|
number | number | The number to check |
- Source
- Type:
- boolean
isOdd(1); // true
isOdd(2); // false
(static) isValidNumber(number)
Check if the given value is a valid number
Name | Type | Description |
---|---|---|
number | number | number to validate |
- Source
a boolean
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.
Name | Type | Description |
---|---|---|
value | number | The decimal value used for interpolation |
min | number | The minimum value |
max | number | The maximum value |
- Source
- See
- https://en.wikipedia.org/wiki/Linear_interpolation for more information on linear interpolation
The result of the interpolation
- Type:
- number
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
Name | Type | Description |
---|---|---|
value | number | The number to be re-mapped |
fromRangeStart | number | The start of the range the number is currently in |
fromRangeEnd | number | The end of the range the number is currently in |
toRangeStart | number | The start of the range the number should be mapped to |
toRangeEnd | number | The end of the range the number should be mapped to |
- Source
- The re-mapped number
- Type:
- number
// 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
Name | Type | Attributes | Description |
---|---|---|---|
numbers | number | <repeatable> | the numbers to get the median |
- Source
- Type:
- number
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
Name | Type | Description |
---|---|---|
args | Array | the elements to get the mode |
- Source
- Type:
- *
mode([1, 2, 2, 3, 4]); // 2
mode(["apple", "banana", "banana", "cherry"]); // "banana"
(static) multiplication(…numbers) → {number}
Calculates the product of all given numbers
Name | Type | Attributes | Description |
---|---|---|---|
numbers | number | <repeatable> | The numbers to be multiplied |
- Source
The product of all given numbers
- Type:
- number
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.
Name | Type | Description |
---|---|---|
number | number | The number to be converted |
- Source
- The negative equivalent of the input number, or 0 if the input was 0.
- Type:
- number
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
Name | Type | Description |
---|---|---|
value | number | The value to be calculated as a percentage |
total | number | The total value to be used as reference |
- Source
The calculated percentage
- Type:
- number
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
Name | Type | Description |
---|---|---|
r | number | The radian value to be converted to degrees |
- Source
The converted radian value in degrees
- Type:
- number
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.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
start | number | The starting point of the range | ||
end | number | The ending point of the range | ||
step | number | <optional> | 1 | The increment value |
skip | Array.<{start: number, end: number}> | <optional> | [] | The range of values to skip |
- Source
An array of numbers
- Type:
- Array.<number>
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.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
num | number | The number to round. | ||
maxOfDecimals | number | <optional> | 2 | The maximum number of decimals to round to. Defaults to 2 if not provided. |
- Source
- If the
num
parameter is not a number.
- If the
- Type
- TypeError
- If the
maxOfDecimals
parameter is not a number.
- If the
- Type
- TypeError
- If the
maxOfDecimals
parameter is less than 0
- If the
- Type
- RangeError
- The rounded number.
- Type:
- number
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
Name | Type | Attributes | Description |
---|---|---|---|
numbers | number | <repeatable> | the numbers to subtraction |
- Source
- Type:
- number
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
Name | Type | Attributes | Description |
---|---|---|---|
numbers | number | <repeatable> | the numbers to sum |
- Source
- Type:
- number
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