Number
Edit on GitHubUtilities for working with numbers.
Added in 0.4.0
No other changes yet.
Types
Type declarations included in the Number module.
Number.ParseIntError
Added in 0.6.0
No other changes yet.
Represents an error that occurred trying to parse an integer.
Variants:
Represents an error caused by trying to parse an empty string.
Represents an error caused by trying to parse a string with an invalid character.
Represents an error caused by trying to parse with an invalid radix.
Values
Functions and constants included in the Number module.
Number.pi
Added in 0.5.2
No other changes yet.
Pi represented as a Number value.
Number.tau
Added in 0.5.2
No other changes yet.
Tau represented as a Number value.
Number.e
Added in 0.5.2
No other changes yet.
Euler’s number represented as a Number value.
Number.(+)
Added in 0.6.0
version | changes |
---|---|
0.4.0 | Originally named `add` |
Computes the sum of its operands.
Parameters:
param | type | description |
---|---|---|
num1 |
Number |
The first operand |
num2 |
Number |
The second operand |
Returns:
type | description |
---|---|
Number |
The sum of the two operands |
Examples:
Number.(-)
Added in 0.6.0
version | changes |
---|---|
0.4.0 | Originally named `sub` |
Computes the difference of its operands.
Parameters:
param | type | description |
---|---|---|
num1 |
Number |
The first operand |
num2 |
Number |
The second operand |
Returns:
type | description |
---|---|
Number |
The difference of the two operands |
Examples:
Number.(*)
Added in 0.6.0
version | changes |
---|---|
0.4.0 | Originally named `mul` |
Computes the product of its operands.
Parameters:
param | type | description |
---|---|---|
num1 |
Number |
The first operand |
num2 |
Number |
The second operand |
Returns:
type | description |
---|---|
Number |
The product of the two operands |
Examples:
Number.(/)
Added in 0.6.0
version | changes |
---|---|
0.4.0 | Originally named `div` |
Computes the quotient of its operands.
Parameters:
param | type | description |
---|---|---|
num1 |
Number |
The dividend |
num2 |
Number |
The divisor |
Returns:
type | description |
---|---|
Number |
The quotient of the two operands |
Examples:
Number.(**)
Added in 0.6.0
version | changes |
---|---|
0.5.4 | Originally named `pow` |
Computes the exponentiation of the given base and power.
Parameters:
param | type | description |
---|---|---|
base |
Number |
The base number |
power |
Number |
The exponent number |
Returns:
type | description |
---|---|
Number |
The base raised to the given power |
Examples:
Number.exp
Added in 0.5.4
No other changes yet.
Computes the exponentiation of Euler’s number to the given power.
Parameters:
param | type | description |
---|---|---|
power |
Number |
The exponent number |
Returns:
type | description |
---|---|
Number |
The Number.e value raised to the given power |
Examples:
Number.sqrt
Added in 0.4.0
No other changes yet.
Computes the square root of its operand.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The number to square root |
Returns:
type | description |
---|---|
Number |
The square root of the operand |
Examples:
Number.sign
Added in 0.5.0
No other changes yet.
Determine the positivity or negativity of a Number.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The number to inspect |
Returns:
type | description |
---|---|
Number |
-1 if the number is negative, 1 if positive, or 0 otherwise; signedness of -0.0 is preserved |
Examples:
Number.min
Added in 0.4.0
version | changes |
---|---|
0.5.4 | Handle NaN properly |
Returns the smaller of its operands.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The first operand |
y |
Number |
The second operand |
Returns:
type | description |
---|---|
Number |
The smaller of the two operands |
Examples:
Number.max
Added in 0.4.0
version | changes |
---|---|
0.5.4 | Handle NaN properly |
Returns the larger of its operands.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The first operand |
y |
Number |
The second operand |
Returns:
type | description |
---|---|
Number |
The larger of the two operands |
Examples:
Number.ceil
Added in 0.4.0
version | changes |
---|---|
0.5.4 | Handle NaN and Infinity properly |
Rounds its operand up to the next largest integer.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The number to round |
Returns:
type | description |
---|---|
Number |
The next largest integer of the operand |
Examples:
Number.floor
Added in 0.4.0
version | changes |
---|---|
0.5.4 | Handle NaN and Infinity properly |
Rounds its operand down to the largest integer less than the operand.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The number to round |
Returns:
type | description |
---|---|
Number |
The previous integer of the operand |
Examples:
Number.trunc
Added in 0.4.0
version | changes |
---|---|
0.5.4 | Handle NaN and Infinity properly |
Returns the integer part of its operand, removing any fractional value.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The number to truncate |
Returns:
type | description |
---|---|
Number |
The integer part of the operand |
Examples:
Number.round
Added in 0.4.0
version | changes |
---|---|
0.5.4 | Handle NaN and Infinity properly |
Returns its operand rounded to its nearest integer.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The number to round |
Returns:
type | description |
---|---|
Number |
The nearest integer to the operand |
Examples:
Number.abs
Added in 0.4.0
No other changes yet.
Returns the absolute value of a number. That is, it returns x
if x
is positive or zero and the negation of x
if x
is negative.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The operand |
Returns:
type | description |
---|---|
Number |
The absolute value of the operand |
Examples:
Number.neg
Added in 0.4.0
No other changes yet.
Returns the negation of its operand.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The number to negate |
Returns:
type | description |
---|---|
Number |
The negated operand |
Examples:
Number.isFloat
Added in 0.5.3
No other changes yet.
Checks if a number is a floating point value.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The number to check |
Returns:
type | description |
---|---|
Bool |
true if the value is a floating point number or false otherwise |
Examples:
Number.isInteger
Added in 0.5.3
No other changes yet.
Checks if a number is an integer.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The number to check |
Returns:
type | description |
---|---|
Bool |
true if the value is an integer or false otherwise |
Examples:
Number.isRational
Added in 0.5.3
No other changes yet.
Checks if a number is a non-integer rational value.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The number to check |
Returns:
type | description |
---|---|
Bool |
true if the value is a non-integer rational number or false otherwise |
Examples:
Number.isFinite
Added in 0.4.0
No other changes yet.
Checks if a number is finite. All values are finite exept for floating point NaN, infinity or negative infinity.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The number to check |
Returns:
type | description |
---|---|
Bool |
true if the value is finite or false otherwise |
Examples:
Number.isNaN
Added in 0.4.0
No other changes yet.
Checks if a number is the float NaN value (Not A Number).
Parameters:
param | type | description |
---|---|---|
x |
Number |
The number to check |
Returns:
type | description |
---|---|
Bool |
true if the value is NaN, otherwise false |
Examples:
Number.isInfinite
Added in 0.4.0
No other changes yet.
Checks if a number is infinite, that is either of floating point positive or negative infinity. Note that this function is not the exact opposite of isFinite(Number) in that it doesn’t return true for NaN.
Parameters:
param | type | description |
---|---|---|
x |
Number |
The number to check |
Returns:
type | description |
---|---|
Bool |
true if the value is infinite or false otherwise |
Examples:
Number.isClose
Added in 0.6.0
No other changes yet.
Determines whether two values are considered close to each other using a relative and absolute tolerance.
Parameters:
param | type | description |
---|---|---|
a |
Number |
The first value |
b |
Number |
The second value |
?relativeTolerance |
Number |
The maximum tolerance to use relative to the larger absolute value a or b |
?absoluteTolerance |
Number |
The absolute tolerance to use, regardless of the values of a or b |
Returns:
type | description |
---|---|
Bool |
true if the values are considered close to each other or false otherwise |
Examples:
Number.parseInt
Added in 0.4.5
version | changes |
---|---|
0.6.0 | Switched from a string-based error message to a structured error enum |
Parses a string representation of an integer into a Number
using the
specified radix (also known as a number system “base”).
If the string has a radix prefix (i.e. “0x”/“0X”, “0o”/“0O”, or “0b”/“0B” for radixes 16, 8, or 2 respectively), the supplied radix is ignored in favor of the prefix. Underscores that appear in the numeric portion of the input are ignored.
Parameters:
param | type | description |
---|---|---|
string |
String |
The string to parse |
radix |
Number |
The number system base to use when parsing the input string |
Returns:
type | description |
---|---|
Result<Number, Atoi.ParseIntError> |
Ok(value) containing the parsed number on a successful parse or Err(err) containing a variant of ParseIntError |
Examples:
Number.parseFloat
Added in 0.5.5
No other changes yet.
Parses a string representation of a float into a Number
. Underscores that appear
in numeric portions of the input are ignored.
Parameters:
param | type | description |
---|---|---|
string |
String |
The string to parse |
Returns:
type | description |
---|---|
Result<Number, String> |
Ok(value) containing the parsed number on a successful parse or Err(msg) containing an error message string otherwise |
Examples:
Number.parse
Added in 0.5.5
No other changes yet.
Parses a string representation of an integer, float, or rational into a Number
.
Underscores that appear in the numeric portion of the input are ignored.
Parameters:
param | type | description |
---|---|---|
input |
String |
The string to parse |
Returns:
type | description |
---|---|
Result<Number, Atoi.ParseIntError> |
Ok(value) containing the parsed number on a successful parse or Err(msg) containing an error message string otherwise |
Examples:
Number.asin
Added in 0.6.0
No other changes yet.
Computes the inverse sine of the given angle.
Parameters:
param | type | description |
---|---|---|
angle |
Number |
A number between -1 and 1, representing the angle’s sine value |
Returns:
type | description |
---|---|
Number |
The inverse sine (angle in radians between -pi/2 and pi/2 ) of the given angle or NaN if the given angle is not between-1 and 1 |
Examples:
Number.acos
Added in 0.6.0
No other changes yet.
Computes the inverse cosine of the given angle.
Parameters:
param | type | description |
---|---|---|
angle |
Number |
A number between -1 and 1, representing the angle’s cosine value |
Returns:
type | description |
---|---|
Number |
The inverse cosine (angle in radians between -pi/2 and pi/2 ) of the given angle or NaN if the given angle is not between-1 and 1 |
Examples:
Number.atan
Added in 0.6.0
No other changes yet.
Computes the inverse tangent of the given angle.
Parameters:
param | type | description |
---|---|---|
angle |
Number |
A number between -1 and 1, representing the angle’s tangent value |
Returns:
type | description |
---|---|
Number |
The inverse tangent (angle in radians between -pi/2 and pi/2 ) of the given angle or NaN if the given angle is not between-1 and 1 |
Examples:
Number.atan2
Added in 0.6.0
No other changes yet.
Computes the angle between the positive x-axis and the ray from the origin to the point (x, y).
Parameters:
param | type | description |
---|---|---|
y |
Number |
The given y coordinate |
x |
Number |
The given x coordinate |
Returns:
type | description |
---|---|
Number |
The angle in radians between the positive x-axis and the point (x, y) |
Examples:
Number.toRadians
Added in 0.5.4
No other changes yet.
Converts degrees to radians.
Parameters:
param | type | description |
---|---|---|
degrees |
Number |
The value to convert |
Returns:
type | description |
---|---|
Number |
The value in radians |
Examples:
Number.toDegrees
Added in 0.5.4
No other changes yet.
Converts radians to degrees.
Parameters:
param | type | description |
---|---|---|
radians |
Number |
The value to convert |
Returns:
type | description |
---|---|
Number |
The value in degrees |
Examples:
Number.clamp
Added in 0.6.0
No other changes yet.
Constrains a number within the given inclusive range.
Parameters:
param | type | description |
---|---|---|
range |
Range<Number> |
The inclusive range to clamp within |
input |
Number |
The number to clamp |
Returns:
type | description |
---|---|
Number |
The constrained number |
Number.linearInterpolate
Added in 0.6.0
No other changes yet.
Maps a weight between 0 and 1 within the given inclusive range.
Parameters:
param | type | description |
---|---|---|
range |
Range<Number> |
The inclusive range to interpolate within |
weight |
Number |
The weight to interpolate |
Returns:
type | description |
---|---|
Number |
The blended value |
Throws:
InvalidArgument(String)
- When
weight
is not between 0 and 1 - When
range
is not finite - When
range
includes NaN
Number.linearMap
Added in 0.6.0
No other changes yet.
Scales a number from one inclusive range to another inclusive range. If the number is outside the input range, it will be clamped.
Parameters:
param | type | description |
---|---|---|
inputRange |
Range<Number> |
The inclusive range you are mapping from |
outputRange |
Range<Number> |
The inclusive range you are mapping to |
current |
Number |
The number to map |
Returns:
type | description |
---|---|
Number |
The mapped number |
Throws:
InvalidArgument(String)
- When
inputRange
is not finite - When
inputRange
includes NaN - When
outputRange
is not finite - When
outputRange
includes NaN