X-Git-Url: https://git.ladys.computer/Pisces/blobdiff_plain/5af99cb7def7f5a9b19db6a8e214920dd233113d..fb3e0d562e2dbe9e3ea911a80bfdabc8851f92b2:/numeric.js?ds=inline diff --git a/numeric.js b/numeric.js index 78ea64c..64d9a80 100644 --- a/numeric.js +++ b/numeric.js @@ -377,6 +377,12 @@ export const { isSafeInteger: isSafeIntegralNumber, } = Number; +/** Positive zero. */ +export const POSITIVE_ZERO = 0; + +/** Negative zero. */ +export const NEGATIVE_ZERO = -0; + /** * Returns the magnitude (absolute value) of the provided value. * @@ -560,10 +566,10 @@ export const sgn = ($) => { ? n === 0n ? 0n : n < 0n ? -1n : 1n : isNan(n) || n === 0 ? n - : //deno-lint-ignore no-compare-neg-zero - n < -0 - ? -1 - : 1; + //deno-lint-ignore no-compare-neg-zero + : n < -0 + ? -1 + : 1; }; /** @@ -605,19 +611,75 @@ export const { return { toIntN: (n, $) => { const prim = toPrimitive($); - const big·int = toBigInt(prim); - const intN = asIntN(n, big·int); - return typeof prim === "bigint" ? intN : toNumber(intN); + if (typeof prim === "bigint") { + // The primitive value is a big·int. + return asIntN(n, prim); + } else { + // The primitive value is not a big·int. + const int = trunc(prim); + if (!isFiniteNumber(int) || int == 0) { + // The truncated value is zero or not finite. + return 0; + } else { + // The truncated value is finite. + return toNumber(asIntN(n, toBigInt(int))); + } + } }, toUintN: (n, $) => { const prim = toPrimitive($); - const big·int = toBigInt(prim); - const uintN = asUintN(n, big·int); - return typeof prim === "bigint" ? uintN : toNumber(uintN); + if (typeof prim === "bigint") { + // The primitive value is a big·int. + return asUintN(n, prim); + } else { + // The primitive value is not a big·int. + const int = trunc(prim); + if (!isFiniteNumber(int) || int == 0) { + // The truncated value is zero or not finite. + return 0; + } else { + // The truncated value is finite. + return toNumber(asUintN(n, toBigInt(int))); + } + } }, }; })(); +/** + * Returns the result of converting the provided number to an integer + * or infinity. + * + * ※ This function will never return negative zero. + */ +export const toIntegerOrInfinity = ($) => { + const integer = trunc(toNumber($)); + if (isNan(integer) || integer == 0) { + // The provided value truncs to nan or (positive or negative) zero. + return 0; + } else if (integer == POSITIVE_INFINITY) { + // The provided value truncs to positive infinity. + return POSITIVE_INFINITY; + } else if (integer == NEGATIVE_INFINITY) { + // The provided value truncs to negative infinity. + return NEGATIVE_INFINITY; + } else { + // The provided value truncs to an integer. + return integer; + } +}; + +/** + * Returns the result of converting the provided number to an integral + * number. + * + * ※ This function will never return negative zero. + */ +export const toIntegralNumber = ($) => { + const n = toIntegerOrInfinity($); + return !isFiniteNumber(n) || n == 0 ? 0 : n; +}; + /** * Returns the result of converting the provided value to a number. *