Skip to content

Commit 6d1ef1a

Browse files
committed
feat(sqrt): add directional sqrt support
1 parent b3c8023 commit 6d1ef1a

5 files changed

Lines changed: 70 additions & 13 deletions

File tree

src/index.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import {
3333
toPercentFloat,
3434
formatPercent,
3535
percentSqrt,
36+
percentSqrtUp,
37+
percentSqrtDown,
3638
} from "./percent";
3739
import {
3840
wadAdd,
@@ -54,6 +56,8 @@ import {
5456
toWadFloat,
5557
formatWad,
5658
wadSqrt,
59+
wadSqrtUp,
60+
wadSqrtDown,
5761
} from "./wad";
5862
import {
5963
rayAdd,
@@ -75,6 +79,8 @@ import {
7579
toRayFloat,
7680
formatRay,
7781
raySqrt,
82+
raySqrtUp,
83+
raySqrtDown,
7884
} from "./ray";
7985

8086
declare global {
@@ -92,6 +98,8 @@ declare global {
9298
mulDivDown: (other: bigint, scale: bigint) => bigint;
9399

94100
sqrt: () => bigint;
101+
sqrtUp: () => bigint;
102+
sqrtDown: () => bigint;
95103

96104
compMul: (other: bigint) => bigint;
97105
compDiv: (other: bigint) => bigint;
@@ -110,6 +118,8 @@ declare global {
110118
percentPowDown: (exponent: bigint) => bigint;
111119
percentExpN: (exponent: bigint) => bigint;
112120
percentSqrt: () => bigint;
121+
percentSqrtUp: () => bigint;
122+
percentSqrtDown: () => bigint;
113123
percentToDecimals: (decimals: number) => bigint;
114124
percentToWad: () => bigint;
115125
percentToRay: () => bigint;
@@ -130,6 +140,8 @@ declare global {
130140
wadPowDown: (exponent: bigint) => bigint;
131141
wadExpN: (exponent: bigint) => bigint;
132142
wadSqrt: () => bigint;
143+
wadSqrtUp: () => bigint;
144+
wadSqrtDown: () => bigint;
133145
wadToDecimals: (decimals: number) => bigint;
134146
wadToPercent: () => bigint;
135147
wadToRay: () => bigint;
@@ -150,6 +162,8 @@ declare global {
150162
rayPowDown: (exponent: bigint) => bigint;
151163
rayExpN: (exponent: bigint) => bigint;
152164
raySqrt: () => bigint;
165+
raySqrtUp: () => bigint;
166+
raySqrtDown: () => bigint;
153167
rayToDecimals: (decimals: number) => bigint;
154168
rayToPercent: () => bigint;
155169
rayToWad: () => bigint;
@@ -205,8 +219,14 @@ BigInt.prototype.mulDivDown = function (other: bigint, scale: bigint) {
205219
return mulDivDown(this as bigint, other, scale);
206220
};
207221

208-
BigInt.prototype.sqrt = function () {
209-
return sqrt(this as bigint);
222+
BigInt.prototype.sqrt = function (scale = 1n) {
223+
return sqrt(this as bigint, scale, mulDivHalfUp);
224+
};
225+
BigInt.prototype.sqrtUp = function (scale = 1n) {
226+
return sqrt(this as bigint, scale, mulDivUp);
227+
};
228+
BigInt.prototype.sqrtDown = function (scale = 1n) {
229+
return sqrt(this as bigint, scale, mulDivDown);
210230
};
211231

212232
BigInt.prototype.compMul = function (other: bigint) {
@@ -258,6 +278,12 @@ BigInt.prototype.percentExpN = function (N: bigint) {
258278
BigInt.prototype.percentSqrt = function () {
259279
return percentSqrt(this as bigint);
260280
};
281+
BigInt.prototype.percentSqrtUp = function () {
282+
return percentSqrtUp(this as bigint);
283+
};
284+
BigInt.prototype.percentSqrtDown = function () {
285+
return percentSqrtDown(this as bigint);
286+
};
261287
BigInt.prototype.percentToDecimals = function (decimals: number) {
262288
return percentToDecimals(this as bigint, decimals);
263289
};
@@ -316,6 +342,12 @@ BigInt.prototype.wadExpN = function (N: bigint) {
316342
BigInt.prototype.wadSqrt = function () {
317343
return wadSqrt(this as bigint);
318344
};
345+
BigInt.prototype.wadSqrtUp = function () {
346+
return wadSqrtUp(this as bigint);
347+
};
348+
BigInt.prototype.wadSqrtDown = function () {
349+
return wadSqrtDown(this as bigint);
350+
};
319351
BigInt.prototype.wadToDecimals = function (decimals: number) {
320352
return wadToDecimals(this as bigint, decimals);
321353
};
@@ -374,6 +406,12 @@ BigInt.prototype.rayExpN = function (N: bigint) {
374406
BigInt.prototype.raySqrt = function () {
375407
return raySqrt(this as bigint);
376408
};
409+
BigInt.prototype.raySqrtUp = function () {
410+
return raySqrtUp(this as bigint);
411+
};
412+
BigInt.prototype.raySqrtDown = function () {
413+
return raySqrtDown(this as bigint);
414+
};
377415
BigInt.prototype.rayToDecimals = function (decimals: number) {
378416
return rayToDecimals(this as bigint, decimals);
379417
};

src/percent.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ export const percentExpN = (x: bigint, N: bigint) => {
5555
};
5656

5757
export const percentSqrt = (x: bigint) => {
58-
return sqrt(x, PERCENT);
58+
return sqrt(x, PERCENT, mulDivHalfUp);
59+
};
60+
61+
export const percentSqrtUp = (x: bigint) => {
62+
return sqrt(x, PERCENT, mulDivUp);
63+
};
64+
65+
export const percentSqrtDown = (x: bigint) => {
66+
return sqrt(x, PERCENT, mulDivDown);
5967
};
6068

6169
export const percentToDecimals = (x: bigint, decimals: number) => {

src/ray.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ export const rayExpN = (x: bigint, N: bigint) => {
5555
};
5656

5757
export const raySqrt = (x: bigint) => {
58-
return sqrt(x, RAY);
58+
return sqrt(x, RAY, mulDivHalfUp);
59+
};
60+
61+
export const raySqrtUp = (x: bigint) => {
62+
return sqrt(x, RAY, mulDivUp);
63+
};
64+
65+
export const raySqrtDown = (x: bigint) => {
66+
return sqrt(x, RAY, mulDivDown);
5967
};
6068

6169
export const rayToDecimals = (x: bigint, decimals: number) => {

src/utils.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@ export const mulDivUp: MulDiv = (x, y, scale) => {
5353
export const avgHalfUp = (x: bigint, y: bigint, pct: bigint, scale: bigint) =>
5454
(max(0n, scale - pct) * x + min(scale, pct) * y + scale / 2n) / scale;
5555

56-
export const pow = (
57-
x: bigint,
58-
exponent: bigint,
59-
scale: bigint,
60-
mulDiv: MulDiv = mulDivHalfUp,
61-
): bigint => {
56+
export const pow = (x: bigint, exponent: bigint, scale: bigint, mulDiv: MulDiv): bigint => {
6257
if (exponent === 0n) return scale;
6358
if (exponent === 1n) return x;
6459

@@ -68,7 +63,7 @@ export const pow = (
6863
return mulDiv(x, pow(xSquared, (exponent - 1n) / 2n, scale, mulDiv), scale);
6964
};
7065

71-
export const sqrt = (x: bigint, scale = 1n, mulDiv: MulDiv = mulDivHalfUp) => {
66+
export const sqrt = (x: bigint, scale: bigint, mulDiv: MulDiv) => {
7267
if (x === 0n) return 0n;
7368

7469
const scale2 = 2n * scale;
@@ -84,7 +79,7 @@ export const sqrt = (x: bigint, scale = 1n, mulDiv: MulDiv = mulDivHalfUp) => {
8479
return x0;
8580
};
8681

87-
export const expN = (x: bigint, N: bigint, scale: bigint, mulDiv: MulDiv = mulDivDown) => {
82+
export const expN = (x: bigint, N: bigint, scale: bigint, mulDiv = mulDivDown) => {
8883
let res = scale;
8984
let monomial = scale;
9085
for (let k = 1n; k <= N; k++) {

src/wad.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ export const wadExpN = (x: bigint, N: bigint) => {
5555
};
5656

5757
export const wadSqrt = (x: bigint) => {
58-
return sqrt(x, WAD);
58+
return sqrt(x, WAD, mulDivHalfUp);
59+
};
60+
61+
export const wadSqrtUp = (x: bigint) => {
62+
return sqrt(x, WAD, mulDivUp);
63+
};
64+
65+
export const wadSqrtDown = (x: bigint) => {
66+
return sqrt(x, WAD, mulDivDown);
5967
};
6068

6169
export const wadToDecimals = (x: bigint, decimals: number) => {

0 commit comments

Comments
 (0)