@rbxts/jsnatives
Number Constants
This page documents all constant numeric values provided by the Number object.
EPSILON
const EPSILON: number
The smallest interval between two representable numbers.
Example:
console.log(Number.EPSILON);// Outputs: 2.220446049250313e-16
// Practical usage - checking if two numbers are nearly equalfunction almostEqual(a: number, b: number): boolean { return Math.abs(a - b) < Number.EPSILON;}
MAX_SAFE_INTEGER
const MAX_SAFE_INTEGER: number
The maximum safe integer in JavaScript (2^53 - 1).
Example:
console.log(Number.MAX_SAFE_INTEGER);// Outputs: 9007199254740991
// Check if a number can be safely used as an integerfunction isSafeForIntegerOperations(num: number): boolean { return num <= Number.MAX_SAFE_INTEGER && num >= Number.MIN_SAFE_INTEGER;}
MIN_SAFE_INTEGER
const MIN_SAFE_INTEGER: number
The minimum safe integer in JavaScript (-(2^53 - 1)).
Example:
console.log(Number.MIN_SAFE_INTEGER);// Outputs: -9007199254740991
MAX_VALUE
const MAX_VALUE: number
The largest positive representable number.
Example:
console.log(Number.MAX_VALUE);// Outputs: 1.7976931348623157e+308
// Check if a computation might overflowfunction willOverflow(a: number, b: number): boolean { // Simple check, not perfect for all cases return a > Number.MAX_VALUE / b;}
MIN_VALUE
const MIN_VALUE: number
The smallest positive representable number (closest to zero).
Example:
console.log(Number.MIN_VALUE);// Outputs: 5e-324
// Check if a number is effectively zerofunction isEffectivelyZero(num: number): boolean { return Math.abs(num) < Number.MIN_VALUE;}
NaN
const NaN: number
Represents "Not a Number" value.
Example:
console.log(Number.NaN);// Outputs: NaN
// Operations that produce NaNconsole.log(0/0); // NaNconsole.log(Math.sqrt(-1)); // NaN
NegativeNaN
const NegativeNaN: number
Represents a negative "Not a Number" value.
Example:
console.log(Number.NegativeNaN);// Outputs: -NaN
NEGATIVE_INFINITY
const NEGATIVE_INFINITY: number
Represents negative infinity.
Example:
console.log(Number.NEGATIVE_INFINITY);// Outputs: -Infinity
// Operations that produce negative infinityconsole.log(-1/0); // -Infinityconsole.log(Math.log(0)); // -Infinity
POSITIVE_INFINITY
const POSITIVE_INFINITY: number
Represents positive infinity.
Example:
console.log(Number.POSITIVE_INFINITY);// Outputs: Infinity
// Operations that produce positive infinityconsole.log(1/0); // Infinityconsole.log(Number.MAX_VALUE * 2); // Infinity
Practical Examples
Using Constants in Range Validation
function validateNumberRange(num: number): string { if (Number.isNaN(num)) { return "Invalid number"; }
if (num === Number.POSITIVE_INFINITY) { return "Value is too large"; }
if (num === Number.NEGATIVE_INFINITY) { return "Value is too small"; }
if (Math.abs(num) > Number.MAX_SAFE_INTEGER) { return "Value is outside safe integer range"; }
return "Value is valid";}
Working with Precision in Calculations
function areFloatsEqual(a: number, b: number, precision = Number.EPSILON): boolean { return Math.abs(a - b) < precision;}
// Example usageconsole.log(areFloatsEqual(0.1 + 0.2, 0.3)); // trueconsole.log(0.1 + 0.2 === 0.3); // false (due to floating point precision issues)
Handling Special Number Cases
function describeNumber(num: number): string { if (Number.isNaN(num)) { return "Not a Number"; }
if (num === Number.POSITIVE_INFINITY) { return "Positive Infinity"; }
if (num === Number.NEGATIVE_INFINITY) { return "Negative Infinity"; }
if (num === 0) { return "Zero"; }
if (Number.isInteger(num)) { return "Integer"; }
return "Floating Point Number";}