-
-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add UD21x18
and SD21x18
types
#212
Conversation
149e1ea
to
f402d59
Compare
@PaulRBerg just wanted to let you know that i've rebased from main and fixed the conflicts |
@andreivladbrg I will review this shortly. In the meantime, it would be helpful if you could write up a changelog and put it in the PR's comment body. This would help me when I release a new version containing the changes in this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
feedback below.
Accepted because I resolved all issues myself.
src/sd21x18/ValueType.sol
Outdated
|
||
import "./Casting.sol" as Casting; | ||
|
||
/// @notice The signed 1.18-decimal fixed-point number representation, which can have up to 1 digit and up to 18 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should say The signed 21.18-decimal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct
src/casting/Uint128.sol
Outdated
import { UD60x18 } from "../ud60x18/ValueType.sol"; | ||
|
||
/// @notice Thrown when trying to cast a uint128 that doesn't fit in SD1x18. | ||
error PRBMath_IntoSD1x18_Overflow(uint128 x); | ||
|
||
/// @notice Thrown when trying to cast a uint128 that doesn't fit in SD1x18. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SD21x18
src/casting/Uint128.sol
Outdated
function intoSD1x18(uint128 x) internal pure returns (SD1x18 result) { | ||
if (x > uint256(int256(uMAX_SD1x18))) { | ||
revert PRBMath_IntoSD1x18_Overflow(x); | ||
} | ||
result = SD1x18.wrap(int64(uint64(x))); | ||
} | ||
|
||
/// @notice Casts a uint256 number to SD21x18. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uint128
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct
src/casting/Uint128.sol
Outdated
function intoUD2x18(uint128 x) internal pure returns (UD2x18 result) { | ||
if (x > uint64(uMAX_UD2x18)) { | ||
revert PRBMath_IntoUD2x18_Overflow(x); | ||
} | ||
result = UD2x18.wrap(uint64(x)); | ||
} | ||
|
||
/// @notice Casts a uint128 number to UD21x18. | ||
/// @dev There is no overflow check because the domain of uint128 is a subset of UD21x18. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove this line - uint128
is not a 'subset' of UD21x18
.
uint128
is UD21x18
.
src/sd21x18/Casting.sol
Outdated
/// @notice Casts an SD21x18 number into UD2x18. | ||
/// - x must be positive. | ||
function intoUD2x18(SD21x18 x) pure returns (UD2x18 result) { | ||
int128 xInt = SD21x18.unwrap(x); | ||
if (xInt < 0) { | ||
revert CastingErrors.PRBMath_SD21x18_ToUD2x18_Underflow(x); | ||
} | ||
result = UD2x18.wrap(uint64(uint128(xInt))); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing check that x
is not greater than uMAX_UD2x18
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct, missed this
src/ud60x18/Casting.sol
Outdated
@@ -22,6 +26,17 @@ function intoSD1x18(UD60x18 x) pure returns (SD1x18 result) { | |||
result = SD1x18.wrap(int64(uint64(xUint))); | |||
} | |||
|
|||
/// @notice Casts a UD60x18 number into SD21x18. | |||
/// @dev Requirements: | |||
/// - x must be less than or equal to `uMAX_SD1x18`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uMAX_SD21x18
src/ud2x18/Casting.sol
Outdated
/// @notice Casts a UD2x18 number into SD59x18. | ||
/// @dev There is no overflow check because the domain of UD2x18 is a subset of SD59x18. | ||
function intoSD59x18(UD2x18 x) pure returns (SD59x18 result) { | ||
result = SD59x18.wrap(int256(uint256(UD2x18.unwrap(x)))); | ||
} | ||
|
||
/// @notice Casts a UD2x18 number into UD21x18. | ||
/// @dev There is no overflow check because the domain of UD2x18 is a subset of UD60x18. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of UD21x18
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fuzz test for intoSD21x18
not written.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct, missed them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fuzz test for intoSD21x18
not written.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fuzz test for intoSD21x18
not written.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct, missed them
src/sd21x18/ValueType.sol
Outdated
|
||
import "./Casting.sol" as Casting; | ||
|
||
/// @notice The signed 1.18-decimal fixed-point number representation, which can have up to 1 digit and up to 18 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct
src/casting/Uint128.sol
Outdated
function intoSD1x18(uint128 x) internal pure returns (SD1x18 result) { | ||
if (x > uint256(int256(uMAX_SD1x18))) { | ||
revert PRBMath_IntoSD1x18_Overflow(x); | ||
} | ||
result = SD1x18.wrap(int64(uint64(x))); | ||
} | ||
|
||
/// @notice Casts a uint256 number to SD21x18. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct
src/sd21x18/Casting.sol
Outdated
/// @notice Casts an SD21x18 number into UD2x18. | ||
/// - x must be positive. | ||
function intoUD2x18(SD21x18 x) pure returns (UD2x18 result) { | ||
int128 xInt = SD21x18.unwrap(x); | ||
if (xInt < 0) { | ||
revert CastingErrors.PRBMath_SD21x18_ToUD2x18_Underflow(x); | ||
} | ||
result = UD2x18.wrap(uint64(uint128(xInt))); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct, missed this
Closes #207
I've also added support for
SD21x18
, which is the type forint128
, its maximum value has the same number of decimals asuint128