Skip to content

Commit 422d870

Browse files
committed
refactor: turn "e" and "pi" into constants
test: fix MulDiv18 constant
1 parent dcf9498 commit 422d870

14 files changed

Lines changed: 18 additions & 110 deletions

File tree

‎contracts/SD59x18.sol‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ error PRBMathSD59x18__ToSD59x18Underflow(int256 x);
6262

6363
/// CONSTANTS ///
6464

65+
/// @dev Euler's number as an SD59x18 number..
66+
SD59x18 constant E = SD59x18.wrap(2_718281828459045235);
67+
6568
/// @dev Half the SCALE number.
6669
SD59x18 constant HALF_SCALE = SD59x18.wrap(5e17);
6770
int256 constant HALF_SCALE_INT = 5e17;
@@ -98,6 +101,9 @@ SD59x18 constant MIN_WHOLE_SD59x18 = SD59x18.wrap(
98101
);
99102
int256 constant MIN_WHOLE_SD59x18_INT = -57896044618658097711785492504343953926634992332820282019728_000000000000000000;
100103

104+
/// @dev PI as an SD59x18 number.
105+
SD59x18 constant PI = SD59x18.wrap(3_141592653589793238);
106+
101107
/// @dev The unit amount which implies how many trailing decimals can be represented.
102108
SD59x18 constant SCALE = SD59x18.wrap(1e18);
103109
int256 constant SCALE_INT = 1e18;
@@ -853,24 +859,13 @@ function xor(SD59x18 x, SD59x18 y) pure returns (SD59x18 result) {
853859

854860
/// HELPER FUNCTIONS ///
855861

856-
/// @notice Returns Euler's number as an SD59x18 number.
857-
/// @dev See https://en.wikipedia.org/wiki/E_(mathematical_constant).
858-
function e() pure returns (SD59x18 result) {
859-
result = SD59x18.wrap(2_718281828459045235);
860-
}
861-
862862
/// @notice Converts an SD59x18 number to basic integer form, rounding down in the process.
863863
/// @param x The SD59x18 number to convert.
864864
/// @return result The same number in basic integer form.
865865
function fromSD59x18(SD59x18 x) pure returns (int256 result) {
866866
result = SD59x18.unwrap(x.uncheckedDiv(SCALE));
867867
}
868868

869-
/// @notice Returns PI as an SD59x18 number.
870-
function pi() pure returns (SD59x18 result) {
871-
result = SD59x18.wrap(3_141592653589793238);
872-
}
873-
874869
/// @notice Converts a number from basic integer form to SD59x18.
875870
///
876871
/// @dev Requirements:

‎contracts/UD60x18.sol‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ error PRBMathUD60x18__ToUD60x18Overflow(uint256 x);
3838

3939
/// CONSTANTS ///
4040

41+
/// @dev Euler's number as an UD60x18 number..
42+
UD60x18 constant E = UD60x18.wrap(2_718281828459045235);
43+
4144
/// @dev Half the SCALE number.
4245
UD60x18 constant HALF_SCALE = UD60x18.wrap(5e17);
4346
uint256 constant HALF_SCALE_UINT = 5e17;
@@ -62,6 +65,9 @@ UD60x18 constant MAX_WHOLE_UD60x18 = UD60x18.wrap(
6265
);
6366
uint256 constant MAX_WHOLE_UD60x18_UINT = 115792089237316195423570985008687907853269984665640564039457_000000000000000000;
6467

68+
/// @dev PI as an UD60x18 number.
69+
UD60x18 constant PI = UD60x18.wrap(3_141592653589793238);
70+
6571
/// @dev The unit amount which implies how many trailing decimals can be represented.
6672
UD60x18 constant SCALE = UD60x18.wrap(1e18);
6773
uint256 constant SCALE_UINT = 1e18;
@@ -625,24 +631,13 @@ function xor(UD60x18 x, UD60x18 y) pure returns (UD60x18 result) {
625631

626632
/// HELPER FUNCTIONS ///
627633

628-
/// @notice Returns Euler's number as an UD60x18 number.
629-
/// @dev See https://en.wikipedia.org/wiki/E_(mathematical_constant).
630-
function e() pure returns (UD60x18 result) {
631-
result = UD60x18.wrap(2_718281828459045235);
632-
}
633-
634634
/// @notice Converts an UD60x18 number to basic integer form, rounding down in the process.
635635
/// @param x The UD60x18 number to convert.
636636
/// @return result The same number in basic integer form.
637637
function fromUD60x18(UD60x18 x) pure returns (uint256 result) {
638638
result = UD60x18.unwrap(x.uncheckedDiv(SCALE));
639639
}
640640

641-
/// @notice Returns PI as an UD60x18 number.
642-
function pi() pure returns (UD60x18 result) {
643-
result = UD60x18.wrap(3_141592653589793238);
644-
}
645-
646641
/// @notice Converts a number from basic integer form to UD60x18.
647642
///
648643
/// @dev Requirements:

‎contracts/test/PRBMathSD59x18Mock.sol‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Unlicense
22
pragma solidity >=0.8.13;
33

4-
import { e, fromSD59x18, pi, SD59x18, toSD59x18 } from "../SD59x18.sol";
4+
import { fromSD59x18, SD59x18, toSD59x18 } from "../SD59x18.sol";
55

66
contract PRBMathSD59x18Mock {
77
function doAbs(SD59x18 x) external pure returns (SD59x18 result) {
@@ -79,12 +79,4 @@ contract PRBMathSD59x18Mock {
7979
function doToSD59x18(int256 x) external pure returns (SD59x18 result) {
8080
result = toSD59x18(x);
8181
}
82-
83-
function getE() external pure returns (SD59x18 result) {
84-
result = e();
85-
}
86-
87-
function getPi() external pure returns (SD59x18 result) {
88-
result = pi();
89-
}
9082
}

‎contracts/test/PRBMathUD60x18Mock.sol‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Unlicense
22
pragma solidity >=0.8.13;
33

4-
import { e, fromUD60x18, pi, toUD60x18, UD60x18 } from "../UD60x18.sol";
4+
import { fromUD60x18, toUD60x18, UD60x18 } from "../UD60x18.sol";
55

66
contract PRBMathUD60x18Mock {
77
function doAvg(UD60x18 x, UD60x18 y) external pure returns (UD60x18 result) {
@@ -75,12 +75,4 @@ contract PRBMathUD60x18Mock {
7575
function doToUD60x18(uint256 x) external pure returns (UD60x18 result) {
7676
result = toUD60x18(x);
7777
}
78-
79-
function getE() external pure returns (UD60x18 result) {
80-
result = e();
81-
}
82-
83-
function getPi() external pure returns (UD60x18 result) {
84-
result = pi();
85-
}
8678
}

‎test/contracts/prbMathSd59x18/PRBMathSD59x18.test.ts‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { shouldBehaveLikeAbs } from "./pure/abs.test";
33
import { shouldBehaveLikeAvg } from "./pure/avg.test";
44
import { shouldBehaveLikeCeil } from "./pure/ceil.test";
55
import { shouldBehaveLikeDiv } from "./pure/div.test";
6-
import { shouldBehaveLikeEGetter } from "./pure/e.test";
76
import { shouldBehaveLikeExp2 } from "./pure/exp2.test";
87
import { shouldBehaveLikeExp } from "./pure/exp.test";
98
import { shouldBehaveLikeFloor } from "./pure/floor.test";
@@ -15,7 +14,6 @@ import { shouldBehaveLikeLn } from "./pure/ln.test";
1514
import { shouldBehaveLikeLog2 } from "./pure/log2.test";
1615
import { shouldBehaveLikeLog10 } from "./pure/log10.test";
1716
import { shouldBehaveLikeMul } from "./pure/mul.test";
18-
import { shouldBehaveLikePiGetter } from "./pure/pi.test";
1917
import { shouldBehaveLikePow } from "./pure/pow.test";
2018
import { shouldBehaveLikePowu } from "./pure/powu.test";
2119
import { shouldBehaveLikeSqrt } from "./pure/sqrt.test";
@@ -96,14 +94,6 @@ export function unitTestPrbMathSd59x18(): void {
9694
shouldBehaveLikeSqrt();
9795
});
9896

99-
describe("e", function () {
100-
shouldBehaveLikeEGetter();
101-
});
102-
103-
describe("pi", function () {
104-
shouldBehaveLikePiGetter();
105-
});
106-
10797
describe("fromSD59x18", function () {
10898
shouldBehaveLikeFromSD59x18();
10999
});

‎test/contracts/prbMathSd59x18/pure/e.test.ts‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎test/contracts/prbMathSd59x18/pure/mul.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function shouldBehaveLikeMul(): void {
7777

7878
forEach(testSets).it("takes %e and %e and reverts", async function (x: BigNumber, y: BigNumber) {
7979
await expect(this.contracts.prbMathSd59x18.doMul(x, y)).to.be.revertedWith(
80-
PRBMathErrors.MUL_DIV_FIXED_POINT_OVERFLOW,
80+
PRBMathErrors.MUL_DIV_18_OVERFLOW,
8181
);
8282
});
8383
});

‎test/contracts/prbMathSd59x18/pure/pi.test.ts‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎test/contracts/prbMathSd59x18/pure/powu.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function shouldBehaveLikePowu(): void {
6262

6363
forEach(testSets).it("takes %e and %e and reverts", async function (x: BigNumber, y: BigNumber) {
6464
await expect(this.contracts.prbMathSd59x18.doPowu(x, y)).to.be.revertedWith(
65-
PRBMathErrors.MUL_DIV_FIXED_POINT_OVERFLOW,
65+
PRBMathErrors.MUL_DIV_18_OVERFLOW,
6666
);
6767
});
6868
});

‎test/contracts/prbMathUd60x18/PRBMathUD60x18.test.ts‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { unitFixturePRBMathUd60x18 } from "../../shared/fixtures";
22
import { shouldBehaveLikeAvg } from "./pure/avg.test";
33
import { shouldBehaveLikeCeil } from "./pure/ceil.test";
44
import { shouldBehaveLikeDiv } from "./pure/div.test";
5-
import { shouldBehaveLikeEGetter } from "./pure/e.test";
65
import { shouldBehaveLikeExp2 } from "./pure/exp2.test";
76
import { shouldBehaveLikeExp } from "./pure/exp.test";
87
import { shouldBehaveLikeFloor } from "./pure/floor.test";
@@ -14,7 +13,6 @@ import { shouldBehaveLikeLn } from "./pure/ln.test";
1413
import { shouldBehaveLikeLog2 } from "./pure/log2.test";
1514
import { shouldBehaveLikeLog10 } from "./pure/log10.test";
1615
import { shouldBehaveLikeMul } from "./pure/mul.test";
17-
import { shouldBehaveLikePiGetter } from "./pure/pi.test";
1816
import { shouldBehaveLikePow } from "./pure/pow.test";
1917
import { shouldBehaveLikePowu } from "./pure/powu.test";
2018
import { shouldBehaveLikeSqrt } from "./pure/sqrt.test";
@@ -95,14 +93,6 @@ export function unitTestPrbMathUd60x18(): void {
9593
shouldBehaveLikeFromUD60x18();
9694
});
9795

98-
describe("e", function () {
99-
shouldBehaveLikeEGetter();
100-
});
101-
102-
describe("pi", function () {
103-
shouldBehaveLikePiGetter();
104-
});
105-
10696
describe("toUD60x18", function () {
10797
shouldBehaveLikeToUD60x18();
10898
});

0 commit comments

Comments
 (0)