-// ♓🌟 Piscēs ∷ numeric.js
-// ====================================================================
-//
-// Copyright © 2022–2023 Lady [@ Lady’s Computer].
-//
-// This Source Code Form is subject to the terms of the Mozilla Public
-// License, v. 2.0. If a copy of the MPL was not distributed with this
-// file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
+// SPDX-FileCopyrightText: 2022, 2023, 2025 Lady <https://www.ladys.computer/about/#lady>
+// SPDX-License-Identifier: MPL-2.0
+/**
+ * ⁌ ♓🧩 Piscēs ∷ numeric.js
+ *
+ * Copyright © 2022–2023, 2025 Lady [@ Ladys Computer].
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
+ */
import { call, createArrowFunction } from "./function.js";
+import { defineOwnDataProperty } from "./object.js";
import {
stringCatenate,
stringPadEnd,
substring,
toString,
} from "./string.js";
-import { sameValue, toPrimitive } from "./value.js";
-
-export const {
- /**
- * ln(10).
- *
- * ※ This is an alias for `Math.LN10`.
- */
- LN10,
-
- /**
- * ln(2).
- *
- * ※ This is an alias for `Math.LN2`.
- */
- LN2,
-
- /**
- * log10(ℇ).
- *
- * ※ This is an alias for `Math.LOG10E`.
- */
- LOG10E: LOG10ℇ,
-
- /**
- * log2(ℇ).
- *
- * ※ This is an alias for `Math.LOG2E`.
- */
- LOG2E: LOG2ℇ,
-
- /**
- * sqrt(½).
- *
- * ※ This is an alias for `Math.SQRT1_2`.
- */
- SQRT1_2: RECIPROCAL_SQRT2,
-
- /**
- * sqrt(2).
- *
- * ※ This is an alias for `Math.SQRT2`.
- */
- SQRT2,
-
- /**
- * The mathematical constant π.
- *
- * ※ This is an alias for `Math.PI`.
- */
- PI: Π,
-
- /**
- * The Euler number.
- *
- * ※ This is an alias for `Math.E`.
- */
- E: ℇ,
-} = Math;
-
-export const {
- /**
- * The largest number value less than infinity.
- *
- * ※ This is an alias for `Number.MAX_VALUE`.
- */
- MAX_VALUE: MAXIMUM_NUMBER,
-
- /**
- * 2**53 - 1.
- *
- * ※ This is an alias for `Number.MAX_SAFE_INTEGER`.
- */
- MAX_SAFE_INTEGER: MAXIMUM_SAFE_INTEGRAL_NUMBER,
-
- /**
- * The smallest number value greater than negative infinity.
- *
- * ※ This is an alias for `Number.MIN_VALUE`.
- */
- MIN_VALUE: MINIMUM_NUMBER,
-
- /**
- * -(2**53 - 1).
- *
- * ※ This is an alias for `Number.MIN_SAFE_INTEGER`.
- */
- MIN_SAFE_INTEGER: MINIMUM_SAFE_INTEGRAL_NUMBER,
-
- /**
- * Negative infinity.
- *
- * ※ This is an alias for `Number.NEGATIVE_INFINITY`.
- */
+import {
+ NAN,
NEGATIVE_INFINITY,
-
- /**
- * Nan.
- *
- * ※ This is an alias for `Number.NaN`.
- */
- NaN: NAN,
-
- /**
- * Positive infinity.
- *
- * ※ This is an alias for `Number.POSITIVE_INFINITY`.
- */
POSITIVE_INFINITY,
+ sameValue,
+ toPrimitive,
+ UNDEFINED,
+} from "./value.js";
- /**
- * The difference between 1 and the smallest number greater than 1.
- *
- * ※ This is an alias for `Number.EPSILON`.
- */
- EPSILON: Ε,
-} = Number;
-
-/** Negative zero. */
-export const NEGATIVE_ZERO = -0;
-
-/** Positive zero. */
-export const POSITIVE_ZERO = 0;
+const PISCĒS = "♓🧩 Piscēs";
/**
* Returns the magnitude (absolute value) of the provided value.
*
- * ※ Unlike `Math.abs`, this function can take big·int arguments.
+ * ※ Unlike `Math.abs´, this function can take big·int arguments.
*/
export const abs = ($) => {
const n = toNumeric($);
/**
* Returns the arccos of the provided value.
*
- * ※ This function is effectively an alias for `Math.acos`.
+ * ※ This function is effectively an alias for `Math.acos´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the arccosh of the provided value.
*
- * ※ This function is effectively an alias for `Math.acosh`.
+ * ※ This function is effectively an alias for `Math.acosh´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the arcsin of the provided value.
*
- * ※ This function is effectively an alias for `Math.asin`.
+ * ※ This function is effectively an alias for `Math.asin´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the arcsinh of the provided value.
*
- * ※ This function is effectively an alias for `Math.asinh`.
+ * ※ This function is effectively an alias for `Math.asinh´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the arctan of the provided value.
*
- * ※ This function is effectively an alias for `Math.atan`.
+ * ※ This function is effectively an alias for `Math.atan´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the arctangent of the dividend of the provided values.
*
- * ※ Unlike `Math.atan2`, this function can take big·int arguments.
+ * ※ Unlike `Math.atan2´, this function can take big·int arguments.
* However, the result will always be a number.
*/
arctan2,
* Returns the number of leading zeroes in the 32‐bit representation
* of the provided value.
*
- * ※ Unlike `Math.clz32`, this function accepts either number or
+ * ※ Unlike `Math.clz32´, this function accepts either number or
* big·int values.
*/
clz32,
* Returns the 32‐bit float which best approximate the provided
* value.
*
- * ※ Unlike `Math.fround`, this function can take big·int arguments.
+ * ※ Unlike `Math.fround´, this function can take big·int arguments.
* However, the result will always be a number.
*/
toFloat32,
/**
* Returns the arctanh of the provided value.
*
- * ※ This function is effectively an alias for `Math.atanh`.
+ * ※ This function is effectively an alias for `Math.atanh´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the cube root of the provided value.
*
- * ※ This function is effectively an alias for `Math.cbrt`.
+ * ※ This function is effectively an alias for `Math.cbrt´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the ceiling of the provided value.
*
- * ※ This function is effectively an alias for `Math.ceil`.
+ * ※ This function is effectively an alias for `Math.ceil´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the cos of the provided value.
*
- * ※ This function is effectively an alias for `Math.cos`.
+ * ※ This function is effectively an alias for `Math.cos´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the cosh of the provided value.
*
- * ※ This function is effectively an alias for `Math.cosh`.
+ * ※ This function is effectively an alias for `Math.cosh´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the Euler number raised to the provided value.
*
- * ※ This function is effectively an alias for `Math.exp`.
+ * ※ This function is effectively an alias for `Math.exp´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the Euler number raised to the provided value, minus one.
*
- * ※ This function is effectively an alias for `Math.expm1`.
+ * ※ This function is effectively an alias for `Math.expm1´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the floor of the provided value.
*
- * ※ This function is effectively an alias for `Math.floor`.
+ * ※ This function is effectively an alias for `Math.floor´.
*
* ☡ This function does not allow big·int arguments.
*/
* Returns the square root of the sum of the squares of the provided
* arguments.
*
- * ※ This function is effectively an alias for `Math.hypot`.
+ * ※ This function is effectively an alias for `Math.hypot´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns whether the provided value is a finite number.
*
- * ※ This function is effectively an alias for `Number.isFinite`.
+ * ※ This function is effectively an alias for `Number.isFinite´.
*/
export const isFiniteNumber = createArrowFunction(
Number.isFinite,
/**
* Returns whether the provided value is an integral number.
*
- * ※ This function is effectively an alias for `Number.isInteger`.
+ * ※ This function is effectively an alias for `Number.isInteger´.
*/
export const isIntegralNumber = createArrowFunction(
Number.isInteger,
/**
* Returns whether the provided value is nan.
*
- * ※ This function is effectively an alias for `Number.isNaN`.
+ * ※ This function is effectively an alias for `Number.isNaN´.
*/
export const isNan = createArrowFunction(
Number.isNaN,
/**
* Returns whether the provided value is a safe integral number.
*
- * ※ This function is effectively an alias for `Number.isSafeInteger`.
+ * ※ This function is effectively an alias for `Number.isSafeInteger´.
*/
export const isSafeIntegralNumber = createArrowFunction(
Number.isSafeInteger,
/**
* Returns the ln of the provided value.
*
- * ※ This function is effectively an alias for `Math.log`.
+ * ※ This function is effectively an alias for `Math.log´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the ln of one plus the provided value.
*
- * ※ This function is effectively an alias for `Math.log1p`.
+ * ※ This function is effectively an alias for `Math.log1p´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the log10 of the provided value.
*
- * ※ This function is effectively an alias for `Math.log10`.
+ * ※ This function is effectively an alias for `Math.log10´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the log2 of the provided value.
*
- * ※ This function is effectively an alias for `Math.log2`.
+ * ※ This function is effectively an alias for `Math.log2´.
*
* ☡ This function does not allow big·int arguments.
*/
* Returns the highest value of the provided arguments, or negative
* infinity if no argument is provided.
*
- * ※ Unlike `Math.max`, this function accepts either number or big·int
+ * ※ Unlike `Math.max´, this function accepts either number or big·int
* values. All values must be of the same type, or this function will
* throw an error.
*
* big·int.
*/
export const max = Object.defineProperties((...$s) => {
- let highest = undefined;
+ let highest = UNDEFINED;
for (let i = 0; i < $s.length; ++i) {
// Iterate over all the numbers.
const number = toNumeric($s[i]);
- if (highest === undefined) {
+ if (highest === UNDEFINED) {
// The current number is the first one.
if (isNan(number)) {
// The current number is nan.
}
} else {
if (typeof highest !== typeof number) {
- // The type of the current number and the lowest number don’t
+ // The type of the current number and the lowest number don¦t
// match.
- throw new TypeError("Piscēs: Type mismatch.");
+ throw new TypeError(`${PISCĒS}: Type mismatch.`);
} else if (isNan(number)) {
// The current number is nan.
return NAN;
} else if (sameValue(number, 0) && sameValue(highest, -0)) {
// The current number is +0 and the highest number is -0.
highest = 0;
- } else if (highest === undefined || number > highest) {
+ } else if (number > highest) {
// The current number is greater than the highest number.
highest = number;
} else {
}
}
return highest ?? NEGATIVE_INFINITY;
-}, { name: { value: "max" }, length: { value: 2 } });
+}, {
+ name: defineOwnDataProperty(Object.create(null), "value", "max"),
+ length: defineOwnDataProperty(Object.create(null), "value", 2),
+});
/**
* Returns the lowest value of the provided arguments, or positive
* infinity if no argument is provided.
*
- * ※ Unlike `Math.min`, this function accepts either number or big·int
+ * ※ Unlike `Math.min´, this function accepts either number or big·int
* values. All values must be of the same type, or this function will
* throw an error.
*
* big·int.
*/
export const min = Object.defineProperties((...$s) => {
- let lowest = undefined;
+ let lowest = UNDEFINED;
for (let i = 0; i < $s.length; ++i) {
// Iterate over all the numbers.
const number = toNumeric($s[i]);
- if (lowest === undefined) {
+ if (lowest === UNDEFINED) {
// The current number is the first one.
if (isNan(number)) {
// The current number is nan.
} else {
// The current number is not the first one.
if (typeof lowest !== typeof number) {
- // The type of the current number and the lowest number don’t
+ // The type of the current number and the lowest number don¦t
// match.
- throw new TypeError("Piscēs: Type mismatch.");
+ throw new TypeError(`${PISCĒS}: Type mismatch.`);
} else if (isNan(number)) {
// The current number is nan.
return NAN;
}
}
return lowest ?? POSITIVE_INFINITY;
-}, { name: { value: "min" }, length: { value: 2 } });
+}, {
+ name: defineOwnDataProperty(Object.create(null), "value", "min"),
+ length: defineOwnDataProperty(Object.create(null), "value", 2),
+});
/**
* Returns a pseudo·random value in the range [0, 1).
*
- * ※ This function is effectively an alias for `Math.random`.
+ * ※ This function is effectively an alias for `Math.random´.
*/
export const rand = createArrowFunction(
Math.random,
/**
* Returns the round of the provided value.
*
- * ※ This function is effectively an alias for `Math.round`.
+ * ※ This function is effectively an alias for `Math.round´.
*
* ☡ This function does not allow big·int arguments.
*/
* that positive and negative infinity will return +1 and −1
* respectively.
*
- * ※ Unlike `Math.sign`, this function accepts either number or
+ * ※ Unlike `Math.sign´, this function accepts either number or
* big·int values.
*/
export const sgn = ($) => {
/**
* Returns the sin of the provided value.
*
- * ※ This function is effectively an alias for `Math.sin`.
+ * ※ This function is effectively an alias for `Math.sin´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the sinh of the provided value.
*
- * ※ This function is effectively an alias for `Math.sinh`.
+ * ※ This function is effectively an alias for `Math.sinh´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the square root of the provided value.
*
- * ※ This function is effectively an alias for `Math.sqrt`.
+ * ※ This function is effectively an alias for `Math.sqrt´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the tan of the provided value.
*
- * ※ This function is effectively an alias for `Math.tan`.
+ * ※ This function is effectively an alias for `Math.tan´.
*
* ☡ This function does not allow big·int arguments.
*/
/**
* Returns the tanh of the provided value.
*
- * ※ This function is effectively an alias for `Math.tanh`.
+ * ※ This function is effectively an alias for `Math.tanh´.
*
* ☡ This function does not allow big·int arguments.
*/
*
* ※ This method is safe to use with numbers.
*
- * ※ This is effectively an alias for `BigInt`.
+ * ※ This is effectively an alias for `BigInt´.
*/
export const { toBigInt } = (() => {
const makeBigInt = BigInt;
const f = toIntegralNumberOrInfinity(fractionDigits);
if (!isFiniteNumber(f) || f < 0 || f > 100) {
throw new RangeError(
- `Piscēs: The number of fractional digits must be a finite number between 0 and 100 inclusive; got: ${f}.`,
+ `${PISCĒS}: The number of fractional digits must be a finite number between 0 and 100 inclusive; got: ${f}.`,
);
} else {
if (typeof n === "number") {
return call(
numberToExponential,
n,
- [fractionDigits === undefined ? fractionDigits : f],
+ [fractionDigits === UNDEFINED ? fractionDigits : f],
);
} else {
const digits = call(bigintToString, n, [10]);
const { length } = digits;
- if (fractionDigits === undefined) {
+ if (fractionDigits === UNDEFINED) {
return length === 1
? `${digits[0]}e+0`
: `${digits[0]}.${substring(digits, 1)}e+${length - 1}`;
const f = toIntegralNumberOrInfinity(fractionDigits);
if (!isFiniteNumber(f) || f < 0 || f > 100) {
throw new RangeError(
- `Piscēs: The number of fractional digits must be a finite number between 0 and 100 inclusive; got: ${f}.`,
+ `${PISCĒS}: The number of fractional digits must be a finite number between 0 and 100 inclusive; got: ${f}.`,
);
} else {
const n = toNumeric($);
*
* ※ This function is safe to use with big·ints.
*
- * ※ This is effectively a nonconstructible version of the `Number`
+ * ※ This is effectively a nonconstructible version of the `Number´
* constructor.
*/
-export const { toNumber } = (() => {
- const makeNumber = Number;
- return { toNumber: ($) => makeNumber($) };
-})();
+export const toNumber = createArrowFunction(
+ Number,
+ { name: "toNumber" },
+);
/**
* Returns the result of converting the provided value to a number or
* Returns the result of converting the provided value to fit within
* the provided number of bits as a signed integer.
*
- * ※ Unlike `BigInt.asIntN`, this function accepts both big·int and
+ * ※ Unlike `BigInt.asIntN´, this function accepts both big·int and
* number values.
*
* ☡ The first argument, the number of bits, must be a number.
* Returns the result of converting the provided value to fit within
* the provided number of bits as an unsigned integer.
*
- * ※ Unlike `BigInt.asUintN`, this function accepts both big·int and
+ * ※ Unlike `BigInt.asUintN´, this function accepts both big·int and
* number values.
*
* ☡ The first argument, the number of bits, must be a number.
/**
* Returns the trunc of the provided value.
*
- * ※ This function is effectively an alias for `Math.trunc`.
+ * ※ This function is effectively an alias for `Math.trunc´.
*
* ☡ This function does not allow big·int arguments.
*/