Skip to content

Commit

Permalink
feat(sqrt): add directional sqrt support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Feb 19, 2024
1 parent b3c8023 commit 6d1ef1a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 13 deletions.
42 changes: 40 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
toPercentFloat,
formatPercent,
percentSqrt,
percentSqrtUp,
percentSqrtDown,
} from "./percent";
import {
wadAdd,
Expand All @@ -54,6 +56,8 @@ import {
toWadFloat,
formatWad,
wadSqrt,
wadSqrtUp,
wadSqrtDown,
} from "./wad";
import {
rayAdd,
Expand All @@ -75,6 +79,8 @@ import {
toRayFloat,
formatRay,
raySqrt,
raySqrtUp,
raySqrtDown,
} from "./ray";

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

sqrt: () => bigint;
sqrtUp: () => bigint;
sqrtDown: () => bigint;

compMul: (other: bigint) => bigint;
compDiv: (other: bigint) => bigint;
Expand All @@ -110,6 +118,8 @@ declare global {
percentPowDown: (exponent: bigint) => bigint;
percentExpN: (exponent: bigint) => bigint;
percentSqrt: () => bigint;
percentSqrtUp: () => bigint;
percentSqrtDown: () => bigint;
percentToDecimals: (decimals: number) => bigint;
percentToWad: () => bigint;
percentToRay: () => bigint;
Expand All @@ -130,6 +140,8 @@ declare global {
wadPowDown: (exponent: bigint) => bigint;
wadExpN: (exponent: bigint) => bigint;
wadSqrt: () => bigint;
wadSqrtUp: () => bigint;
wadSqrtDown: () => bigint;
wadToDecimals: (decimals: number) => bigint;
wadToPercent: () => bigint;
wadToRay: () => bigint;
Expand All @@ -150,6 +162,8 @@ declare global {
rayPowDown: (exponent: bigint) => bigint;
rayExpN: (exponent: bigint) => bigint;
raySqrt: () => bigint;
raySqrtUp: () => bigint;
raySqrtDown: () => bigint;
rayToDecimals: (decimals: number) => bigint;
rayToPercent: () => bigint;
rayToWad: () => bigint;
Expand Down Expand Up @@ -205,8 +219,14 @@ BigInt.prototype.mulDivDown = function (other: bigint, scale: bigint) {
return mulDivDown(this as bigint, other, scale);
};

BigInt.prototype.sqrt = function () {
return sqrt(this as bigint);
BigInt.prototype.sqrt = function (scale = 1n) {
return sqrt(this as bigint, scale, mulDivHalfUp);
};
BigInt.prototype.sqrtUp = function (scale = 1n) {
return sqrt(this as bigint, scale, mulDivUp);
};
BigInt.prototype.sqrtDown = function (scale = 1n) {
return sqrt(this as bigint, scale, mulDivDown);
};

BigInt.prototype.compMul = function (other: bigint) {
Expand Down Expand Up @@ -258,6 +278,12 @@ BigInt.prototype.percentExpN = function (N: bigint) {
BigInt.prototype.percentSqrt = function () {
return percentSqrt(this as bigint);
};
BigInt.prototype.percentSqrtUp = function () {
return percentSqrtUp(this as bigint);
};
BigInt.prototype.percentSqrtDown = function () {
return percentSqrtDown(this as bigint);
};
BigInt.prototype.percentToDecimals = function (decimals: number) {
return percentToDecimals(this as bigint, decimals);
};
Expand Down Expand Up @@ -316,6 +342,12 @@ BigInt.prototype.wadExpN = function (N: bigint) {
BigInt.prototype.wadSqrt = function () {
return wadSqrt(this as bigint);
};
BigInt.prototype.wadSqrtUp = function () {
return wadSqrtUp(this as bigint);
};
BigInt.prototype.wadSqrtDown = function () {
return wadSqrtDown(this as bigint);
};
BigInt.prototype.wadToDecimals = function (decimals: number) {
return wadToDecimals(this as bigint, decimals);
};
Expand Down Expand Up @@ -374,6 +406,12 @@ BigInt.prototype.rayExpN = function (N: bigint) {
BigInt.prototype.raySqrt = function () {
return raySqrt(this as bigint);
};
BigInt.prototype.raySqrtUp = function () {
return raySqrtUp(this as bigint);
};
BigInt.prototype.raySqrtDown = function () {
return raySqrtDown(this as bigint);
};
BigInt.prototype.rayToDecimals = function (decimals: number) {
return rayToDecimals(this as bigint, decimals);
};
Expand Down
10 changes: 9 additions & 1 deletion src/percent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ export const percentExpN = (x: bigint, N: bigint) => {
};

export const percentSqrt = (x: bigint) => {
return sqrt(x, PERCENT);
return sqrt(x, PERCENT, mulDivHalfUp);
};

export const percentSqrtUp = (x: bigint) => {
return sqrt(x, PERCENT, mulDivUp);
};

export const percentSqrtDown = (x: bigint) => {
return sqrt(x, PERCENT, mulDivDown);
};

export const percentToDecimals = (x: bigint, decimals: number) => {
Expand Down
10 changes: 9 additions & 1 deletion src/ray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ export const rayExpN = (x: bigint, N: bigint) => {
};

export const raySqrt = (x: bigint) => {
return sqrt(x, RAY);
return sqrt(x, RAY, mulDivHalfUp);
};

export const raySqrtUp = (x: bigint) => {
return sqrt(x, RAY, mulDivUp);
};

export const raySqrtDown = (x: bigint) => {
return sqrt(x, RAY, mulDivDown);
};

export const rayToDecimals = (x: bigint, decimals: number) => {
Expand Down
11 changes: 3 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ export const mulDivUp: MulDiv = (x, y, scale) => {
export const avgHalfUp = (x: bigint, y: bigint, pct: bigint, scale: bigint) =>
(max(0n, scale - pct) * x + min(scale, pct) * y + scale / 2n) / scale;

export const pow = (
x: bigint,
exponent: bigint,
scale: bigint,
mulDiv: MulDiv = mulDivHalfUp,
): bigint => {
export const pow = (x: bigint, exponent: bigint, scale: bigint, mulDiv: MulDiv): bigint => {
if (exponent === 0n) return scale;
if (exponent === 1n) return x;

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

export const sqrt = (x: bigint, scale = 1n, mulDiv: MulDiv = mulDivHalfUp) => {
export const sqrt = (x: bigint, scale: bigint, mulDiv: MulDiv) => {
if (x === 0n) return 0n;

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

export const expN = (x: bigint, N: bigint, scale: bigint, mulDiv: MulDiv = mulDivDown) => {
export const expN = (x: bigint, N: bigint, scale: bigint, mulDiv = mulDivDown) => {
let res = scale;
let monomial = scale;
for (let k = 1n; k <= N; k++) {
Expand Down
10 changes: 9 additions & 1 deletion src/wad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ export const wadExpN = (x: bigint, N: bigint) => {
};

export const wadSqrt = (x: bigint) => {
return sqrt(x, WAD);
return sqrt(x, WAD, mulDivHalfUp);
};

export const wadSqrtUp = (x: bigint) => {
return sqrt(x, WAD, mulDivUp);
};

export const wadSqrtDown = (x: bigint) => {
return sqrt(x, WAD, mulDivDown);
};

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

0 comments on commit 6d1ef1a

Please sign in to comment.