1 // ♓🌟 Piscēs ∷ numeric.js
2 // ====================================================================
4 // Copyright © 2022–2023 Lady [@ Lady’s Computer].
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
10 import { call
, createArrowFunction
} from "./function.js";
18 import { sameValue
, toPrimitive
} from "./value.js";
24 * ※ This is an alias for `Math.LN10`.
31 * ※ This is an alias for `Math.LN2`.
38 * ※ This is an alias for `Math.LOG10E`.
45 * ※ This is an alias for `Math.LOG2E`.
52 * ※ This is an alias for `Math.SQRT1_2`.
54 SQRT1_2
: RECIPROCAL_SQRT2
,
59 * ※ This is an alias for `Math.SQRT2`.
64 * The mathematical constant π.
66 * ※ This is an alias for `Math.PI`.
73 * ※ This is an alias for `Math.E`.
80 * The largest number value less than infinity.
82 * ※ This is an alias for `Number.MAX_VALUE`.
84 MAX_VALUE
: MAXIMUM_NUMBER
,
89 * ※ This is an alias for `Number.MAX_SAFE_INTEGER`.
91 MAX_SAFE_INTEGER
: MAXIMUM_SAFE_INTEGRAL_NUMBER
,
94 * The smallest number value greater than negative infinity.
96 * ※ This is an alias for `Number.MIN_VALUE`.
98 MIN_VALUE
: MINIMUM_NUMBER
,
103 * ※ This is an alias for `Number.MIN_SAFE_INTEGER`.
105 MIN_SAFE_INTEGER
: MINIMUM_SAFE_INTEGRAL_NUMBER
,
110 * ※ This is an alias for `Number.NEGATIVE_INFINITY`.
117 * ※ This is an alias for `Number.NaN`.
124 * ※ This is an alias for `Number.POSITIVE_INFINITY`.
129 * The difference between 1 and the smallest number greater than 1.
131 * ※ This is an alias for `Number.EPSILON`.
136 /** Negative zero. */
137 export const NEGATIVE_ZERO
= -0;
139 /** Positive zero. */
140 export const POSITIVE_ZERO
= 0;
143 * Returns the magnitude (absolute value) of the provided value.
145 * ※ Unlike `Math.abs`, this function can take big·int arguments.
147 export const abs
= ($) => {
148 const n
= toNumeric($);
149 return typeof n
=== "bigint"
155 : sameValue(n
, NEGATIVE_INFINITY
)
163 * Returns the arccos of the provided value.
165 * ※ This function is effectively an alias for `Math.acos`.
167 * ☡ This function does not allow big·int arguments.
169 export const arccos
= createArrowFunction(
175 * Returns the arccosh of the provided value.
177 * ※ This function is effectively an alias for `Math.acosh`.
179 * ☡ This function does not allow big·int arguments.
181 export const arccosh
= createArrowFunction(
187 * Returns the arcsin of the provided value.
189 * ※ This function is effectively an alias for `Math.asin`.
191 * ☡ This function does not allow big·int arguments.
193 export const arcsin
= createArrowFunction(
199 * Returns the arcsinh of the provided value.
201 * ※ This function is effectively an alias for `Math.asinh`.
203 * ☡ This function does not allow big·int arguments.
205 export const arcsinh
= createArrowFunction(
211 * Returns the arctan of the provided value.
213 * ※ This function is effectively an alias for `Math.atan`.
215 * ☡ This function does not allow big·int arguments.
217 export const arctan
= createArrowFunction(
224 * Returns the arctangent of the dividend of the provided values.
226 * ※ Unlike `Math.atan2`, this function can take big·int arguments.
227 * However, the result will always be a number.
232 * Returns the number of leading zeroes in the 32‐bit representation
233 * of the provided value.
235 * ※ Unlike `Math.clz32`, this function accepts either number or
241 * Returns the 32‐bit float which best approximate the provided
244 * ※ Unlike `Math.fround`, this function can take big·int arguments.
245 * However, the result will always be a number.
249 const { atan2
, fround
, clz32
} = Math
;
251 arctan2
: (y
, x
) => atan2(toNumber(y
), toNumber(x
)),
253 const n
= toNumeric($);
255 typeof n
=== "bigint"
256 ? toNumber(toUnsignedIntegralNumeric(32, n
))
260 toFloat32
: ($) => fround(toNumber($)),
265 * Returns the arctanh of the provided value.
267 * ※ This function is effectively an alias for `Math.atanh`.
269 * ☡ This function does not allow big·int arguments.
271 export const arctanh
= createArrowFunction(
277 * Returns the cube root of the provided value.
279 * ※ This function is effectively an alias for `Math.cbrt`.
281 * ☡ This function does not allow big·int arguments.
283 export const cbrt
= createArrowFunction(Math
.cbrt
);
286 * Returns the ceiling of the provided value.
288 * ※ This function is effectively an alias for `Math.ceil`.
290 * ☡ This function does not allow big·int arguments.
292 export const ceil
= createArrowFunction(Math
.ceil
);
295 * Returns the cos of the provided value.
297 * ※ This function is effectively an alias for `Math.cos`.
299 * ☡ This function does not allow big·int arguments.
301 export const cos
= createArrowFunction(Math
.cos
);
304 * Returns the cosh of the provided value.
306 * ※ This function is effectively an alias for `Math.cosh`.
308 * ☡ This function does not allow big·int arguments.
310 export const cosh
= createArrowFunction(Math
.cosh
);
313 * Returns the Euler number raised to the provided value.
315 * ※ This function is effectively an alias for `Math.exp`.
317 * ☡ This function does not allow big·int arguments.
319 export const exp
= createArrowFunction(Math
.exp
);
322 * Returns the Euler number raised to the provided value, minus one.
324 * ※ This function is effectively an alias for `Math.expm1`.
326 * ☡ This function does not allow big·int arguments.
328 export const expm1
= createArrowFunction(Math
.expm1
);
331 * Returns the floor of the provided value.
333 * ※ This function is effectively an alias for `Math.floor`.
335 * ☡ This function does not allow big·int arguments.
337 export const floor
= createArrowFunction(Math
.floor
);
340 * Returns the square root of the sum of the squares of the provided
343 * ※ This function is effectively an alias for `Math.hypot`.
345 * ☡ This function does not allow big·int arguments.
347 export const hypot
= createArrowFunction(Math
.hypot
);
350 * Returns whether the provided value is a finite number.
352 * ※ This function is effectively an alias for `Number.isFinite`.
354 export const isFiniteNumber
= createArrowFunction(
356 { name
: "isFiniteNumber" },
360 * Returns whether the provided value is an integral number.
362 * ※ This function is effectively an alias for `Number.isInteger`.
364 export const isIntegralNumber
= createArrowFunction(
366 { name
: "isIntegralNumber" },
370 * Returns whether the provided value is nan.
372 * ※ This function is effectively an alias for `Number.isNaN`.
374 export const isNan
= createArrowFunction(
380 * Returns whether the provided value is a safe integral number.
382 * ※ This function is effectively an alias for `Number.isSafeInteger`.
384 export const isSafeIntegralNumber
= createArrowFunction(
385 Number
.isSafeInteger
,
386 { name
: "isSafeIntegralNumber" },
390 * Returns the ln of the provided value.
392 * ※ This function is effectively an alias for `Math.log`.
394 * ☡ This function does not allow big·int arguments.
396 export const ln
= createArrowFunction(Math
.log
, { name
: "ln" });
399 * Returns the ln of one plus the provided value.
401 * ※ This function is effectively an alias for `Math.log1p`.
403 * ☡ This function does not allow big·int arguments.
405 export const ln1p
= createArrowFunction(Math
.log1p
, { name
: "ln1p" });
408 * Returns the log10 of the provided value.
410 * ※ This function is effectively an alias for `Math.log10`.
412 * ☡ This function does not allow big·int arguments.
414 export const log10
= createArrowFunction(Math
.log10
);
417 * Returns the log2 of the provided value.
419 * ※ This function is effectively an alias for `Math.log2`.
421 * ☡ This function does not allow big·int arguments.
423 export const log2
= createArrowFunction(Math
.log2
);
426 * Returns the highest value of the provided arguments, or negative
427 * infinity if no argument is provided.
429 * ※ Unlike `Math.max`, this function accepts either number or big·int
430 * values. All values must be of the same type, or this function will
433 * ☡ If no argument is supplied, the result will be a number, not a
436 export const max
= Object
.defineProperties((...$s
) => {
437 let highest
= undefined;
438 for (let i
= 0; i
< $s
.length
; ++i
) {
439 // Iterate over all the numbers.
440 const number
= toNumeric($s
[i
]);
441 if (highest
=== undefined) {
442 // The current number is the first one.
444 // The current number is nan.
447 // The current number is not nan.
451 if (typeof highest
!== typeof number
) {
452 // The type of the current number and the lowest number don’t
454 throw new TypeError("Piscēs: Type mismatch.");
455 } else if (isNan(number
)) {
456 // The current number is nan.
458 } else if (sameValue(number
, 0) && sameValue(highest
, -0)) {
459 // The current number is +0 and the highest number is -0.
461 } else if (highest
=== undefined || number
> highest
) {
462 // The current number is greater than the highest number.
465 // The current number is less than or equal to the lowest
471 return highest
?? NEGATIVE_INFINITY
;
472 }, { name
: { value
: "max" }, length
: { value
: 2 } });
475 * Returns the lowest value of the provided arguments, or positive
476 * infinity if no argument is provided.
478 * ※ Unlike `Math.min`, this function accepts either number or big·int
479 * values. All values must be of the same type, or this function will
482 * ☡ If no argument is supplied, the result will be a number, not a
485 export const min
= Object
.defineProperties((...$s
) => {
486 let lowest
= undefined;
487 for (let i
= 0; i
< $s
.length
; ++i
) {
488 // Iterate over all the numbers.
489 const number
= toNumeric($s
[i
]);
490 if (lowest
=== undefined) {
491 // The current number is the first one.
493 // The current number is nan.
496 // The current number is not nan.
500 // The current number is not the first one.
501 if (typeof lowest
!== typeof number
) {
502 // The type of the current number and the lowest number don’t
504 throw new TypeError("Piscēs: Type mismatch.");
505 } else if (isNan(number
)) {
506 // The current number is nan.
508 } else if (sameValue(number
, -0) && sameValue(lowest
, 0)) {
509 // The current number is -0 and the lowest number is +0.
511 } else if (number
< lowest
) {
512 // The current number is less than the lowest number.
515 // The current number is greater than or equal to the lowest
521 return lowest
?? POSITIVE_INFINITY
;
522 }, { name
: { value
: "min" }, length
: { value
: 2 } });
525 * Returns a pseudo·random value in the range [0, 1).
527 * ※ This function is effectively an alias for `Math.random`.
529 export const rand
= createArrowFunction(
535 * Returns the round of the provided value.
537 * ※ This function is effectively an alias for `Math.round`.
539 * ☡ This function does not allow big·int arguments.
541 export const round
= createArrowFunction(Math
.round
);
544 * Returns a unit value with the same sign as the provided value, or
545 * the provided value itself if it is not a number or (potentially
548 * For big·ints, the return value of this function is 0n if the
549 * provided value is 0n, −1n if the provided value is negative, and +1n
552 * For numbers, the return value is nan, −0, or +0 if the provided
553 * value is nan, −0, or +0, respectively, and −1 if the provided value
554 * is negative and +1 if the provided value is positive otherwise. Note
555 * that positive and negative infinity will return +1 and −1
558 * ※ Unlike `Math.sign`, this function accepts either number or
561 export const sgn
= ($) => {
562 const n
= toNumeric($);
563 return typeof n
=== "bigint"
564 ? n
=== 0n
? 0n
: n
< 0n
? -1n
: 1n
565 : isNan(n
) || n
=== 0
567 //deno-lint-ignore no-compare-neg-zero
574 * Returns the sin of the provided value.
576 * ※ This function is effectively an alias for `Math.sin`.
578 * ☡ This function does not allow big·int arguments.
580 export const sin
= createArrowFunction(Math
.sin
);
583 * Returns the sinh of the provided value.
585 * ※ This function is effectively an alias for `Math.sinh`.
587 * ☡ This function does not allow big·int arguments.
589 export const sinh
= createArrowFunction(Math
.sinh
);
592 * Returns the square root of the provided value.
594 * ※ This function is effectively an alias for `Math.sqrt`.
596 * ☡ This function does not allow big·int arguments.
598 export const sqrt
= createArrowFunction(Math
.sqrt
);
601 * Returns the tan of the provided value.
603 * ※ This function is effectively an alias for `Math.tan`.
605 * ☡ This function does not allow big·int arguments.
607 export const tan
= createArrowFunction(Math
.tan
);
610 * Returns the tanh of the provided value.
612 * ※ This function is effectively an alias for `Math.tanh`.
614 * ☡ This function does not allow big·int arguments.
616 export const tanh
= createArrowFunction(Math
.tanh
);
619 * Returns the result of converting the provided value to a big·int.
621 * ※ This method is safe to use with numbers.
623 * ※ This is effectively an alias for `BigInt`.
625 export const { toBigInt
} = (() => {
626 const makeBigInt
= BigInt
;
627 return { toBigInt
: ($) => makeBigInt($) };
632 * Returns the result of converting the provided value to an
633 * exponential notation string.
635 * If a second argument is provided, it gives the number of
636 * fractional digits to use in the mantissa. Otherwise, the smallest
637 * number which does not result in a reduction in precision is used.
639 * ※ This method is safe to use with big·ints.
641 toExponentialNotation
,
644 * Returns the result of converting the provided value to a fixed
645 * decimal notation string with the provided number of fractional
648 * ※ This method is safe to use with big·ints.
650 toFixedDecimalNotation
,
653 toExponential
: numberToExponential
,
654 toFixed
: numberToFixed
,
655 } = Number
.prototype;
656 const { toString
: bigintToString
} = BigInt
.prototype;
658 toExponentialNotation
: ($, fractionDigits
) => {
659 const n
= toNumeric($);
660 const f
= toIntegralNumberOrInfinity(fractionDigits
);
661 if (!isFiniteNumber(f
) || f
< 0 || f
> 100) {
662 throw new RangeError(
663 `Piscēs: The number of fractional digits must be a finite number between 0 and 100 inclusive; got: ${f}.`,
666 if (typeof n
=== "number") {
670 [fractionDigits
=== undefined ? fractionDigits
: f
],
673 const digits
= call(bigintToString
, n
, [10]);
674 const { length
} = digits
;
675 if (fractionDigits
=== undefined) {
678 : `${digits[0]}.${substring(digits, 1)}e+${length - 1}`;
679 } else if (f
=== 0) {
680 return `${digits[0]}e+0`;
682 const fractionalPart
= toString(
685 stringPadEnd(substring(digits
, 1, f
+ 1), f
, "0"),
687 digits
[f
+ 1] || "0",
691 return `${digits[0]}.${fractionalPart}e+${length - 1}`;
696 toFixedDecimalNotation
: ($, fractionDigits
) => {
697 const f
= toIntegralNumberOrInfinity(fractionDigits
);
698 if (!isFiniteNumber(f
) || f
< 0 || f
> 100) {
699 throw new RangeError(
700 `Piscēs: The number of fractional digits must be a finite number between 0 and 100 inclusive; got: ${f}.`,
703 const n
= toNumeric($);
704 if (typeof n
=== "number") {
705 return call(numberToFixed
, n
, [f
]);
707 const digits
= call(bigintToString
, n
, [10]);
710 : `${digits}.${stringRepeat("0", f)}`;
718 * Returns the result of converting the provided number to an integral
721 * ※ This function will never return negative zero.
723 export const toIntegralNumber
= ($) => {
724 const n
= toIntegralNumberOrInfinity($);
725 return !isFiniteNumber(n
) || n
== 0 ? 0 : n
;
729 * Returns the result of converting the provided number to an integer
732 * ※ Unlike the ToIntegerOrInfinity function defined in the Ecmascript
733 * specification, this function is safe to use with big·ints. However,
734 * the result will always be a number.
736 * ※ This function will never return negative zero.
738 export const toIntegralNumberOrInfinity
= ($) => {
739 const integer
= trunc(toNumber($));
740 if (isNan(integer
) || integer
== 0) {
741 // The provided value truncs to nan or (positive or negative) zero.
743 } else if (integer
== POSITIVE_INFINITY
) {
744 // The provided value truncs to positive infinity.
745 return POSITIVE_INFINITY
;
746 } else if (integer
== NEGATIVE_INFINITY
) {
747 // The provided value truncs to negative infinity.
748 return NEGATIVE_INFINITY
;
750 // The provided value truncs to an integer.
756 * Returns the result of converting the provided value to a number.
758 * ※ This function is safe to use with big·ints.
760 * ※ This is effectively a nonconstructible version of the `Number`
763 export const { toNumber
} = (() => {
764 const makeNumber
= Number
;
765 return { toNumber
: ($) => makeNumber($) };
769 * Returns the result of converting the provided value to a number or
772 * ※ If the result of converting the provided value to a primitive is
773 * not a big·int, this function will return a number.
775 export const toNumeric
= ($) => {
776 const primValue
= toPrimitive($, "number");
777 return typeof primValue
=== "bigint" ? primValue
: +primValue
;
782 * Returns the result of converting the provided value to fit within
783 * the provided number of bits as a signed integer.
785 * ※ Unlike `BigInt.asIntN`, this function accepts both big·int and
788 * ☡ The first argument, the number of bits, must be a number.
790 toSignedIntegralNumeric
,
793 * Returns the result of converting the provided value to fit within
794 * the provided number of bits as an unsigned integer.
796 * ※ Unlike `BigInt.asUintN`, this function accepts both big·int and
799 * ☡ The first argument, the number of bits, must be a number.
801 toUnsignedIntegralNumeric
,
803 const { asIntN
, asUintN
} = BigInt
;
805 toSignedIntegralNumeric
: (n
, $) => {
806 const prim
= toPrimitive($);
807 if (typeof prim
=== "bigint") {
808 // The primitive value is a big·int.
809 return asIntN(n
, prim
);
811 // The primitive value is not a big·int.
812 const int = trunc(prim
);
813 if (!isFiniteNumber(int) || int == 0) {
814 // The truncated value is zero or not finite.
817 // The truncated value is finite.
818 return toNumber(asIntN(n
, toBigInt(int)));
822 toUnsignedIntegralNumeric
: (n
, $) => {
823 const prim
= toPrimitive($);
824 if (typeof prim
=== "bigint") {
825 // The primitive value is a big·int.
826 return asUintN(n
, prim
);
828 // The primitive value is not a big·int.
829 const int = trunc(prim
);
830 if (!isFiniteNumber(int) || int == 0) {
831 // The truncated value is zero or not finite.
834 // The truncated value is finite.
835 return toNumber(asUintN(n
, toBigInt(int)));
843 * Returns the trunc of the provided value.
845 * ※ This function is effectively an alias for `Math.trunc`.
847 * ☡ This function does not allow big·int arguments.
849 export const trunc
= createArrowFunction(Math
.trunc
);