Numeric Utilities Reference

Table of contents


Introduction

The numeric utility functions and constants are useful for manipulating numbers. All utility functions and constants are properties of the global/window object and may be used without importing.


Functions

addAverage()

Syntax

addAverage(avg, value, n)

Add a new value to an average. n is the total number of samples, including this one. Returns the updated average. For example:

avg = addAverage(avg, value, ++n)

Parameters

Return value

A number. The updated average.


adjust360()

Syntax

adjust360(deg)

Return deg adjusted so that the angle is equivalent and > 0 but ≤ 360.

Parameters

Return value

The adjusted number.


angleDiff()

Syntax

angleDiff(a, b)

Return the shortest angle difference in degrees (±180) from a to b. Positive values mean b is clockwise from a. Negative values mean b is counter-clockwise from a.

Parameters

Return value

A number in degrees between -180 and 180.


average()

Syntax

average(...args)

Returns the average of all the arguments.

Parameters

Return value

A number. The average of all the arguments.


clamp()

Syntax

clamp(value, min, max)

Returns the value constrained to be within the range min to max.

Parameters

Return value

A number. The value clamped to be within the range min to max.


epsilonEqual()

Syntax

epsilonEqual(a, b)

Returns true if numbers a and b are within EPSILON of each other.

Parameters

Return value

A Boolean. True if a and b are within EPSILON of each other.


isAngleIn()

Syntax

isAngleIn(a, start, end)

Return true if angle a in degrees is between start moving clockwise to end.

Parameters

Return value

A Boolean. True if a in degrees is between start moving clockwise to end.


fmtNum()

Syntax

fmtNum(num)
fmtNum(num, digits)

Returns num as a string with thousands separators. If digits is present and greater than 0, then the result is rounded to that number of digits and that number of digits appear to the right of the decimal.

Parameters

Return value

A string containing the formatted num.


fmtRange()

Syntax

fmtRange(start, end)
fmtRange(start, end, unit)
fmtRange(start, end, unit, sep)

Returns a string representing a numeric range from start to end. If unit is present, it is a string containing a unit abbreviation that is included in the range. If sep is present, it is a string containing a separator that is placed between the start and end values in the formatted range. The default separator is '-'. For example:

fmtRange(0, 10)

returns 0 - 10.

fmtRange(0, 10, 'kg.', 'to')

returns 0 kg. to 10 kg.. If start is equal to end, then only the start and any unit is returned.

Parameters

Return value

A string containing the formatted range.


getDigit()

Syntax

getDigit(value, digit)

Extracts a single decimal digit from value. A digit value of zero specifies the rightmost integer digit, increasing for digits to the left of the decimal point and decreasing for digits to the right of the decimal point. For example:

getDigit(12.34, -1) == 3

Parameters

Return value

An integer number.


getDigits()

Syntax

getDigits(value, digit, len)

Extracts decimal digits from value. A digit value of zero specifies the rightmost integer digit, increasing for digits to the left of the decimal point and decreasing for digits to the right of the decimal point. len specifies the number of digits to extract from the starting point moving right. For example:

getDigit(12.34, 1, 2) == 12

Parameters

Return value

An integer number.


iDigits()

Syntax

iDigits(value)

Returns the number of integer digits in value.

Parameters

Return value

An integer number.


integrate()

Syntax

integrate(fn, from, to, incr)

Return the sum of a function over a numeric interval. The function fn(start, size) is called with a start value beginning at from, then is repeatedly called with new start values by adding incr to the last start value, stopping when the entire interval between from and to is covered. integrate() returns the sum of the values returned by calling fn().

The size argument to fn() indicates the size of the sub-interval. For all sub-intervals except the last, this is the incr value. If incr does not evenly divide the interval from from to to, then the last call to fn() will be the size of the partial sub-interval remaining.

The from value can be greater than the to value provided incr is negative.

Parameters

Return value

A number that's the sum of all the returned values of fn() for all sub-intervals.


interpolate()

Syntax

interpolate(x, x1, y1, x2, y2)

Returns the Y value associated with the x value by linearly interpolating a line on a two-dimensional plane between points (x1, y1) and (x2, y2). The x value must be between x1 and x2.

