1 // ♓🌟 Piscēs ∷ numeric.js
2 // ====================================================================
4 // Copyright © 2022 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 { sameValue
, toPrimitive
} from "./value.js";
16 * ※ This is an alias for Math.LN10.
23 * ※ This is an alias for Math.LN2.
30 * ※ This is an alias for Math.LOG10E.
37 * ※ This is an alias for Math.LOG2E.
44 * ※ This is an alias for Math.SQRT1_2.
46 SQRT1_2
: RECIPROCAL_SQRT2
,
51 * ※ This is an alias for Math.SQRT2.
56 * Returns the arccos of the provided value.
58 * ※ This is an alias for Math.acos.
60 * ☡ This function does not allow big·int arguments.
65 * Returns the arccosh of the provided value.
67 * ※ This is an alias for Math.acosh.
69 * ☡ This function does not allow big·int arguments.
74 * Returns the arcsin of the provided value.
76 * ※ This is an alias for Math.asin.
78 * ☡ This function does not allow big·int arguments.
83 * Returns the arcsinh of the provided value.
85 * ※ This is an alias for Math.asinh.
87 * ☡ This function does not allow big·int arguments.
92 * Returns the arctan of the provided value.
94 * ※ This is an alias for Math.atan.
96 * ☡ This function does not allow big·int arguments.
101 * Returns the arctanh of the provided value.
103 * ※ This is an alias for Math.atanh.
105 * ☡ This function does not allow big·int arguments.
110 * Returns the cube root of the provided value.
112 * ※ This is an alias for Math.cbrt.
114 * ☡ This function does not allow big·int arguments.
119 * Returns the ceiling of the provided value.
121 * ※ This is an alias for Math.ceil.
123 * ☡ This function does not allow big·int arguments.
128 * Returns the cos of the provided value.
130 * ※ This is an alias for Math.cos.
132 * ☡ This function does not allow big·int arguments.
137 * Returns the cosh of the provided value.
139 * ※ This is an alias for Math.cosh.
141 * ☡ This function does not allow big·int arguments.
146 * Returns the Euler number raised to the provided value.
148 * ※ This is an alias for Math.exp.
150 * ☡ This function does not allow big·int arguments.
155 * Returns the Euler number raised to the provided value, minus one.
157 * ※ This is an alias for Math.expm1.
159 * ☡ This function does not allow big·int arguments.
164 * Returns the floor of the provided value.
166 * ※ This is an alias for Math.floor.
168 * ☡ This function does not allow big·int arguments.
173 * Returns the square root of the sum of the squares of the provided
176 * ※ This is an alias for Math.hypot.
178 * ☡ This function does not allow big·int arguments.
183 * Returns the ln of the provided value.
185 * ※ This is an alias for Math.log.
187 * ☡ This function does not allow big·int arguments.
192 * Returns the log10 of the provided value.
194 * ※ This is an alias for Math.log10.
196 * ☡ This function does not allow big·int arguments.
201 * Returns the ln of one plus the provided value.
203 * ※ This is an alias for Math.log1p.
205 * ☡ This function does not allow big·int arguments.
210 * Returns the log2 of the provided value.
212 * ※ This is an alias for Math.log2.
214 * ☡ This function does not allow big·int arguments.
219 * Returns a pseudo·random value in the range [0, 1).
221 * ※ This is an alias for Math.random.
226 * Returns the round of the provided value.
228 * ※ This is an alias for Math.round.
230 * ☡ This function does not allow big·int arguments.
235 * Returns the sinh of the provided value.
237 * ※ This is an alias for Math.sinh.
239 * ☡ This function does not allow big·int arguments.
244 * Returns the square root of the provided value.
246 * ※ This is an alias for Math.sqrt.
248 * ☡ This function does not allow big·int arguments.
253 * Returns the tan of the provided value.
255 * ※ This is an alias for Math.tan.
257 * ☡ This function does not allow big·int arguments.
262 * Returns the tanh of the provided value.
264 * ※ This is an alias for Math.tanh.
266 * ☡ This function does not allow big·int arguments.
271 * Returns the trunc of the provided value.
273 * ※ This is an alias for Math.trunc.
275 * ☡ This function does not allow big·int arguments.
280 * The mathematical constant π.
282 * ※ This is an alias for Math.PI.
289 * ※ This is an alias for Math.E.
296 * The largest number value less than infinity.
298 * ※ This is an alias for Number.MAX_VALUE.
300 MAX_VALUE
: MAXIMUM_NUMBER
,
305 * ※ This is an alias for Number.MAX_SAFE_INTEGER.
307 MAX_SAFE_INTEGER
: MAXIMUM_SAFE_INTEGRAL_NUMBER
,
310 * The smallest number value greater than negative infinity.
312 * ※ This is an alias for Number.MIN_VALUE.
314 MIN_VALUE
: MINIMUM_NUMBER
,
319 * ※ This is an alias for Number.MIN_SAFE_INTEGER.
321 MIN_SAFE_INTEGER
: MINIMUM_SAFE_INTEGRAL_NUMBER
,
326 * ※ This is an alias for Number.NEGATIVE_INFINITY.
333 * ※ This is an alias for Number.NaN.
340 * ※ This is an alias for Number.POSITIVE_INFINITY.
345 * The difference between 1 and the smallest number greater than 1.
347 * ※ This is an alias for Number.EPSILON.
352 * Returns whether the provided value is a finite number.
354 * ※ This is an alias for Number.isFinite.
356 isFinite
: isFiniteNumber
,
359 * Returns whether the provided value is an integral number.
361 * ※ This is an alias for Number.isInteger.
363 isInteger
: isIntegralNumber
,
366 * Returns whether the provided value is nan.
368 * ※ This is an alias for Number.isNaN.
373 * Returns whether the provided value is a safe integral number.
375 * ※ This is an alias for Number.isSafeInteger.
377 isSafeInteger
: isSafeIntegralNumber
,
381 * Returns the magnitude (absolute value) of the provided value.
383 * ※ Unlike Math.abs, this function can take big·int arguments.
385 export const abs
= ($) => {
386 const n
= toNumeric($);
387 return typeof n
=== "bigint"
393 : sameValue(n
, NEGATIVE_INFINITY
)
402 * Returns the arctangent of the dividend of the provided values.
404 * ※ Unlike Math.atan2, this function can take big·int arguments.
405 * However, the result will always be a number.
410 * Returns the number of leading zeroes in the 32‐bit representation of
411 * the provided value.
413 * ※ Unlike Math.clz32, this function accepts either number or big·int
419 * Returns the 32‐bit float which best approximate the provided
422 * ※ Unlike Math.fround, this function can take big·int arguments.
423 * However, the result will always be a number.
427 const { atan2
, fround
, clz32
} = Math
;
429 atan2
: (y
, x
) => atan2(toNumber(y
), toNumber(x
)),
431 const n
= toNumeric($);
433 typeof n
=== "bigint" ? toNumber(toUintN(32, n
)) : n
,
436 toFloat32
: ($) => fround(toNumber($)),
441 * Returns the highest value of the provided arguments, or negative
442 * infinity if no argument is provided.
444 * ※ Unlike Math.max, this function accepts either number or big·int
445 * values. All values must be of the same type, or this function will
448 * ☡ If no argument is supplied, the result will be a number, not a
451 export const max
= (...$s
) => {
452 let highest
= undefined;
453 for (let i
= 0; i
< $s
.length
; ++i
) {
454 // Iterate over all the numbers.
455 const number
= toNumeric($s
[i
]);
456 if (highest
=== undefined) {
457 // The current number is the first one.
459 // The current number is nan.
462 // The current number is not nan.
466 if (typeof highest
!== typeof number
) {
467 // The type of the current number and the lowest number don’t
469 throw new TypeError("Piscēs: Type mismatch.");
470 } else if (isNan(number
)) {
471 // The current number is nan.
473 } else if (sameValue(number
, 0) && sameValue(highest
, -0)) {
474 // The current number is +0 and the highest number is -0.
476 } else if (highest
=== undefined || number
> highest
) {
477 // The current number is greater than the highest number.
480 // The current number is less than or equal to the lowest
486 return highest
?? NEGATIVE_INFINITY
;
490 * Returns the lowest value of the provided arguments, or positive
491 * infinity if no argument is provided.
493 * ※ Unlike Math.min, this function accepts either number or big·int
494 * values. All values must be of the same type, or this function will
497 * ☡ If no argument is supplied, the result will be a number, not a
500 export const min
= (...$s
) => {
501 let lowest
= undefined;
502 for (let i
= 0; i
< $s
.length
; ++i
) {
503 // Iterate over all the numbers.
504 const number
= toNumeric($s
[i
]);
505 if (lowest
=== undefined) {
506 // The current number is the first one.
508 // The current number is nan.
511 // The current number is not nan.
515 // The current number is not the first one.
516 if (typeof lowest
!== typeof number
) {
517 // The type of the current number and the lowest number don’t
519 throw new TypeError("Piscēs: Type mismatch.");
520 } else if (isNan(number
)) {
521 // The current number is nan.
523 } else if (sameValue(number
, -0) && sameValue(lowest
, 0)) {
524 // The current number is -0 and the lowest number is +0.
526 } else if (number
< lowest
) {
527 // The current number is less than the lowest number.
530 // The current number is greater than or equal to the lowest
536 return lowest
?? POSITIVE_INFINITY
;
540 * Returns a unit value with the same sign as the provided value, or
541 * the provided value itself if it is not a number or (potentially
544 * For big·ints, the return value of this function is 0n if the
545 * provided value is 0n, -1n if the provided value is negative, and +1n
548 * For numbers, the return value is nan, -0, or +0 if the provided
549 * value is nan, -0, or +0, respectively, and -1 if the provided value
550 * is negative and +1 if the provided value is positive otherwise. Note
551 * that positive and negative infinity will return +1 and -1
554 * ※ Unlike Math.sign, this function accepts either number or big·int
557 export const sgn
= ($) => {
558 const n
= toNumeric($);
559 return typeof n
=== "bigint"
560 ? n
=== 0n
? 0n
: n
< 0n
? -1n
: 1n
561 : isNan(n
) || n
=== 0
563 : //deno-lint-ignore no-compare-neg-zero
570 * Returns the result of converting the provided value to a big·int.
572 * ※ This method is safe to use with numbers.
574 * ※ This is effectively an alias for BigInt.
576 export const { toBigInt
} = (() => {
577 const makeBigInt
= BigInt
;
578 return { toBigInt
: ($) => makeBigInt($) };
583 * Returns the result of converting the provided value to fit within
584 * the provided number of bits as a signed integer.
586 * ※ Unlike BigInt.asIntN, this function accepts both big·int and
589 * ☡ The first argument, the number of bits, must be a number.
594 * Returns the result of converting the provided value to fit within
595 * the provided number of bits as an unsigned integer.
597 * ※ Unlike BigInt.asUintN, this function accepts both big·int and
600 * ☡ The first argument, the number of bits, must be a number.
604 const { asIntN
, asUintN
} = BigInt
;
607 const prim
= toPrimitive($);
608 const big
·int = toBigInt(prim
);
609 const intN
= asIntN(n
, big
·int);
610 return typeof prim
=== "bigint" ? intN
: toNumber(intN
);
613 const prim
= toPrimitive($);
614 const big
·int = toBigInt(prim
);
615 const uintN
= asUintN(n
, big
·int);
616 return typeof prim
=== "bigint" ? uintN
: toNumber(uintN
);
622 * Returns the result of converting the provided value to a number.
624 * ※ This method is safe to use with big·ints.
626 * ※ This is effectively a nonconstructible version of the Number
629 export const { toNumber
} = (() => {
630 const makeNumber
= Number
;
631 return { toNumber
: ($) => makeNumber($) };
635 * Returns the result of converting the provided value to a number or
638 * ※ If the result of converting the provided value to a primitive is
639 * not a big·int, this function will return a number.
641 export const toNumeric
= ($) => {
642 const primValue
= toPrimitive($, "number");
643 return typeof primValue
=== "bigint" ? primValue
: +primValue
;