Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
jessealama committed Jun 19, 2024
1 parent 1e5abb5 commit 07ec1f1
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 43 deletions.
37 changes: 28 additions & 9 deletions examples/five-numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,39 @@ function getRandomInt(max: number): number {
return Math.floor(Math.random() * max);
}

function getRandomDigits(n: number): number[]
{
function getRandomDigits(n: number): number[] {
let digits = [];
for (let i = 0; i < n; i++) {
digits.push(getRandomInt(10));
}
return digits;
}

function getRandomDigitsAsString(n: number): string
{
function getRandomDigitsAsString(n: number): string {
let digits = getRandomDigits(n + 4);
return digits.slice(0, n).join("") + "." + digits.slice(n).join("");
}

function compareNumbers(digits1: string, digits2: string, digits3: string, digits4: string, digits5: string): [string, string] {
function compareNumbers(
digits1: string,
digits2: string,
digits3: string,
digits4: string,
digits5: string
): [string, string] {
let d1 = new Decimal128(digits1);
let d2 = new Decimal128(digits2);
let d3 = new Decimal128(digits3);
let d4 = new Decimal128(digits4);
let d5 = new Decimal128(digits5);

let decimalResult = d1.add(d2).add(d3).add(d4).multiply(d5);
let numberResult = (Number(digits1) + Number(digits2) + Number(digits3) + Number(digits4)) * Number(digits5);
let numberResult =
(Number(digits1) +
Number(digits2) +
Number(digits3) +
Number(digits4)) *
Number(digits5);
let numberResultString = numberResult.toFixed(4);
let decimalResultString = decimalResult.toFixed(4);

Expand All @@ -41,11 +50,21 @@ function keepTrying(): boolean {
let digits4 = getRandomDigitsAsString(4);
let digits5 = getRandomDigitsAsString(4);

let [result1, result2] = compareNumbers(digits1, digits2, digits3, digits4, digits5);
let [result1, result2] = compareNumbers(
digits1,
digits2,
digits3,
digits4,
digits5
);

if (result1 !== result2) {
let diff = new Decimal128(result1).subtract(new Decimal128(result2)).abs();
console.log(`(${digits1} + ${digits2} + ${digits3} + ${digits4}) * ${digits5} = ${result1} but it should be ${result2} (difference: ${diff.toString()})`);
let diff = new Decimal128(result1)
.subtract(new Decimal128(result2))
.abs();
console.log(
`(${digits1} + ${digits2} + ${digits3} + ${digits4}) * ${digits5} = ${result1} but it should be ${result2} (difference: ${diff.toString()})`
);
}

return result1 !== result2;
Expand Down
14 changes: 8 additions & 6 deletions examples/midprice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ function getRandomInt(max: number): number {
return Math.floor(Math.random() * max);
}

function getRandomDigits(n: number): number[]
{
function getRandomDigits(n: number): number[] {
let digits = [];
for (let i = 0; i < n; i++) {
digits.push(getRandomInt(10));
}
return digits;
}

function getRandomDigitsAsString(n: number): string
{
function getRandomDigitsAsString(n: number): string {
let digits = getRandomDigits(n + 4);
return digits.slice(0, n).join("") + "." + digits.slice(n).join("");
}
Expand All @@ -39,8 +37,12 @@ function keepTrying(): boolean {
let [result1, result2] = compareNumbers(digits1, digits2);

if (result1 !== result2) {
let diff = new Decimal128(result1).subtract(new Decimal128(result2)).abs();
console.log(`(${digits1} + ${digits2})/2 = ${result1} but it should be ${result2} (difference: ${diff.toString()})`);
let diff = new Decimal128(result1)
.subtract(new Decimal128(result2))
.abs();
console.log(
`(${digits1} + ${digits2})/2 = ${result1} but it should be ${result2} (difference: ${diff.toString()})`
);
}

return result1 !== result2;
Expand Down
24 changes: 15 additions & 9 deletions examples/rounding-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@ function getRandomInt(max: number): number {
return Math.floor(Math.random() * max);
}

function getRandomDigits(n: number): number[]
{
function getRandomDigits(n: number): number[] {
let digits = [];
for (let i = 0; i < n; i++) {
digits.push(getRandomInt(10));
}
return digits;
}

function getRandomDigitsAsString(n: number): string
{
function getRandomDigitsAsString(n: number): string {
let digits = getRandomDigits(n + 4);
return digits.slice(0, n).join("") + "." + digits.slice(n).join("");
}

function compareNumbers(digits1: string, digits2: string, digits3: string): [string, string] {
function compareNumbers(
digits1: string,
digits2: string,
digits3: string
): [string, string] {
let d1 = new Decimal128(digits1);
let d2 = new Decimal128(digits2);
let d3 = new Decimal128(digits3);

let decimalResult = d1.multiply(d2).add(d3);
let numberResult = (Number(digits1) * Number(digits2)) + Number(digits3);
let numberResult = Number(digits1) * Number(digits2) + Number(digits3);
let numberResultString = numberResult.toFixed(4);
let decimalResultString =decimalResult.toFixed(4);
let decimalResultString = decimalResult.toFixed(4);

return [numberResultString, decimalResultString];
}
Expand All @@ -39,8 +41,12 @@ function keepTrying(): boolean {
let [result1, result2] = compareNumbers(digits1, digits2, digits3);

if (result1 !== result2) {
let diff = new Decimal128(result1).subtract(new Decimal128(result2)).abs();
console.log(`(${digits1} * ${digits2}) + ${digits3} = ${result1} but it should be ${result2} (difference of ${diff.toString()})`);
let diff = new Decimal128(result1)
.subtract(new Decimal128(result2))
.abs();
console.log(
`(${digits1} * ${digits2}) + ${digits3} = ${result1} but it should be ${result2} (difference of ${diff.toString()})`
);
}

return result1 !== result2;
Expand Down
6 changes: 2 additions & 4 deletions examples/roundtrip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ function getRandomInt(max: number): number {
return Math.floor(Math.random() * max);
}

function getRandomDigits(n: number): number[]
{
function getRandomDigits(n: number): number[] {
let digits = [];
for (let i = 0; i < n; i++) {
digits.push(getRandomInt(10));
}
return digits;
}

function getRandomDigitsAsString(n: number): string
{
function getRandomDigitsAsString(n: number): string {
let digits = getRandomDigits(n + 2);
let result = digits.slice(0, n).join("") + "." + digits.slice(n).join("");
let noInitialZeroes = result.replace(/^0+/, "");
Expand Down
13 changes: 6 additions & 7 deletions src/decimal128.mts
Original file line number Diff line number Diff line change
Expand Up @@ -925,14 +925,15 @@ export class Decimal128 {
return this.emitDecimal(options);
}

toFixed(n?: number): string
{
toFixed(n?: number): string {
if (this.isNaN) {
return NAN;
}

if (!this.isFinite) {
return this.isNegative ? "-" + POSITIVE_INFINITY : POSITIVE_INFINITY;
return this.isNegative
? "-" + POSITIVE_INFINITY
: POSITIVE_INFINITY;
}

if (typeof n === "number" && n < 0) {
Expand All @@ -949,13 +950,11 @@ export class Decimal128 {
return this.round(n).emitDecimal(opts);
}

toPrecision(n?: number): string
{
toPrecision(n?: number): string {
return "6";
}

toExponential(): string
{
toExponential(): string {
return "7";
}

Expand Down
9 changes: 2 additions & 7 deletions tests/tofixed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ describe("to decimal places", function () {
const d = "123.456";
const decimalD = new Decimal128(d);
test("more digits than available means digits get added", () => {
expectDecimal128(
decimalD.toFixed(4),
"123.4560"
);
expectDecimal128(decimalD.toFixed(4), "123.4560");
});
test("same number of digits as available means no change", () => {
expectDecimal128(decimalD.toFixed(3), "123.456");
Expand All @@ -52,8 +49,6 @@ describe("to decimal places", function () {
expect(() => decimalD.toFixed(-1)).toThrow(RangeError);
});
test("non-integer takes floor", () => {
expect(decimalD.toFixed(1.5)).toStrictEqual(
"123.4"
);
expect(decimalD.toFixed(1.5)).toStrictEqual("123.4");
});
});
4 changes: 3 additions & 1 deletion tests/toprecison.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ describe("zero", () => {

describe("infinity", () => {
test("positive infinity", () => {
expect(new Decimal128("Infinity").toPrecision()).toStrictEqual("Infinity");
expect(new Decimal128("Infinity").toPrecision()).toStrictEqual(
"Infinity"
);
});
test("negative infinity", () => {
expect(new Decimal128("-Infinity").toPrecision()).toStrictEqual(
Expand Down

0 comments on commit 07ec1f1

Please sign in to comment.