Skip to content
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

Fix negative denominator in boost::rational during exponentiation. #5348

Merged
merged 1 commit into from
Nov 8, 2018

Conversation

ekpyron
Copy link
Member

@ekpyron ekpyron commented Nov 5, 2018

Actual fix for the problem in #5347.
During exponentiation negative denominators may occur, resulting in boost 1.68 throwing an exception.

@ekpyron ekpyron self-assigned this Nov 5, 2018
@codecov
Copy link

codecov bot commented Nov 5, 2018

Codecov Report

Merging #5348 into develop will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #5348      +/-   ##
===========================================
+ Coverage       88%   88.01%   +<.01%     
===========================================
  Files          322      322              
  Lines        32489    32494       +5     
  Branches      3865     3866       +1     
===========================================
+ Hits         28593    28599       +6     
+ Misses        2592     2591       -1     
  Partials      1304     1304
Flag Coverage Δ
#all 88.01% <100%> (ø) ⬆️
#syntax 27.92% <100%> (+0.01%) ⬆️

@@ -1145,9 +1145,14 @@ TypePointer RationalNumberType::binaryOperatorResult(Token _operator, TypePointe

if (exp >= 0)
value = rational(numerator, denominator);
else
else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ to the next line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too long that I haven't written solidity code :-D.

// invert
value = rational(denominator, numerator);
// due to a bug in certain versions of boost the denominator cannot be negative
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is rather confusing because you swap numerator and denominator. Would it be better to create a function that is used to construct the rational which then adjusts the signs accordingly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's probably clearer, I moved it to a makeRational function.

@ekpyron ekpyron force-pushed the boostRationalNegativeDenominatorFix branch from 1530632 to 5c63575 Compare November 6, 2018 09:44
@ekpyron ekpyron force-pushed the boostRationalNegativeDenominatorFix branch from 5c63575 to e036133 Compare November 6, 2018 09:52
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be better suited in an anonymous namespace inside the cpp file or in a more central place inside libdevcore.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I don't think so - in this header we introduce the rational alias and - if we want to stay compatible with the buggy boost version - we will always want to use makeRational, when we create a rational from a numerator and denominator, so I think it makes a lot of sense to have it right here below the using rational = ......

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or if we want to move it, I'd at least move the using rational = ... part as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanly moving using rational = ... to libdevcore would require changing it's namespace, though - which we could do, but which may have side-effects. So actually I think right here is not too bad of a place for this for now :-).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Types.h would need some cleanup at some point anyway....

@chriseth chriseth merged commit cc2de07 into develop Nov 8, 2018
@ekpyron ekpyron deleted the boostRationalNegativeDenominatorFix branch November 8, 2018 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants