-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Consistently use Real datatype #1400
Conversation
Thanks for opening this pull request! It might take a while before we look at it, so don't worry if there seems to be no feedback. We'll get to it. |
@@ -65,7 +65,7 @@ namespace QuantLib { | |||
//calculate payoff discount factor assuming continuous compounding | |||
DiscountFactor discount = riskFreeDiscount(t1); | |||
Real result = 0; | |||
constexpr Real minusInf = -std::numeric_limits<Real>::infinity(); | |||
const Real minusInf = -std::numeric_limits<Real>::infinity(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These and the others are constants, so I'd use double
as you did in the change right above. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We thought about that, but there could be an issue with this in the remote (admittedly unlikely) case that Real
is something else than double
with a different representation of infinity. If we define a constant with double-infinity, and then use it in calculations with Real
, it may become an issue.
The case above is different, as it's just a regular number, not infinity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd keep the double constants now and think about the unlikely case if it comes up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, that is just pushed and fixed as suggested.
ql/interestrate.hpp
Outdated
@@ -185,6 +185,15 @@ namespace QuantLib { | |||
Real freq_; | |||
}; | |||
|
|||
// helper to avoid relying on implicit conversion to Rate | |||
inline Rate operator-(const InterestRate& a, const InterestRate& b) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I didn't realize one could add or subtract InterestRate
instances. I would probably remove these operators and write an explicit call to rate
at the point of use, to avoid adding or subtracting rates with different conventions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We tend to agree on that point. However, in that case I would remove the implicit conversion operator in the InterestRate
class to Rate
(i.e. Real
) altogether. Otherwise such operations can be added without developers noticing.
One example where this is used is in ql/experimental/processes/extendedblackscholesprocess.cpp, but there are 15 uses in total throughout the library.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't disagree, but removing the implicit conversion would break backward compatibility.
Removing these operators at least would help us find and avoid implicit conversions in subtractions and additions, which are more at risk of inconsistency because they involve two different rates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's just pushed - we changed it at the point of use to an explicit call to rate
and removed those operators.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found 17 potential problems in the proposed changes. Check the Files changed tab for more details.
As mentioned by Luigi, |
Also, I think you will need to add a CI job to pass in the specific type of |
Using |
Right, so if somebody later makes a change to the library to use I think that preserving the ability to use the |
This is a good suggestion indeed @sweemer - that would prevent any stray use of |
Regarding the In general, a However, we suggest when/if this issue comes up, However, we think for this PR no further changes are needed and we can keep the constants as |
Congratulations on your first merged pull request! |
This replaces many stray uses of the
double
datatype withQuantLib::Real
, to ensure it is used consistently throughout the library and test suite.In particular:
double
withReal
std::inner_product
andstd::accumulate
withReal
literals, to ensure the values are aggregated with the correct data type internallyconstexpr Real x = ...
statements, as with the global typedef we cannot generally assume that Real can in fact be initialised in aconstexpr