我在语用稳健性方面有这个错误,>=0.7.0 <0.9.0
int256 constant REAL_BITS = 128;
int256 constant REAL_FBITS = 40;
int256 constant REAL_IBITS = REAL_BITS - REAL_FBITS;
int128 constant REAL_ONE = int128(1) << int128(REAL_FBITS);
int128 constant REAL_HALF = REAL_ONE >> int128(1);
int128 constant REAL_TWO = REAL_ONE << int128(1);错误:
发布于 2022-08-06 17:51:25
不能用有符号整数移位。解决这个问题的一种方法是
function shiftRightBySigned(int128 toShift, int128 shift) public pure returns(uint128 shifted) {
if(shift < 0) {
uint256 shiftUint = uint256(int256(shift * (-1)));
// Shift left to mimic what would happen if we shifted right by a negative number
shifted = toShift << shiftUint;
}
else {
uint256 shiftUint = uint256(int256(shift));
shifted = toShift >> shiftUint;
}
}如果你需要左转的话就换个方向。
发布于 2022-08-06 19:16:06
您还可以用它们的文字值替换所有的转换:
int256 constant REAL_BITS = 128;
int256 constant REAL_FBITS = 40;
int256 constant REAL_IBITS = REAL_BITS - REAL_FBITS;
int128 constant REAL_ONE = 1 << 40;
int128 constant REAL_HALF = REAL_ONE >> 1;
int128 constant REAL_TWO = REAL_ONE << 1;这个很管用。
在您的示例中,编译器很难解决类型问题。无论如何,当使用constant和immutable变量时,编译器将使用它们的文字类型替换它们。所以,如果您像我展示的那样直接使用文字类型,就没有问题了。
提示:
在稳健性中,我们可以使用文字数字进行任何算术运算,即使它们远远超出了它们的值范围,但最终我们需要将它们与我们想要用于它们的范围相匹配。
例如,我们可以这样做:
uint32 n = (2**800 + 1) - 2**800;正如我们所看到的,2 ** 800远远超出了Solidity支持的最大值范围,即2 ** 64。但是,由于我们正在提取2 ** 800,所以结果低于我们在示例中的预期类型,即uint32,并且工作良好。
https://docs.soliditylang.org/en/v0.8.15/types.html#rational-and-integer-literals
https://ethereum.stackexchange.com/questions/133079
复制相似问题