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

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jessealama committed Jun 25, 2024
1 parent 928aec2 commit 4f5ee87
Show file tree
Hide file tree
Showing 38 changed files with 42 additions and 404 deletions.
2 changes: 1 addition & 1 deletion examples/bill.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../src/Decimal128.mjs";

const zero = new Decimal128("0");
const one = new Decimal128("1");
Expand Down
2 changes: 1 addition & 1 deletion examples/currency.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../src/Decimal128.mjs";

let exchangeRateEurToUsd = new Decimal128("1.09");
let amountInUsd = new Decimal128("450.27");
Expand Down
2 changes: 1 addition & 1 deletion examples/five-numbers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../src/Decimal128.mjs";

function getRandomInt(max: number): number {
return Math.floor(Math.random() * max);
Expand Down
2 changes: 1 addition & 1 deletion examples/floor.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../src/Decimal128.mjs";

function floor(d: Decimal128): Decimal128 {
return d.round(0, "floor");
Expand Down
2 changes: 1 addition & 1 deletion examples/midprice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../src/Decimal128.mjs";

function getRandomInt(max: number): number {
return Math.floor(Math.random() * max);
Expand Down
2 changes: 1 addition & 1 deletion examples/mortgage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../src/Decimal128.mjs";
import { pow } from "./pow.mjs";

const one = new Decimal128("1");
Expand Down
2 changes: 1 addition & 1 deletion examples/pow.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../src/Decimal128.mjs";

function pow(a: Decimal128, b: number): Decimal128 {
let result = a;
Expand Down
2 changes: 1 addition & 1 deletion examples/round.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Examples from the Intl.NumberFormat spec

import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../src/Decimal128.mjs";

let minusOnePointFive = new Decimal128("-1.5");
let zeroPointFour = new Decimal128("0.4");
Expand Down
2 changes: 1 addition & 1 deletion examples/rounding-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../src/Decimal128.mjs";

function getRandomInt(max: number): number {
return Math.floor(Math.random() * max);
Expand Down
2 changes: 0 additions & 2 deletions examples/roundtrip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Decimal128 } from "../src/decimal128.mjs";

function getRandomInt(max: number): number {
return Math.floor(Math.random() * max);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Decimal.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Rational } from "./rational.mjs";
import { Rational } from "./Rational.mjs";

const ratOne = new Rational(1n, 1n);
const ratTen = new Rational(10n, 1n);
Expand Down
2 changes: 1 addition & 1 deletion src/decimal128.mts → src/Decimal128.mts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

import { Digit, DigitOrTen, RoundingMode, ROUNDING_MODES } from "./common.mjs";
import { Rational } from "./rational.mjs";
import { Rational } from "./Rational.mjs";
import { Decimal } from "./Decimal.mjs";

const EXPONENT_MIN = -6176;
Expand Down
File renamed without changes.
27 changes: 11 additions & 16 deletions src/common.mts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import {
ROUNDING_MODE_CEILING,
ROUNDING_MODE_FLOOR,
ROUNDING_MODE_HALF_EXPAND,
ROUNDING_MODE_TRUNCATE,
} from "./decimal128.mjs";

/**
* Counts the number of significant digits in a digit string, assumed to be normalized.
*
Expand Down Expand Up @@ -36,10 +29,6 @@ export function countSignificantDigits(s: string): number {
return s.length;
}

export function countDigits(s: string): number {
return s.replace(/[.]/, "").length;
}

export function countFractionalDigits(s: string): number {
let [, fractional] = s.split(".");

Expand All @@ -53,6 +42,12 @@ export function countFractionalDigits(s: string): number {
export type Digit = -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; // -1 signals that we're moving from the integer part to the decimal part of a decimal number
export type DigitOrTen = Digit | 10;

const ROUNDING_MODE_CEILING = "ceil";
const ROUNDING_MODE_FLOOR = "floor";
const ROUNDING_MODE_TRUNCATE = "trunc";
const ROUNDING_MODE_HALF_EVEN = "halfEven";
const ROUNDING_MODE_HALF_EXPAND = "halfExpand";

export type RoundingMode =
| "ceil"
| "floor"
Expand All @@ -61,11 +56,11 @@ export type RoundingMode =
| "halfExpand";

export const ROUNDING_MODES: RoundingMode[] = [
"ceil",
"floor",
"trunc",
"halfEven",
"halfExpand",
ROUNDING_MODE_CEILING,
ROUNDING_MODE_FLOOR,
ROUNDING_MODE_TRUNCATE,
ROUNDING_MODE_HALF_EVEN,
ROUNDING_MODE_HALF_EXPAND,
];

function roundIt(
Expand Down
2 changes: 1 addition & 1 deletion tests/abs.test.js → tests/Decimal128/abs.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";

const MAX_SIGNIFICANT_DIGITS = 34;
const bigDigits = "9".repeat(MAX_SIGNIFICANT_DIGITS);
Expand Down
2 changes: 1 addition & 1 deletion tests/add.test.js → tests/Decimal128/add.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";

const MAX_SIGNIFICANT_DIGITS = 34;
const bigDigits = "9".repeat(MAX_SIGNIFICANT_DIGITS);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";

const MAX_SIGNIFICANT_DIGITS = 34;

Expand Down
2 changes: 1 addition & 1 deletion tests/divide.test.js → tests/Decimal128/divide.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";

describe("division", () => {
test("simple example", () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/equals.test.js → tests/Decimal128/equals.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";

const MAX_SIGNIFICANT_DIGITS = 34;
const nan = new Decimal128("NaN");
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";

const MAX_SIGNIFICANT_DIGITS = 34;
const nan = new Decimal128("NaN");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";

const examples = [
["123.456", "789.789", "97504.190784"],
Expand Down
2 changes: 1 addition & 1 deletion tests/neg.test.js → tests/Decimal128/neg.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";

const MAX_SIGNIFICANT_DIGITS = 34;
const bigDigits = "9".repeat(MAX_SIGNIFICANT_DIGITS);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";

const a = "4.1";
const b = "1.25";
Expand Down
2 changes: 1 addition & 1 deletion tests/round.test.js → tests/Decimal128/round.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";
import * as string_decoder from "string_decoder";
import { expectDecimal128 } from "./util.js";

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";
import { expectDecimal128 } from "./util.js";

const MAX_SIGNIFICANT_DIGITS = 34;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";
import { expectDecimal128 } from "./util.js";

describe("NaN", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";
import { expectDecimal128 } from "./util.js";

describe("NaN", () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/tofixed.test.js → tests/Decimal128/tofixed.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";
import { expectDecimal128 } from "./util.js";

describe("NaN", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";
import { expectDecimal128 } from "./util.js";

describe("NaN", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";
import { expectDecimal128 } from "./util.js";

describe("NaN", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";
import { expectDecimal128 } from "./util.js";

describe("NaN", () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/util.js → tests/Decimal128/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";

export function expectDecimal128(a, b) {
let lhs = a instanceof Decimal128 ? a.toString({ normalize: false }) : a;
Expand Down
2 changes: 1 addition & 1 deletion tests/valueof.test.js → tests/Decimal128/valueof.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Decimal128 } from "../src/decimal128.mjs";
import { Decimal128 } from "../../src/Decimal128.mjs";

describe("valueOf", () => {
test("throws unconditionally", () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/rational.test.js → tests/Rational/rational.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Rational } from "../src/rational.mjs";
import { Rational } from "../../src/Rational.mjs";
describe("constructor", () => {
test("cannot divide by zero", () => {
expect(() => new Rational(1n, 0n)).toThrow(RangeError);
Expand Down
File renamed without changes.
Loading

0 comments on commit 4f5ee87

Please sign in to comment.