p-number
The number module provides Number utility functions and numeric constants.
use p-number as nType Checks
Section titled “Type Checks”isfinite[val]
Section titled “isfinite[val]”Returns true if val is a finite number.
isinteger[val]
Section titled “isinteger[val]”Returns true if val is an integer.
isnan[val]
Section titled “isnan[val]”Returns true if val is NaN.
issafe[val]
Section titled “issafe[val]”Returns true if val is a safe integer (within Number.MAX_SAFE_INTEGER range).
use p-number as nn.isfinite[42] -- truen.isfinite[infinity] -- falsen.isinteger[3.0] -- truen.isinteger[3.5] -- falsen.isnan[nan] -- truen.issafe[42] -- trueParsing
Section titled “Parsing”parsefloat[str]
Section titled “parsefloat[str]”Parses a string and returns a floating-point number.
parseint[str; radix]
Section titled “parseint[str; radix]”Parses a string and returns an integer. radix defaults to 10.
use p-number as nn.parsefloat[///3.14///] -- 3.14n.parseint[///FF///; 16] -- 255n.parseint[///42///] -- 42Formatting
Section titled “Formatting”tofixed[num; digits]
Section titled “tofixed[num; digits]”Returns a string with the number formatted to digits decimal places. digits defaults to 0.
toprecision[num; digits]
Section titled “toprecision[num; digits]”Returns a string with the number formatted to digits significant digits.
toexponential[num; digits]
Section titled “toexponential[num; digits]”Returns a string in exponential notation with digits decimal places.
tostring[num; radix]
Section titled “tostring[num; radix]”Returns a string representation in the specified base. radix defaults to 10.
use p-number as nn.tofixed[3.14159; 2] -- "3.14"n.toprecision[123.456; 4] -- "123.5"n.toexponential[1234; 2] -- "1.23e+3"n.tostring[255; 16] -- "ff"Utility
Section titled “Utility”clamp[num; min; max]
Section titled “clamp[num; min; max]”Returns num clamped to the range [min, max].
use p-number as nn.clamp[15; 0; 10] -- 10n.clamp[-5; 0; 10] -- 0n.clamp[5; 0; 10] -- 5Constants
Section titled “Constants”| Name | JS Equivalent | Description |
|---|---|---|
maxsafe | Number.MAX_SAFE_INTEGER | Maximum safe integer (2^53 - 1) |
minsafe | Number.MIN_SAFE_INTEGER | Minimum safe integer (-(2^53 - 1)) |
epsilon | Number.EPSILON | Smallest difference between representable numbers |
maxvalue | Number.MAX_VALUE | Maximum representable number |
minvalue | Number.MIN_VALUE | Minimum positive representable number |
posinf | Number.POSITIVE_INFINITY | Positive infinity |
neginf | Number.NEGATIVE_INFINITY | Negative infinity |
use p-number as nconsole.log[n.maxsafe] -- 9007199254740991console.log[n.epsilon] -- 2.220446049250313e-16