Parameters

Return value

The numeric Y value of the x value on a line between (x1, y1) and (x2, y2).


isAngleIn()

Syntax

isAngleIn(a, start, end)

Return true if angle a in degrees is between start moving clockwise to end.

Parameters

Return value

A Boolean that's true if a is in the angle between start moving clockwise to end.


isApproximately()

Syntax

isApproximately(value1, value2, ratio)

Returns true if value2 is within approximation bounds of value1. The approximation bounds are:
value1 ± (ratio * value1).

For example:

isApproximately(1, 1.1, 0.2) === true

Parameters

Return value

A boolean that's true if value2 falls within the approximation bounds of value1.


isBetween()

Syntax

isBetween(value, min, max)

Returns true if value is greater than or equal to min and less than or equal to max.

Parameters

Return value

A boolean that's true if value falls within the specified range.


isValidNumInputString()

Syntax

isValidNumInputString(str)

Return true if the str string is valid number or a prefix of a valid number.

Parameters

Return value

A boolean.


lineLength()

Syntax

lineLength(x1, y1, x2, y2)

Returns the length of the line from (x1, y1) to (x2, y2).

Parameters

Return value

A number. The line length.


round()

Syntax

round(value)
round(value, digits)

If digits is zero or omitted, rounds to the nearest integer value. Otherwise, if digits is a positive integer, it rounds to the nearest digits places to the right of the decimal point. For example:

round(12.34, 1) === 12.3

If digits is a negative integer, it rounds to the nearest -digits places to the left of the decimal point. For example:

round(123.45, -1) === 120

Parameters

Return value

The rounded number.


roundDown()

Syntax

roundDown(value)
roundDown(value, digits)

If digits is zero or omitted, rounds down to the next integer value. Negative numbers round lower. For example:

roundDown(-12.34) === -13

If digits is a positive integer, it rounds down to the next digits places to the right of the decimal point. For example:

roundDown(12.34, 1) === 12.3

If digits is a negative integer, it rounds down to the next -digits places to the left of the decimal point. For example:

roundDown(123.45, -1) === 120

Parameters

Return value

The rounded number.


roundDownMult()

Syntax

roundDownMult(value, mult)

Rounds down to the next multiple of mult. For example:

roundDownMult(123.45, 20) === 120

and

roundDownMult(123.45, .5) === 123

Parameters

Return value

The rounded number.


roundMult()

Syntax

roundMult(value)
roundMult(value, mult)

Rounds to the nearest multiple of mult. For example:

roundMult(123.45, 20) === 120

and

roundMult(123.45, .5) === 123.5

If mult is a string, it contains a number to be used as the multiple followed by a 'd', or 'u' to round up or down, respectively. For example:

roundMult(123.45, '20u') === 140

and

roundMult(123.45, '.5d') === 123

Parameters

Return value

The rounded number.


roundUp()

Syntax

roundUp(value)
roundUp(value, digits)

If digits is zero or omitted, rounds up to the next integer value. Negative numbers round higher. For example:

roundUp(-12.34) === -12

If digits is a positive integer, it rounds up to the next digits places to the right of the decimal point. For example:

roundUp(12.34, 1) === 12.4

If digits is a negative integer, it rounds up to the next -digits places to the left of the decimal point. For example:

roundUp(123.45, -1) === 130

Parameters

Return value

The rounded number.


roundUpMult()

Syntax

roundUpMult(value, mult)

Rounds up to the next multiple of mult. For example:

roundUpMult(123.45, 20) === 140

and

roundUpMult(123.45, .5) === 123.5

Parameters

Return value

The rounded number.


weightedAverage()

Syntax

weightedAverage(values, weights)

Returns the average of the numbers in the values array. Each value is weighted by the corresponding weight in the weights array. The weights do not need to add up to 1.

Parameters

Return value

A number. The weighted average of all the values.


Constants

EPSILON

The value used to assume rough equality in epsilonEqual(). This is a small number but quite a bit bigger than Number.MIN_VALUE. It is a practical smallest number for printed non-exponential fractions and is used to prevent useless arithmetic differences due to the way JavaScript numbers are represented.