// ♓🌟 Piscēs ∷ numeric.test.js
// ====================================================================
//
-// Copyright © 2022 Lady [@ Lady’s Computer].
+// 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
POSITIVE_ZERO,
sgn,
toBigInt,
+ toExponentialNotation,
+ toFixedDecimalNotation,
toFloat32,
- toIntegerOrInfinity,
toIntegralNumber,
- toIntN,
+ toIntegralNumberOrInfinity,
toNumber,
toNumeric,
- toUintN,
+ toSignedIntegralNumeric,
+ toUnsignedIntegralNumeric,
} from "./numeric.js";
describe("NEGATIVE_ZERO", () => {
});
});
+describe("toExponentialNotation", () => {
+ it("[[Call]] converts to exponential notation", () => {
+ assertStrictEquals(toExponentialNotation(231), "2.31e+2");
+ });
+
+ it("[[Call]] works with big·ints", () => {
+ assertStrictEquals(
+ toExponentialNotation(9007199254740993n),
+ "9.007199254740993e+15",
+ );
+ });
+
+ it("[[Call]] respects the specified number of fractional digits", () => {
+ assertStrictEquals(
+ toExponentialNotation(.00000000642, 3),
+ "6.420e-9",
+ );
+ assertStrictEquals(toExponentialNotation(.00691, 1), "6.9e-3");
+ assertStrictEquals(toExponentialNotation(.00685, 1), "6.9e-3");
+ assertStrictEquals(toExponentialNotation(.004199, 2), "4.20e-3");
+ assertStrictEquals(
+ toExponentialNotation(6420000000n, 3),
+ "6.420e+9",
+ );
+ assertStrictEquals(toExponentialNotation(6910n, 1), "6.9e+3");
+ assertStrictEquals(toExponentialNotation(6850n, 1), "6.9e+3");
+ assertStrictEquals(toExponentialNotation(4199n, 2), "4.20e+3");
+ });
+});
+
+describe("toFixedDecimalNotation", () => {
+ it("[[Call]] converts to fixed decimal notation", () => {
+ assertStrictEquals(toFixedDecimalNotation(69.4199, 3), "69.420");
+ });
+
+ it("[[Call]] works with big·ints", () => {
+ assertStrictEquals(
+ toFixedDecimalNotation(9007199254740993n),
+ "9007199254740993",
+ );
+ assertStrictEquals(
+ toFixedDecimalNotation(9007199254740993n, 2),
+ "9007199254740993.00",
+ );
+ });
+});
+
describe("toFloat32", () => {
it("[[Call]] returns the 32‐bit floating‐point representation", () => {
assertStrictEquals(
});
});
-describe("toIntN", () => {
+describe("toSignedIntegralNumeric", () => {
it("[[Call]] converts to an int·n", () => {
- assertStrictEquals(toIntN(2, 7n), -1n);
+ assertStrictEquals(toSignedIntegralNumeric(2, 7n), -1n);
});
it("[[Call]] works with numbers", () => {
- assertStrictEquals(toIntN(2, 7), -1);
+ assertStrictEquals(toSignedIntegralNumeric(2, 7), -1);
});
it("[[Call]] works with non‐integers", () => {
- assertStrictEquals(toIntN(2, 7.21), -1);
- assertStrictEquals(toIntN(2, Infinity), 0);
+ assertStrictEquals(toSignedIntegralNumeric(2, 7.21), -1);
+ assertStrictEquals(toSignedIntegralNumeric(2, Infinity), 0);
});
});
-describe("toIntegerOrInfinity", () => {
+describe("toIntegralNumber", () => {
it("[[Call]] converts nan to zero", () => {
- assertStrictEquals(toIntegerOrInfinity(NaN), 0);
+ assertStrictEquals(toIntegralNumber(NaN), 0);
});
it("[[Call]] converts negative zero to positive zero", () => {
- assertStrictEquals(toIntegerOrInfinity(-0), 0);
+ assertStrictEquals(toIntegralNumber(-0), 0);
});
it("[[Call]] drops the fractional part of negative numbers", () => {
- assertStrictEquals(toIntegerOrInfinity(-1.79), -1);
+ assertStrictEquals(toIntegralNumber(-1.79), -1);
});
- it("[[Call]] returns infinity for infinity", () => {
- assertStrictEquals(toIntegerOrInfinity(Infinity), Infinity);
+ it("[[Call]] returns zero for infinity", () => {
+ assertStrictEquals(toIntegralNumber(Infinity), 0);
});
- it("[[Call]] returns negative infinity for negative infinity", () => {
- assertStrictEquals(toIntegerOrInfinity(-Infinity), -Infinity);
+ it("[[Call]] returns zero for negative infinity", () => {
+ assertStrictEquals(toIntegralNumber(-Infinity), 0);
});
it("[[Call]] works with big·ints", () => {
- assertStrictEquals(toIntegerOrInfinity(2n), 2);
+ assertStrictEquals(toIntegralNumber(2n), 2);
});
});
-describe("toIntegralNumber", () => {
+describe("toIntegralNumberOrInfinity", () => {
it("[[Call]] converts nan to zero", () => {
- assertStrictEquals(toIntegralNumber(NaN), 0);
+ assertStrictEquals(toIntegralNumberOrInfinity(NaN), 0);
});
it("[[Call]] converts negative zero to positive zero", () => {
- assertStrictEquals(toIntegralNumber(-0), 0);
+ assertStrictEquals(toIntegralNumberOrInfinity(-0), 0);
});
it("[[Call]] drops the fractional part of negative numbers", () => {
- assertStrictEquals(toIntegralNumber(-1.79), -1);
+ assertStrictEquals(toIntegralNumberOrInfinity(-1.79), -1);
});
- it("[[Call]] returns zero for infinity", () => {
- assertStrictEquals(toIntegralNumber(Infinity), 0);
+ it("[[Call]] returns infinity for infinity", () => {
+ assertStrictEquals(toIntegralNumberOrInfinity(Infinity), Infinity);
});
- it("[[Call]] returns zero for negative infinity", () => {
- assertStrictEquals(toIntegralNumber(-Infinity), 0);
+ it("[[Call]] returns negative infinity for negative infinity", () => {
+ assertStrictEquals(
+ toIntegralNumberOrInfinity(-Infinity),
+ -Infinity,
+ );
});
it("[[Call]] works with big·ints", () => {
- assertStrictEquals(toIntegralNumber(2n), 2);
+ assertStrictEquals(toIntegralNumberOrInfinity(2n), 2);
});
});
});
});
-describe("toUintN", () => {
+describe("toUnsignedIntegralNumeric", () => {
it("[[Call]] converts to an int·n", () => {
- assertStrictEquals(toUintN(2, 7n), 3n);
+ assertStrictEquals(toUnsignedIntegralNumeric(2, 7n), 3n);
});
it("[[Call]] works with numbers", () => {
- assertStrictEquals(toUintN(2, 7), 3);
+ assertStrictEquals(toUnsignedIntegralNumeric(2, 7), 3);
});
it("[[Call]] works with non‐integers", () => {
- assertStrictEquals(toUintN(2, 7.21), 3);
- assertStrictEquals(toUintN(2, Infinity), 0);
+ assertStrictEquals(toUnsignedIntegralNumeric(2, 7.21), 3);
+ assertStrictEquals(toUnsignedIntegralNumeric(2, Infinity), 0);
});
});