Skip to content

Commit

Permalink
Fix negative denominator in boost::rational during exponentiation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ekpyron committed Nov 6, 2018
1 parent 88aee34 commit 5c63575
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions libsolidity/ast/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions libsolidity/ast/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ 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)
{
// 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 };

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
contract C {
function f() public pure returns (int) {
return (-1 / 2) ** -1;
}
}

0 comments on commit 5c63575

Please sign in to comment.