How to convert from unsigned to signed? #47
-
What's the best way of converting a unsinged fixed point uint256 (PRBMathUD60x18) into a signed fixed point uint256 (PRBMathSD59x18)? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
PRBMath comes with a multitude of castingand conversion functions. To convert from pragma solidity >=0.8.13;
import "@prb/math/UD60x18.sol";
contract YourContract {
function converUD60x18IntoSD59x18(UD60x18 x) external pure returns (SD59x18 result) {
uint256 xUint = UD60x18.unwrap(x);
require(xUint <= uint256(type(int256).max), "int256 overflow");
int256 xInt = int256(xUint);
result = SD59x18.wrap(xInt);
}
} I don't think that offering a cross-type convertor would be helpful, since the vast majority of PRBMath users import either the signed or the unsigned flavor (users rarely import both at the same time). In addition, for vanilla casting between |
Beta Was this translation helpful? Give feedback.
PRBMath comes with a multitude of castingand conversion functions. To convert from
UD60x18
toSD59x18
, you would do something like this:I don't think that offering a cross-type convertor would be helpful, since the vast majority of PRBMath users import either the signed or the unsigned flavor (users rarely import both at th…