Fix negative denominator in `boost::rational` during exponentiation.

This commit is contained in:
Daniel Kirchner 2018-11-05 23:52:45 +01:00
parent 88aee34c22
commit e036133d1b
3 changed files with 16 additions and 2 deletions

View File

@ -1144,10 +1144,10 @@ TypePointer RationalNumberType::binaryOperatorResult(Token _operator, TypePointe
bigint denominator = optimizedPow(m_value.denominator(), absExp);
if (exp >= 0)
value = rational(numerator, denominator);
value = makeRational(numerator, denominator);
else
// invert
value = rational(denominator, numerator);
value = makeRational(denominator, numerator);
}
break;
}

View File

@ -51,6 +51,15 @@ using FunctionTypePointer = std::shared_ptr<FunctionType const>;
using TypePointers = std::vector<TypePointer>;
using rational = boost::rational<dev::bigint>;
inline rational makeRational(bigint const& _numerator, bigint const& _denominator)
{
solAssert(_denominator != 0, "division by zero");
// due to a bug in certain versions of boost the denominator has to be positive
if (_denominator < 0)
return rational(-_numerator, -_denominator);
else
return rational(_numerator, _denominator);
}
enum class DataLocation { Storage, CallData, Memory };

View File

@ -0,0 +1,5 @@
contract C {
function f() public pure returns (int) {
return (-1 / 2) ** -1;
}
}