Skip to content

Commit

Permalink
refactor: turn "e" and "pi" into constants
Browse files Browse the repository at this point in the history
test: fix MulDiv18 constant
  • Loading branch information
PaulRBerg committed Sep 26, 2022
1 parent dcf9498 commit 422d870
Show file tree
Hide file tree
Showing 14 changed files with 18 additions and 110 deletions.
17 changes: 6 additions & 11 deletions contracts/SD59x18.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ error PRBMathSD59x18__ToSD59x18Underflow(int256 x);

/// CONSTANTS ///

/// @dev Euler's number as an SD59x18 number..
SD59x18 constant E = SD59x18.wrap(2_718281828459045235);

/// @dev Half the SCALE number.
SD59x18 constant HALF_SCALE = SD59x18.wrap(5e17);
int256 constant HALF_SCALE_INT = 5e17;
Expand Down Expand Up @@ -98,6 +101,9 @@ SD59x18 constant MIN_WHOLE_SD59x18 = SD59x18.wrap(
);
int256 constant MIN_WHOLE_SD59x18_INT = -57896044618658097711785492504343953926634992332820282019728_000000000000000000;

/// @dev PI as an SD59x18 number.
SD59x18 constant PI = SD59x18.wrap(3_141592653589793238);

/// @dev The unit amount which implies how many trailing decimals can be represented.
SD59x18 constant SCALE = SD59x18.wrap(1e18);
int256 constant SCALE_INT = 1e18;
Expand Down Expand Up @@ -853,24 +859,13 @@ function xor(SD59x18 x, SD59x18 y) pure returns (SD59x18 result) {

/// HELPER FUNCTIONS ///

/// @notice Returns Euler's number as an SD59x18 number.
/// @dev See https://en.wikipedia.org/wiki/E_(mathematical_constant).
function e() pure returns (SD59x18 result) {
result = SD59x18.wrap(2_718281828459045235);
}

/// @notice Converts an SD59x18 number to basic integer form, rounding down in the process.
/// @param x The SD59x18 number to convert.
/// @return result The same number in basic integer form.
function fromSD59x18(SD59x18 x) pure returns (int256 result) {
result = SD59x18.unwrap(x.uncheckedDiv(SCALE));
}

/// @notice Returns PI as an SD59x18 number.
function pi() pure returns (SD59x18 result) {
result = SD59x18.wrap(3_141592653589793238);
}

/// @notice Converts a number from basic integer form to SD59x18.
///
/// @dev Requirements:
Expand Down
17 changes: 6 additions & 11 deletions contracts/UD60x18.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ error PRBMathUD60x18__ToUD60x18Overflow(uint256 x);

/// CONSTANTS ///

/// @dev Euler's number as an UD60x18 number..
UD60x18 constant E = UD60x18.wrap(2_718281828459045235);

/// @dev Half the SCALE number.
UD60x18 constant HALF_SCALE = UD60x18.wrap(5e17);
uint256 constant HALF_SCALE_UINT = 5e17;
Expand All @@ -62,6 +65,9 @@ UD60x18 constant MAX_WHOLE_UD60x18 = UD60x18.wrap(
);
uint256 constant MAX_WHOLE_UD60x18_UINT = 115792089237316195423570985008687907853269984665640564039457_000000000000000000;

/// @dev PI as an UD60x18 number.
UD60x18 constant PI = UD60x18.wrap(3_141592653589793238);

/// @dev The unit amount which implies how many trailing decimals can be represented.
UD60x18 constant SCALE = UD60x18.wrap(1e18);
uint256 constant SCALE_UINT = 1e18;
Expand Down Expand Up @@ -625,24 +631,13 @@ function xor(UD60x18 x, UD60x18 y) pure returns (UD60x18 result) {

/// HELPER FUNCTIONS ///

/// @notice Returns Euler's number as an UD60x18 number.
/// @dev See https://en.wikipedia.org/wiki/E_(mathematical_constant).
function e() pure returns (UD60x18 result) {
result = UD60x18.wrap(2_718281828459045235);
}

/// @notice Converts an UD60x18 number to basic integer form, rounding down in the process.
/// @param x The UD60x18 number to convert.
/// @return result The same number in basic integer form.
function fromUD60x18(UD60x18 x) pure returns (uint256 result) {
result = UD60x18.unwrap(x.uncheckedDiv(SCALE));
}

/// @notice Returns PI as an UD60x18 number.
function pi() pure returns (UD60x18 result) {
result = UD60x18.wrap(3_141592653589793238);
}

/// @notice Converts a number from basic integer form to UD60x18.
///
/// @dev Requirements:
Expand Down
10 changes: 1 addition & 9 deletions contracts/test/PRBMathSD59x18Mock.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.13;

import { e, fromSD59x18, pi, SD59x18, toSD59x18 } from "../SD59x18.sol";
import { fromSD59x18, SD59x18, toSD59x18 } from "../SD59x18.sol";

contract PRBMathSD59x18Mock {
function doAbs(SD59x18 x) external pure returns (SD59x18 result) {
Expand Down Expand Up @@ -79,12 +79,4 @@ contract PRBMathSD59x18Mock {
function doToSD59x18(int256 x) external pure returns (SD59x18 result) {
result = toSD59x18(x);
}

function getE() external pure returns (SD59x18 result) {
result = e();
}

function getPi() external pure returns (SD59x18 result) {
result = pi();
}
}
10 changes: 1 addition & 9 deletions contracts/test/PRBMathUD60x18Mock.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.13;

import { e, fromUD60x18, pi, toUD60x18, UD60x18 } from "../UD60x18.sol";
import { fromUD60x18, toUD60x18, UD60x18 } from "../UD60x18.sol";

contract PRBMathUD60x18Mock {
function doAvg(UD60x18 x, UD60x18 y) external pure returns (UD60x18 result) {
Expand Down Expand Up @@ -75,12 +75,4 @@ contract PRBMathUD60x18Mock {
function doToUD60x18(uint256 x) external pure returns (UD60x18 result) {
result = toUD60x18(x);
}

function getE() external pure returns (UD60x18 result) {
result = e();
}

function getPi() external pure returns (UD60x18 result) {
result = pi();
}
}
10 changes: 0 additions & 10 deletions test/contracts/prbMathSd59x18/PRBMathSD59x18.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { shouldBehaveLikeAbs } from "./pure/abs.test";
import { shouldBehaveLikeAvg } from "./pure/avg.test";
import { shouldBehaveLikeCeil } from "./pure/ceil.test";
import { shouldBehaveLikeDiv } from "./pure/div.test";
import { shouldBehaveLikeEGetter } from "./pure/e.test";
import { shouldBehaveLikeExp2 } from "./pure/exp2.test";
import { shouldBehaveLikeExp } from "./pure/exp.test";
import { shouldBehaveLikeFloor } from "./pure/floor.test";
Expand All @@ -15,7 +14,6 @@ import { shouldBehaveLikeLn } from "./pure/ln.test";
import { shouldBehaveLikeLog2 } from "./pure/log2.test";
import { shouldBehaveLikeLog10 } from "./pure/log10.test";
import { shouldBehaveLikeMul } from "./pure/mul.test";
import { shouldBehaveLikePiGetter } from "./pure/pi.test";
import { shouldBehaveLikePow } from "./pure/pow.test";
import { shouldBehaveLikePowu } from "./pure/powu.test";
import { shouldBehaveLikeSqrt } from "./pure/sqrt.test";
Expand Down Expand Up @@ -96,14 +94,6 @@ export function unitTestPrbMathSd59x18(): void {
shouldBehaveLikeSqrt();
});

describe("e", function () {
shouldBehaveLikeEGetter();
});

describe("pi", function () {
shouldBehaveLikePiGetter();
});

describe("fromSD59x18", function () {
shouldBehaveLikeFromSD59x18();
});
Expand Down
11 changes: 0 additions & 11 deletions test/contracts/prbMathSd59x18/pure/e.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/contracts/prbMathSd59x18/pure/mul.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function shouldBehaveLikeMul(): void {

forEach(testSets).it("takes %e and %e and reverts", async function (x: BigNumber, y: BigNumber) {
await expect(this.contracts.prbMathSd59x18.doMul(x, y)).to.be.revertedWith(
PRBMathErrors.MUL_DIV_FIXED_POINT_OVERFLOW,
PRBMathErrors.MUL_DIV_18_OVERFLOW,
);
});
});
Expand Down
11 changes: 0 additions & 11 deletions test/contracts/prbMathSd59x18/pure/pi.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/contracts/prbMathSd59x18/pure/powu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function shouldBehaveLikePowu(): void {

forEach(testSets).it("takes %e and %e and reverts", async function (x: BigNumber, y: BigNumber) {
await expect(this.contracts.prbMathSd59x18.doPowu(x, y)).to.be.revertedWith(
PRBMathErrors.MUL_DIV_FIXED_POINT_OVERFLOW,
PRBMathErrors.MUL_DIV_18_OVERFLOW,
);
});
});
Expand Down
10 changes: 0 additions & 10 deletions test/contracts/prbMathUd60x18/PRBMathUD60x18.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { unitFixturePRBMathUd60x18 } from "../../shared/fixtures";
import { shouldBehaveLikeAvg } from "./pure/avg.test";
import { shouldBehaveLikeCeil } from "./pure/ceil.test";
import { shouldBehaveLikeDiv } from "./pure/div.test";
import { shouldBehaveLikeEGetter } from "./pure/e.test";
import { shouldBehaveLikeExp2 } from "./pure/exp2.test";
import { shouldBehaveLikeExp } from "./pure/exp.test";
import { shouldBehaveLikeFloor } from "./pure/floor.test";
Expand All @@ -14,7 +13,6 @@ import { shouldBehaveLikeLn } from "./pure/ln.test";
import { shouldBehaveLikeLog2 } from "./pure/log2.test";
import { shouldBehaveLikeLog10 } from "./pure/log10.test";
import { shouldBehaveLikeMul } from "./pure/mul.test";
import { shouldBehaveLikePiGetter } from "./pure/pi.test";
import { shouldBehaveLikePow } from "./pure/pow.test";
import { shouldBehaveLikePowu } from "./pure/powu.test";
import { shouldBehaveLikeSqrt } from "./pure/sqrt.test";
Expand Down Expand Up @@ -95,14 +93,6 @@ export function unitTestPrbMathUd60x18(): void {
shouldBehaveLikeFromUD60x18();
});

describe("e", function () {
shouldBehaveLikeEGetter();
});

describe("pi", function () {
shouldBehaveLikePiGetter();
});

describe("toUD60x18", function () {
shouldBehaveLikeToUD60x18();
});
Expand Down
11 changes: 0 additions & 11 deletions test/contracts/prbMathUd60x18/pure/e.test.ts

This file was deleted.

4 changes: 1 addition & 3 deletions test/contracts/prbMathUd60x18/pure/mul.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ export function shouldBehaveLikeMul(): void {
];

forEach(testSets).it("takes %e and %e and reverts", async function (x: BigNumber, y: BigNumber) {
await expect(this.contracts.prbMathUd60x18.doMul(x, y)).to.be.revertedWith(
PRBMathErrors.MUL_DIV_FIXED_POINT_OVERFLOW,
);
await expect(this.contracts.prbMathUd60x18.doMul(x, y)).to.be.revertedWith(PRBMathErrors.MUL_DIV_18_OVERFLOW);
});
});

Expand Down
11 changes: 0 additions & 11 deletions test/contracts/prbMathUd60x18/pure/pi.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/contracts/prbMathUd60x18/pure/powu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function shouldBehaveLikePowu(): void {

forEach(testSets).it("takes %e and %e and reverts", async function (x: BigNumber, y: BigNumber) {
await expect(this.contracts.prbMathUd60x18.doPowu(x, y)).to.be.revertedWith(
PRBMathErrors.MUL_DIV_FIXED_POINT_OVERFLOW,
PRBMathErrors.MUL_DIV_18_OVERFLOW,
);
});
});
Expand Down

0 comments on commit 422d870

Please sign in to comment.