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
} 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 * Returns the arccos of the provided value.
66 * ※ This is an alias for `Math.acos`.
68 * ☡ This function does not allow big·int arguments.
73 * Returns the arccosh of the provided value.
75 * ※ This is an alias for `Math.acosh`.
77 * ☡ This function does not allow big·int arguments.
82 * Returns the arcsin of the provided value.
84 * ※ This is an alias for `Math.asin`.
86 * ☡ This function does not allow big·int arguments.
91 * Returns the arcsinh of the provided value.
93 * ※ This is an alias for `Math.asinh`.
95 * ☡ This function does not allow big·int arguments.
100 * Returns the arctan of the provided value.
102 * ※ This is an alias for `Math.atan`.
104 * ☡ This function does not allow big·int arguments.
109 * Returns the arctanh of the provided value.
111 * ※ This is an alias for `Math.atanh`.
113 * ☡ This function does not allow big·int arguments.
118 * Returns the cube root of the provided value.
120 * ※ This is an alias for `Math.cbrt`.
122 * ☡ This function does not allow big·int arguments.
127 * Returns the ceiling of the provided value.
129 * ※ This is an alias for `Math.ceil`.
131 * ☡ This function does not allow big·int arguments.
136 * Returns the cos of the provided value.
138 * ※ This is an alias for `Math.cos`.
140 * ☡ This function does not allow big·int arguments.
145 * Returns the cosh of the provided value.
147 * ※ This is an alias for `Math.cosh`.
149 * ☡ This function does not allow big·int arguments.
154 * Returns the Euler number raised to the provided value.
156 * ※ This is an alias for `Math.exp`.
158 * ☡ This function does not allow big·int arguments.
163 * Returns the Euler number raised to the provided value, minus one.
165 * ※ This is an alias for `Math.expm1`.
167 * ☡ This function does not allow big·int arguments.
172 * Returns the floor of the provided value.
174 * ※ This is an alias for `Math.floor`.
176 * ☡ This function does not allow big·int arguments.
181 * Returns the square root of the sum of the squares of the provided
184 * ※ This is an alias for `Math.hypot`.
186 * ☡ This function does not allow big·int arguments.
191 * Returns the ln of the provided value.
193 * ※ This is an alias for `Math.log`.
195 * ☡ This function does not allow big·int arguments.
200 * Returns the log10 of the provided value.
202 * ※ This is an alias for `Math.log10`.
204 * ☡ This function does not allow big·int arguments.
209 * Returns the ln of one plus the provided value.
211 * ※ This is an alias for `Math.log1p`.
213 * ☡ This function does not allow big·int arguments.
218 * Returns the log2 of the provided value.
220 * ※ This is an alias for `Math.log2`.
222 * ☡ This function does not allow big·int arguments.
227 * Returns a pseudo·random value in the range [0, 1).
229 * ※ This is an alias for `Math.random`.
234 * Returns the round of the provided value.
236 * ※ This is an alias for `Math.round`.
238 * ☡ This function does not allow big·int arguments.
243 * Returns the sinh of the provided value.
245 * ※ This is an alias for `Math.sinh`.
247 * ☡ This function does not allow big·int arguments.
252 * Returns the square root of the provided value.
254 * ※ This is an alias for `Math.sqrt`.
256 * ☡ This function does not allow big·int arguments.
261 * Returns the tan of the provided value.
263 * ※ This is an alias for `Math.tan`.
265 * ☡ This function does not allow big·int arguments.
270 * Returns the tanh of the provided value.
272 * ※ This is an alias for `Math.tanh`.
274 * ☡ This function does not allow big·int arguments.
279 * Returns the trunc of the provided value.
281 * ※ This is an alias for `Math.trunc`.
283 * ☡ This function does not allow big·int arguments.
288 * The mathematical constant π.
290 * ※ This is an alias for `Math.PI`.
297 * ※ This is an alias for `Math.E`.
304 * The largest number value less than infinity.
306 * ※ This is an alias for `Number.MAX_VALUE`.
308 MAX_VALUE
: MAXIMUM_NUMBER
,
313 * ※ This is an alias for `Number.MAX_SAFE_INTEGER`.
315 MAX_SAFE_INTEGER
: MAXIMUM_SAFE_INTEGRAL_NUMBER
,
318 * The smallest number value greater than negative infinity.
320 * ※ This is an alias for `Number.MIN_VALUE`.
322 MIN_VALUE
: MINIMUM_NUMBER
,
327 * ※ This is an alias for `Number.MIN_SAFE_INTEGER`.
329 MIN_SAFE_INTEGER
: MINIMUM_SAFE_INTEGRAL_NUMBER
,
334 * ※ This is an alias for `Number.NEGATIVE_INFINITY`.
341 * ※ This is an alias for `Number.NaN`.
348 * ※ This is an alias for `Number.POSITIVE_INFINITY`.
353 * The difference between 1 and the smallest number greater than 1.
355 * ※ This is an alias for `Number.EPSILON`.
360 * Returns whether the provided value is a finite number.
362 * ※ This is an alias for `Number.isFinite`.
364 isFinite
: isFiniteNumber
,
367 * Returns whether the provided value is an integral number.
369 * ※ This is an alias for `Number.isInteger`.
371 isInteger
: isIntegralNumber
,
374 * Returns whether the provided value is nan.
376 * ※ This is an alias for `Number.isNaN`.
381 * Returns whether the provided value is a safe integral number.
383 * ※ This is an alias for `Number.isSafeInteger`.
385 isSafeInteger
: isSafeIntegralNumber
,
388 /** Positive zero. */
389 export const POSITIVE_ZERO
= 0;
391 /** Negative zero. */
392 export const NEGATIVE_ZERO
= -0;
395 * Returns the magnitude (absolute value) of the provided value.
397 * ※ Unlike `Math.abs`, this function can take big·int arguments.
399 export const abs
= ($) => {
400 const n
= toNumeric($);
401 return typeof n
=== "bigint"
407 : sameValue(n
, NEGATIVE_INFINITY
)
416 * Returns the arctangent of the dividend of the provided values.
418 * ※ Unlike `Math.atan2`, this function can take big·int arguments.
419 * However, the result will always be a number.
424 * Returns the number of leading zeroes in the 32‐bit representation
425 * of the provided value.
427 * ※ Unlike `Math.clz32`, this function accepts either number or
433 * Returns the 32‐bit float which best approximate the provided
436 * ※ Unlike `Math.fround`, this function can take big·int arguments.
437 * However, the result will always be a number.
441 const { atan2
, fround
, clz32
} = Math
;
443 atan2
: (y
, x
) => atan2(toNumber(y
), toNumber(x
)),
445 const n
= toNumeric($);
447 typeof n
=== "bigint"
448 ? toNumber(toUnsignedIntegralNumeric(32, n
))
452 toFloat32
: ($) => fround(toNumber($)),
457 * Returns the highest value of the provided arguments, or negative
458 * infinity if no argument is provided.
460 * ※ Unlike `Math.max`, this function accepts either number or big·int
461 * values. All values must be of the same type, or this function will
464 * ☡ If no argument is supplied, the result will be a number, not a
467 export const max
= (...$s
) => {
468 let highest
= undefined;
469 for (let i
= 0; i
< $s
.length
; ++i
) {
470 // Iterate over all the numbers.
471 const number
= toNumeric($s
[i
]);
472 if (highest
=== undefined) {
473 // The current number is the first one.
475 // The current number is nan.
478 // The current number is not nan.
482 if (typeof highest
!== typeof number
) {
483 // The type of the current number and the lowest number don’t
485 throw new TypeError("Piscēs: Type mismatch.");
486 } else if (isNan(number
)) {
487 // The current number is nan.
489 } else if (sameValue(number
, 0) && sameValue(highest
, -0)) {
490 // The current number is +0 and the highest number is -0.
492 } else if (highest
=== undefined || number
> highest
) {
493 // The current number is greater than the highest number.
496 // The current number is less than or equal to the lowest
502 return highest
?? NEGATIVE_INFINITY
;
506 * Returns the lowest value of the provided arguments, or positive
507 * infinity if no argument is provided.
509 * ※ Unlike `Math.min`, this function accepts either number or big·int
510 * values. All values must be of the same type, or this function will
513 * ☡ If no argument is supplied, the result will be a number, not a
516 export const min
= (...$s
) => {
517 let lowest
= undefined;
518 for (let i
= 0; i
< $s
.length
; ++i
) {
519 // Iterate over all the numbers.
520 const number
= toNumeric($s
[i
]);
521 if (lowest
=== undefined) {
522 // The current number is the first one.
524 // The current number is nan.
527 // The current number is not nan.
531 // The current number is not the first one.
532 if (typeof lowest
!== typeof number
) {
533 // The type of the current number and the lowest number don’t
535 throw new TypeError("Piscēs: Type mismatch.");
536 } else if (isNan(number
)) {
537 // The current number is nan.
539 } else if (sameValue(number
, -0) && sameValue(lowest
, 0)) {
540 // The current number is -0 and the lowest number is +0.
542 } else if (number
< lowest
) {
543 // The current number is less than the lowest number.
546 // The current number is greater than or equal to the lowest
552 return lowest
?? POSITIVE_INFINITY
;
556 * Returns a unit value with the same sign as the provided value, or
557 * the provided value itself if it is not a number or (potentially
560 * For big·ints, the return value of this function is 0n if the
561 * provided value is 0n, −1n if the provided value is negative, and +1n
564 * For numbers, the return value is nan, −0, or +0 if the provided
565 * value is nan, −0, or +0, respectively, and −1 if the provided value
566 * is negative and +1 if the provided value is positive otherwise. Note
567 * that positive and negative infinity will return +1 and −1
570 * ※ Unlike `Math.sign`, this function accepts either number or
573 export const sgn
= ($) => {
574 const n
= toNumeric($);
575 return typeof n
=== "bigint"
576 ? n
=== 0n
? 0n
: n
< 0n
? -1n
: 1n
577 : isNan(n
) || n
=== 0
579 //deno-lint-ignore no-compare-neg-zero
586 * Returns the result of converting the provided value to a big·int.
588 * ※ This method is safe to use with numbers.
590 * ※ This is effectively an alias for `BigInt`.
592 export const { toBigInt
} = (() => {
593 const makeBigInt
= BigInt
;
594 return { toBigInt
: ($) => makeBigInt($) };
599 * Returns the result of converting the provided value to an
600 * exponential notation string.
602 * If a second argument is provided, it gives the number of
603 * fractional digits to use in the mantissa. Otherwise, the smallest
604 * number which does not result in a reduction in precision is used.
606 * ※ This method is safe to use with big·ints.
608 toExponentialNotation
,
611 * Returns the result of converting the provided value to a fixed
612 * decimal notation string with the provided number of fractional
615 * ※ This method is safe to use with big·ints.
617 toFixedDecimalNotation
,
620 toExponential
: numberToExponential
,
621 toFixed
: numberToFixed
,
622 } = Number
.prototype;
623 const { toString
: bigintToString
} = BigInt
.prototype;
625 toExponentialNotation
: ($, fractionDigits
) => {
626 const n
= toNumeric($);
627 const f
= toIntegralNumberOrInfinity(fractionDigits
);
628 if (!isFiniteNumber(f
) || f
< 0 || f
> 100) {
629 throw new RangeError(
630 `Piscēs: The number of fractional digits must be a finite number between 0 and 100 inclusive; got: ${f}.`,
633 if (typeof n
=== "number") {
637 [fractionDigits
=== undefined ? fractionDigits
: f
],
640 const digits
= call(bigintToString
, n
, [10]);
641 const { length
} = digits
;
642 if (fractionDigits
=== undefined) {
645 : `${digits[0]}.${substring(digits, 1)}e+${length - 1}`;
646 } else if (f
=== 0) {
647 return `${digits[0]}e+0`;
649 const fractionalPart
= toString(
652 stringPadEnd(substring(digits
, 1, f
+ 1), f
, "0"),
654 digits
[f
+ 1] || "0",
658 return `${digits[0]}.${fractionalPart}e+${length - 1}`;
663 toFixedDecimalNotation
: ($, fractionDigits
) => {
664 const f
= toIntegralNumberOrInfinity(fractionDigits
);
665 if (!isFiniteNumber(f
) || f
< 0 || f
> 100) {
666 throw new RangeError(
667 `Piscēs: The number of fractional digits must be a finite number between 0 and 100 inclusive; got: ${f}.`,
670 const n
= toNumeric($);
671 if (typeof n
=== "number") {
672 return call(numberToFixed
, n
, [f
]);
674 const digits
= call(bigintToString
, n
, [10]);
677 : `${digits}.${stringRepeat("0", f)}`;
685 * Returns the result of converting the provided number to an integral
688 * ※ This function will never return negative zero.
690 export const toIntegralNumber
= ($) => {
691 const n
= toIntegralNumberOrInfinity($);
692 return !isFiniteNumber(n
) || n
== 0 ? 0 : n
;
696 * Returns the result of converting the provided number to an integer
699 * ※ Unlike the ToIntegerOrInfinity function defined in the Ecmascript
700 * specification, this function is safe to use with big·ints. However,
701 * the result will always be a number.
703 * ※ This function will never return negative zero.
705 export const toIntegralNumberOrInfinity
= ($) => {
706 const integer
= trunc(toNumber($));
707 if (isNan(integer
) || integer
== 0) {
708 // The provided value truncs to nan or (positive or negative) zero.
710 } else if (integer
== POSITIVE_INFINITY
) {
711 // The provided value truncs to positive infinity.
712 return POSITIVE_INFINITY
;
713 } else if (integer
== NEGATIVE_INFINITY
) {
714 // The provided value truncs to negative infinity.
715 return NEGATIVE_INFINITY
;
717 // The provided value truncs to an integer.
723 * Returns the result of converting the provided value to a number.
725 * ※ This function is safe to use with big·ints.
727 * ※ This is effectively a nonconstructible version of the `Number`
730 export const { toNumber
} = (() => {
731 const makeNumber
= Number
;
732 return { toNumber
: ($) => makeNumber($) };
736 * Returns the result of converting the provided value to a number or
739 * ※ If the result of converting the provided value to a primitive is
740 * not a big·int, this function will return a number.
742 export const toNumeric
= ($) => {
743 const primValue
= toPrimitive($, "number");
744 return typeof primValue
=== "bigint" ? primValue
: +primValue
;
749 * Returns the result of converting the provided value to fit within
750 * the provided number of bits as a signed integer.
752 * ※ Unlike `BigInt.asIntN`, this function accepts both big·int and
755 * ☡ The first argument, the number of bits, must be a number.
757 toSignedIntegralNumeric
,
760 * Returns the result of converting the provided value to fit within
761 * the provided number of bits as an unsigned integer.
763 * ※ Unlike `BigInt.asUintN`, this function accepts both big·int and
766 * ☡ The first argument, the number of bits, must be a number.
768 toUnsignedIntegralNumeric
,
770 const { asIntN
, asUintN
} = BigInt
;
772 toSignedIntegralNumeric
: (n
, $) => {
773 const prim
= toPrimitive($);
774 if (typeof prim
=== "bigint") {
775 // The primitive value is a big·int.
776 return asIntN(n
, prim
);
778 // The primitive value is not a big·int.
779 const int = trunc(prim
);
780 if (!isFiniteNumber(int) || int == 0) {
781 // The truncated value is zero or not finite.
784 // The truncated value is finite.
785 return toNumber(asIntN(n
, toBigInt(int)));
789 toUnsignedIntegralNumeric
: (n
, $) => {
790 const prim
= toPrimitive($);
791 if (typeof prim
=== "bigint") {
792 // The primitive value is a big·int.
793 return asUintN(n
, prim
);
795 // The primitive value is not a big·int.
796 const int = trunc(prim
);
797 if (!isFiniteNumber(int) || int == 0) {
798 // The truncated value is zero or not finite.
801 // The truncated value is finite.
802 return toNumber(asUintN(n
, toBigInt(int)));