Skip to content

Commit

Permalink
feat(exp3): generalize to expN
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Sep 29, 2023
1 parent 9226c98 commit 7b50d72
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 29 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ BigInt.from(WAD * 2n).rayMul(0.5e27); // WAD
- [wadPow](#wadPow)
- [wadPowUp](#wadPowUp)
- [wadPowDown](#wadPowDown)
- [wadExp3](#wadExp3)
- [wadExpN](#wadExpN)
- [wadMulUp](#wadMulUp)
- [wadMulDown](#wadMulDown)
- [wadDivUp](#wadDivUp)
Expand All @@ -85,7 +85,7 @@ BigInt.from(WAD * 2n).rayMul(0.5e27); // WAD
- [rayPow](#rayPow)
- [rayPowUp](#rayPowUp)
- [rayPowDown](#rayPowDown)
- [rayExp3](#rayExp3)
- [rayExpN](#rayExpN)
- [rayMulUp](#rayMulUp)
- [rayMulDown](#rayMulDown)
- [rayDivUp](#rayDivUp)
Expand All @@ -104,7 +104,7 @@ BigInt.from(WAD * 2n).rayMul(0.5e27); // WAD
- [percentPow](#percentPow)
- [percentPowUp](#percentPowUp)
- [percentPowDown](#percentPowDown)
- [percentExp3](#percentExp3)
- [percentExpN](#percentExpN)
- [percentMulUp](#percentMulUp)
- [percentMulDown](#percentMulDown)
- [percentDivUp](#percentDivUp)
Expand Down Expand Up @@ -421,12 +421,12 @@ BigInt.PERCENT *
.wadPowDown(2); // 2.0 ** 2 = 4.0 (in wad)
```

#### `wadExp3`
#### `wadExpN`

Returns the third order Taylor polynomial approximation of the integer exp of a BigInt, calculated using wad-based multiplications (4 decimals precision), rounded down
Returns the N-th order Taylor polynomial approximation of the integer exp of a BigInt, calculated using wad-based multiplications (4 decimals precision), rounded down

```typescript
BigInt.PERCENT.wadExp3(1); // ~exp(1.0 * 1) ~= exp (in wad)
BigInt.PERCENT.wadExpN(3); // ~exp(1.0) ~= exp (in wad), using third-order Taylor polynomial
```

#### `wadMulUp`
Expand Down Expand Up @@ -583,12 +583,12 @@ BigInt.PERCENT *
.rayPowDown(2); // 2.0 ** 2 = 4.0 (in ray)
```

#### `rayExp3`
#### `rayExpN`

Returns the third order Taylor polynomial approximation of the integer exp of a BigInt, calculated using ray-based multiplications (4 decimals precision), rounded down
Returns the N-th order Taylor polynomial approximation of the integer exp of a BigInt, calculated using ray-based multiplications (4 decimals precision), rounded down

```typescript
BigInt.PERCENT.rayExp3(1); // ~exp(1.0 * 1) ~= exp (in ray)
BigInt.PERCENT.rayExpN(3); // ~exp(1.0) ~= exp (in ray), using third-order Taylor polynomial
```

#### `rayMulUp`
Expand Down Expand Up @@ -746,12 +746,12 @@ BigInt.PERCENT *
.percentPowDown(2); // 2.0 ** 2 = 4.0 (in percent)
```

#### `percentExp3`
#### `percentExpN`

Returns the third order Taylor polynomial approximation of the integer exp of a BigInt, calculated using percent-based multiplications (4 decimals precision), rounded down
Returns the N-th order Taylor polynomial approximation of the integer exp of a BigInt, calculated using percent-based multiplications (4 decimals precision), rounded down

```typescript
BigInt.PERCENT.percentExp3(1); // ~exp(1.0 * 1) ~= exp (in percent)
BigInt.PERCENT.percentExpN(3); // ~exp(1.0) ~= exp (in percent), using third-order Taylor polynomial
```

#### `percentMulUp`
Expand Down
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
mulDivDown,
approxEqAbs,
abs,
exp3,
expN,
} from "./utils";

declare global {
Expand Down Expand Up @@ -48,7 +48,7 @@ declare global {
percentPow: (exponent: BigNumberish) => bigint;
percentPowUp: (exponent: BigNumberish) => bigint;
percentPowDown: (exponent: BigNumberish) => bigint;
percentExp3: (exponent: BigNumberish) => bigint;
percentExpN: (exponent: BigNumberish) => bigint;
percentToDecimals: (decimals: number) => bigint;
percentToWad: () => bigint;
percentToRay: () => bigint;
Expand All @@ -67,7 +67,7 @@ declare global {
wadPow: (exponent: BigNumberish) => bigint;
wadPowUp: (exponent: BigNumberish) => bigint;
wadPowDown: (exponent: BigNumberish) => bigint;
wadExp3: (exponent: BigNumberish) => bigint;
wadExpN: (exponent: BigNumberish) => bigint;
wadToDecimals: (decimals: number) => bigint;
wadToPercent: () => bigint;
wadToRay: () => bigint;
Expand All @@ -86,7 +86,7 @@ declare global {
rayPow: (exponent: BigNumberish) => bigint;
rayPowUp: (exponent: BigNumberish) => bigint;
rayPowDown: (exponent: BigNumberish) => bigint;
rayExp3: (exponent: BigNumberish) => bigint;
rayExpN: (exponent: BigNumberish) => bigint;
rayToDecimals: (decimals: number) => bigint;
rayToPercent: () => bigint;
rayToWad: () => bigint;
Expand Down Expand Up @@ -198,8 +198,8 @@ BigInt.prototype.percentPowUp = function (exponent: BigNumberish) {
BigInt.prototype.percentPowDown = function (exponent: BigNumberish) {
return pow(this as bigint, exponent, PERCENT, mulDivDown);
};
BigInt.prototype.percentExp3 = function (exponent: BigNumberish) {
return exp3(this as bigint, exponent, PERCENT, mulDivDown);
BigInt.prototype.percentExpN = function (N: BigNumberish) {
return expN(this as bigint, N, PERCENT, mulDivDown);
};
BigInt.prototype.percentToDecimals = function (decimals: number) {
if (decimals <= 4) {
Expand Down Expand Up @@ -259,8 +259,8 @@ BigInt.prototype.wadPowUp = function (exponent: BigNumberish) {
BigInt.prototype.wadPowDown = function (exponent: BigNumberish) {
return pow(this as bigint, exponent, WAD, mulDivDown);
};
BigInt.prototype.wadExp3 = function (exponent: BigNumberish) {
return exp3(this as bigint, exponent, WAD, mulDivDown);
BigInt.prototype.wadExpN = function (N: BigNumberish) {
return expN(this as bigint, N, WAD, mulDivDown);
};
BigInt.prototype.wadToDecimals = function (decimals: number) {
if (decimals <= 18) {
Expand Down Expand Up @@ -320,8 +320,8 @@ BigInt.prototype.rayPowUp = function (exponent: BigNumberish) {
BigInt.prototype.rayPowDown = function (exponent: BigNumberish) {
return pow(this as bigint, exponent, RAY, mulDivDown);
};
BigInt.prototype.rayExp3 = function (exponent: BigNumberish) {
return exp3(this as bigint, exponent, RAY, mulDivDown);
BigInt.prototype.rayExpN = function (N: BigNumberish) {
return expN(this as bigint, N, RAY, mulDivDown);
};
BigInt.prototype.rayToDecimals = function (decimals: number) {
if (decimals <= 27) {
Expand Down
57 changes: 50 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,62 @@ export const pow = (
return mulDiv(x, pow(xSquared, (exponent - 1n) / 2n, scale, mulDiv), scale);
};

export const exp3 = (
export const expN = (
x: BigNumberish,
exponent: BigNumberish,
N: BigNumberish,
scale: BigNumberish,
mulDiv: MulDiv = mulDivDown,
) => {
x = toBigInt(x);
exponent = toBigInt(exponent);
scale = toBigInt(scale);
N = toBigInt(N);

let res = scale;
let monomial = scale;
for (let k = toBigInt(1); k <= N; k++) {
monomial = mulDiv(monomial, x, k * scale);
res += monomial;
}

return res;
};

const firstTerm = x * exponent;
const secondTerm = mulDiv(firstTerm, firstTerm, 2n * scale);
const thirdTerm = mulDiv(secondTerm, firstTerm, 3n * scale);
export const getConvertToAssets = (
virtualAssets: BigNumberish,
virtualShares: BigNumberish,
mulDiv: MulDiv,
) => {
virtualAssets = toBigInt(virtualAssets);
virtualShares = toBigInt(virtualShares);

return (shares: BigNumberish, totalAssets: BigNumberish, totalShares: BigNumberish) => {
totalAssets = toBigInt(totalAssets);
totalShares = toBigInt(totalShares);

return mulDiv(
shares,
totalAssets + (virtualAssets as bigint),
totalShares + (virtualShares as bigint),
);
};
};

return scale + firstTerm + secondTerm + thirdTerm;
export const getConvertToShares = (
virtualAssets: BigNumberish,
virtualShares: BigNumberish,
mulDiv: MulDiv,
) => {
virtualAssets = toBigInt(virtualAssets);
virtualShares = toBigInt(virtualShares);

return (assets: BigNumberish, totalAssets: BigNumberish, totalShares: BigNumberish) => {
totalAssets = toBigInt(totalAssets);
totalShares = toBigInt(totalShares);

return mulDiv(
assets,
totalShares + (virtualShares as bigint),
totalAssets + (virtualAssets as bigint),
);
};
};

0 comments on commit 7b50d72

Please sign in to comment.