From 89ead9e724a8ec2f71fdb7e2067bfd94cec88ee4 Mon Sep 17 00:00:00 2001 From: Luigi Ballabio Date: Sat, 6 Nov 2021 11:18:15 +0100 Subject: [PATCH] Avoid syntax deprecated in C++20 --- ql/cashflows/cashflows.cpp | 2 +- ql/cashflows/conundrumpricer.cpp | 17 ++--- .../credit/blackcdsoptionengine.cpp | 2 +- .../haganirregularswaptionengine.cpp | 3 +- ql/math/interpolations/multicubicspline.hpp | 58 +++++++-------- ql/pricingengines/blackformula.cpp | 71 +++++++++++-------- .../capfloor/bacheliercapfloorengine.cpp | 2 +- .../capfloor/blackcapfloorengine.cpp | 2 +- .../volatility/swaption/swaptionvolcube1.hpp | 4 +- test-suite/amortizingbond.cpp | 2 +- test-suite/zerocouponswap.cpp | 2 +- 11 files changed, 90 insertions(+), 75 deletions(-) diff --git a/ql/cashflows/cashflows.cpp b/ql/cashflows/cashflows.cpp index 91f0cdb4d19..df9113aa153 100644 --- a/ql/cashflows/cashflows.cpp +++ b/ql/cashflows/cashflows.cpp @@ -710,7 +710,7 @@ namespace QuantLib { QL_REQUIRE(y.compounding() == Compounded, "compounded rate required"); - return (1.0+y.rate()/y.frequency()) * + return (1.0+y.rate()/Integer(y.frequency())) * modifiedDuration(leg, y, includeSettlementDateFlows, settlementDate, npvDate); diff --git a/ql/cashflows/conundrumpricer.cpp b/ql/cashflows/conundrumpricer.cpp index 95eb0722652..9b4da050e12 100644 --- a/ql/cashflows/conundrumpricer.cpp +++ b/ql/cashflows/conundrumpricer.cpp @@ -340,7 +340,7 @@ namespace QuantLib { // v. HAGAN, Conundrums..., formule 2.17a, 2.18a return coupon_->accrualPeriod() * (discount_/annuity_) * - ((1 + dFdK) * swaptionPrice + optionType*integralValue); + ((1 + dFdK) * swaptionPrice + Integer(optionType) * integralValue); } Real NumericHaganPricer::swapletPrice() const { @@ -475,13 +475,14 @@ namespace QuantLib { const Real dminus12 = (lnRoverK-.5*variance)/sqrtSigma2T; CumulativeNormalDistribution cumulativeOfNormal; - const Real N32 = cumulativeOfNormal(optionType*d32); - const Real N12 = cumulativeOfNormal(optionType*d12); - const Real Nminus12 = cumulativeOfNormal(optionType*dminus12); - - price += optionType * firstDerivativeOfGAtForwardValue * annuity_ * - swapRateValue_ * (swapRateValue_ * std::exp(variance) * N32- - (swapRateValue_+strike) * N12 + strike * Nminus12); + Integer sign = Integer(optionType); + const Real N32 = cumulativeOfNormal(sign * d32); + const Real N12 = cumulativeOfNormal(sign * d12); + const Real Nminus12 = cumulativeOfNormal(sign * dminus12); + + price += sign * firstDerivativeOfGAtForwardValue * annuity_ * + swapRateValue_ * (swapRateValue_ * std::exp(variance) * N32 - + (swapRateValue_ + strike) * N12 + strike * Nminus12); price *= coupon_->accrualPeriod(); return price; } diff --git a/ql/experimental/credit/blackcdsoptionengine.cpp b/ql/experimental/credit/blackcdsoptionengine.cpp index 5e3975a20c1..d755b55c2eb 100644 --- a/ql/experimental/credit/blackcdsoptionengine.cpp +++ b/ql/experimental/credit/blackcdsoptionengine.cpp @@ -71,7 +71,7 @@ namespace QuantLib { // if a non knock-out payer option, add front end protection value if (arguments_.side == Protection::Buyer && !arguments_.knocksOut) { Real frontEndProtection = - callPut * arguments_.swap->notional() + Integer(callPut) * arguments_.swap->notional() * (1.-recoveryRate_) * probability_->defaultProbability(exerciseDate) * termStructure_->discount(exerciseDate); diff --git a/ql/experimental/swaptions/haganirregularswaptionengine.cpp b/ql/experimental/swaptions/haganirregularswaptionengine.cpp index 4fe977f641a..9e77e43284c 100644 --- a/ql/experimental/swaptions/haganirregularswaptionengine.cpp +++ b/ql/experimental/swaptions/haganirregularswaptionengine.cpp @@ -178,7 +178,8 @@ namespace QuantLib { Real defect = -targetNPV_; - for(Size i=0; i< weights.size();++i) defect -= swap_->type()*lambda*weights[i]*annuities_[i]; + for (Size i=0; i< weights.size();++i) + defect -= Integer(swap_->type()) * lambda*weights[i] * annuities_[i]; return defect; } diff --git a/ql/math/interpolations/multicubicspline.hpp b/ql/math/interpolations/multicubicspline.hpp index 1fa23a6b15a..435375897a2 100644 --- a/ql/math/interpolations/multicubicspline.hpp +++ b/ql/math/interpolations/multicubicspline.hpp @@ -46,15 +46,15 @@ namespace QuantLib { struct EmptyDim {}; // size_t termination marker template struct DataTable { - DataTable(const std::vector::const_iterator &i) { + DataTable(const std::vector::const_iterator &i) { std::vector temp(*i, X(i + 1)); data_table_.swap(temp); } - DataTable(const SplineGrid::const_iterator &i) { + DataTable(const SplineGrid::const_iterator &i) { std::vector temp(i->size(), X(i + 1)); data_table_.swap(temp); } - template DataTable(const std::vector &v) { + template DataTable(const std::vector &v) { DataTable temp(v.begin()); data_table_.swap(temp.data_table_); } @@ -67,12 +67,12 @@ namespace QuantLib { }; template<> struct DataTable { - DataTable(Size n) : data_table_(n) {} - DataTable(const std::vector::const_iterator& i) + DataTable(Size n) : data_table_(n) {} + DataTable(const std::vector::const_iterator& i) : data_table_(*i) {} - DataTable(const SplineGrid::const_iterator &i) + DataTable(const SplineGrid::const_iterator &i) : data_table_(i->size()) {} - template DataTable(const std::vector &v) { + template DataTable(const std::vector &v) { DataTable temp(v.begin()); data_table_.swap(temp.data_table_); } @@ -87,11 +87,11 @@ namespace QuantLib { typedef DataTable base_data_table; template struct Data { - Data() + Data() : first(X()), second(Y()) {} - Data(const SplineGrid::const_iterator &i) + Data(const SplineGrid::const_iterator &i) : first(*i), second(i + 1) {} - Data(const SplineGrid &v) + Data(const SplineGrid &v) : first(v[0]), second(v.begin()+1) {} void swap(Data &d) { first.swap(d.first); @@ -102,13 +102,13 @@ namespace QuantLib { }; template<> struct Data, EmptyArg> { - Data, EmptyArg>() + Data() : first(std::vector()) {} - Data, EmptyArg>(const SplineGrid::const_iterator &i) + Data(const SplineGrid::const_iterator &i) : first(*i) {} - Data, EmptyArg>(const SplineGrid &v) + Data(const SplineGrid &v) : first(v[0]) {} - Data, EmptyArg>(std::vector v) : first(std::move(v)) {} + Data(std::vector v) : first(std::move(v)) {} void swap(Data, EmptyArg> &d) { first.swap(d.first); } @@ -122,15 +122,15 @@ namespace QuantLib { template struct Point { typedef X data_type; - Point() + Point() : first(data_type()), second(Y()) {} - Point(const std::vector::const_iterator &i) + Point(const std::vector::const_iterator &i) : first(*i), second(i + 1) {} - Point(const std::vector &v) + Point(const std::vector &v) : first(v[0]), second(v.begin()+1) {} - Point(const SplineGrid::const_iterator &i) + Point(const SplineGrid::const_iterator &i) : first(i->size()), second(i + 1) {} - Point(const SplineGrid &grid) + Point(const SplineGrid &grid) : first(grid[0].size()), second(grid.begin()+1) {} operator data_type() const { return first; @@ -143,11 +143,11 @@ namespace QuantLib { template<> struct Point { typedef Real data_type; - Point(data_type s) + Point(data_type s) : first(s) {} - Point(const std::vector::const_iterator &i) + Point(const std::vector::const_iterator &i) : first(*i) {} - Point(const std::vector &v) + Point(const std::vector &v) : first(v[0]) {} operator data_type() const {return first;} data_type operator[](Size n) const { @@ -166,9 +166,9 @@ namespace QuantLib { template<> struct Point { typedef Real data_type; - Point() + Point() : first(data_type()) {} - Point(data_type s) + Point(data_type s) : first(s) {} operator data_type() const {return first;} const data_type &operator[](Size n) const { @@ -187,9 +187,9 @@ namespace QuantLib { template<> struct Point { typedef Size data_type; - Point() + Point() : first(data_type()) {} - Point(data_type s) + Point(data_type s) : first(s) {} operator data_type() const {return first;} data_type operator[](Size n) const { @@ -208,10 +208,10 @@ namespace QuantLib { template<> struct Point { typedef base_data_table data_type; - Point(data_type s) : first(std::move(s)) {} - Point(const SplineGrid::const_iterator &i) + Point(data_type s) : first(std::move(s)) {} + Point(const SplineGrid::const_iterator &i) : first(i->size()) {} - Point(const SplineGrid &grid) + Point(const SplineGrid &grid) : first(grid[0].size()) {} Real operator[](Size n) const {return first[n];} Real& operator[](Size n) {return first[n];} diff --git a/ql/pricingengines/blackformula.cpp b/ql/pricingengines/blackformula.cpp index 6f3f3e6f202..46434893f1d 100644 --- a/ql/pricingengines/blackformula.cpp +++ b/ql/pricingengines/blackformula.cpp @@ -76,8 +76,10 @@ namespace QuantLib { QL_REQUIRE(discount>0.0, "discount (" << discount << ") must be positive"); - if (stdDev==0.0) - return std::max((forward-strike)*optionType, Real(0.0))*discount; + Integer sign = Integer(optionType); + + if (stdDev == 0.0) + return std::max((forward-strike) * sign, Real(0.0)) * discount; forward = forward + displacement; strike = strike + displacement; @@ -90,9 +92,9 @@ namespace QuantLib { Real d1 = std::log(forward/strike)/stdDev + 0.5*stdDev; Real d2 = d1 - stdDev; CumulativeNormalDistribution phi; - Real nd1 = phi(optionType*d1); - Real nd2 = phi(optionType*d2); - Real result = discount * optionType * (forward*nd1 - strike*nd2); + Real nd1 = phi(sign * d1); + Real nd2 = phi(sign * d2); + Real result = discount * sign * (forward*nd1 - strike*nd2); QL_ENSURE(result>=0.0, "negative value (" << result << ") for " << stdDev << " stdDev, " << @@ -123,19 +125,21 @@ namespace QuantLib { "stdDev (" << stdDev << ") must be non-negative"); QL_REQUIRE(discount>0.0, "discount (" << discount << ") must be positive"); - - if (stdDev==0.0) - return optionType * std::max(1.0 * boost::math::sign((forward - strike) * optionType), 0.0) * discount; + + Integer sign = Integer(optionType); + + if (stdDev == 0.0) + return sign * std::max(1.0 * boost::math::sign((forward - strike) * sign), 0.0) * discount; forward = forward + displacement; strike = strike + displacement; - if (strike==0.0) - return (optionType==Option::Call ? discount : 0.0); + if (strike == 0.0) + return (optionType == Option::Call ? discount : 0.0); Real d1 = std::log(forward/strike)/stdDev + 0.5*stdDev; CumulativeNormalDistribution phi; - return optionType * phi(optionType * d1) * discount; + return sign * phi(sign * d1) * discount; } Real blackFormulaForwardDerivative(const ext::shared_ptr& payoff, @@ -169,7 +173,7 @@ namespace QuantLib { stdDev = blackPrice/discount*std::sqrt(2.0 * M_PI)/forward; else { // Corrado and Miller extended moneyness approximation - Real moneynessDelta = optionType*(forward-strike); + Real moneynessDelta = Integer(optionType) * (forward-strike); Real moneynessDelta_2 = moneynessDelta/2.0; Real temp = blackPrice/discount - moneynessDelta_2; Real moneynessDelta_PI = moneynessDelta*moneynessDelta/M_PI; @@ -337,16 +341,18 @@ namespace QuantLib { Real forward, Real undiscountedBlackPrice, Real displacement = 0.0) - : halfOptionType_(0.5*optionType), signedStrike_(optionType*(strike+displacement)), - signedForward_(optionType*(forward+displacement)), + : halfOptionType_(0.5 * Integer(optionType)), + signedStrike_(Integer(optionType) * (strike+displacement)), + signedForward_(Integer(optionType) * (forward+displacement)), undiscountedBlackPrice_(undiscountedBlackPrice) { checkParameters(strike, forward, displacement); QL_REQUIRE(undiscountedBlackPrice>=0.0, "undiscounted Black price (" << undiscountedBlackPrice << ") must be non-negative"); - signedMoneyness_ = optionType*std::log((forward+displacement)/(strike+displacement)); + signedMoneyness_ = Integer(optionType) * std::log((forward+displacement)/(strike+displacement)); } + Real operator()(Real stdDev) const { #if defined(QL_EXTRA_SAFETY_CHECKS) QL_REQUIRE(stdDev>=0.0, @@ -398,7 +404,7 @@ namespace QuantLib { QL_REQUIRE(blackPrice>=0.0, "option price (" << blackPrice << ") must be non-negative"); // check the price of the "other" option implied by put-call paity - Real otherOptionPrice = blackPrice - optionType*(forward-strike)*discount; + Real otherOptionPrice = blackPrice - Integer(optionType) * (forward-strike)*discount; QL_REQUIRE(otherOptionPrice>=0.0, "negative " << Option::Type(-1*optionType) << " price (" << otherOptionPrice << @@ -567,16 +573,19 @@ namespace QuantLib { Real stdDev, Real displacement) { checkParameters(strike, forward, displacement); + + Integer sign = Integer(optionType); + if (stdDev==0.0) - return (forward*optionType > strike*optionType ? 1.0 : 0.0); + return (forward * sign > strike * sign ? 1.0 : 0.0); forward = forward + displacement; strike = strike + displacement; if (strike==0.0) - return (optionType==Option::Call ? 1.0 : 0.0); + return (optionType == Option::Call ? 1.0 : 0.0); Real d2 = std::log(forward/strike)/stdDev - 0.5*stdDev; CumulativeNormalDistribution phi; - return phi(optionType*d2); + return phi(sign * d2); } Real blackFormulaCashItmProbability( @@ -595,16 +604,19 @@ namespace QuantLib { Real stdDev, Real displacement) { checkParameters(strike, forward, displacement); + + Integer sign = Integer(optionType); + if (stdDev==0.0) - return (forward*optionType < strike*optionType ? 1.0 : 0.0); + return (forward * sign < strike * sign ? 1.0 : 0.0); forward = forward + displacement; strike = strike + displacement; - if (strike==0.0) - return (optionType==Option::Call ? 1.0 : 0.0); + if (strike == 0.0) + return (optionType == Option::Call ? 1.0 : 0.0); Real d1 = std::log(forward/strike)/stdDev + 0.5*stdDev; CumulativeNormalDistribution phi; - return phi(optionType*d1); + return phi(sign * d1); } Real blackFormulaAssetItmProbability( @@ -707,7 +719,7 @@ namespace QuantLib { "stdDev (" << stdDev << ") must be non-negative"); QL_REQUIRE(discount>0.0, "discount (" << discount << ") must be positive"); - Real d = (forward-strike)*optionType, h = d/stdDev; + Real d = (forward-strike) * Integer(optionType), h = d / stdDev; if (stdDev==0.0) return discount*std::max(d, 0.0); CumulativeNormalDistribution phi; @@ -737,11 +749,12 @@ namespace QuantLib { "stdDev (" << stdDev << ") must be non-negative"); QL_REQUIRE(discount>0.0, "discount (" << discount << ") must be positive"); - if (stdDev==0.0) - return optionType * std::max(1.0 * boost::math::sign((forward - strike) * optionType), 0.0) * discount; - Real d = (forward-strike)*optionType, h = d/stdDev; + Integer sign = Integer(optionType); + if (stdDev == 0.0) + return sign * std::max(1.0 * boost::math::sign((forward - strike) * sign), 0.0) * discount; + Real d = (forward - strike) * sign, h = d / stdDev; CumulativeNormalDistribution phi; - return optionType * phi(h) * discount; + return sign * phi(h) * discount; } Real bachelierBlackFormulaForwardDerivative( @@ -863,7 +876,7 @@ namespace QuantLib { Real stdDev) { QL_REQUIRE(stdDev>=0.0, "stdDev (" << stdDev << ") must be non-negative"); - Real d = (forward-strike)*optionType, h = d/stdDev; + Real d = (forward - strike) * Integer(optionType), h = d / stdDev; if (stdDev==0.0) return std::max(d, 0.0); CumulativeNormalDistribution phi; diff --git a/ql/pricingengines/capfloor/bacheliercapfloorengine.cpp b/ql/pricingengines/capfloor/bacheliercapfloorengine.cpp index b60287959e0..43cdf23bd55 100644 --- a/ql/pricingengines/capfloor/bacheliercapfloorengine.cpp +++ b/ql/pricingengines/capfloor/bacheliercapfloorengine.cpp @@ -115,7 +115,7 @@ namespace QuantLib { strike)); floorletVega = bachelierBlackFormulaStdDevDerivative(strike, forward, stdDevs[i], discountedAccrual) * sqrtTime; - floorletDelta = Option::Put * bachelierBlackFormulaAssetItmProbability( + floorletDelta = Integer(Option::Put) * bachelierBlackFormulaAssetItmProbability( Option::Put, strike, forward, stdDevs[i]); } diff --git a/ql/pricingengines/capfloor/blackcapfloorengine.cpp b/ql/pricingengines/capfloor/blackcapfloorengine.cpp index 30af7d4a15f..96683a56e88 100644 --- a/ql/pricingengines/capfloor/blackcapfloorengine.cpp +++ b/ql/pricingengines/capfloor/blackcapfloorengine.cpp @@ -132,7 +132,7 @@ namespace QuantLib { floorletVega = blackFormulaStdDevDerivative(strike, forward, stdDevs[i], discountedAccrual, displacement_) * sqrtTime; - floorletDelta = Option::Put * blackFormulaAssetItmProbability( + floorletDelta = Integer(Option::Put) * blackFormulaAssetItmProbability( Option::Put, strike, forward, stdDevs[i], displacement_); } diff --git a/ql/termstructures/volatility/swaption/swaptionvolcube1.hpp b/ql/termstructures/volatility/swaption/swaptionvolcube1.hpp index 7175aa5e6ac..267b8f32294 100644 --- a/ql/termstructures/volatility/swaption/swaptionvolcube1.hpp +++ b/ql/termstructures/volatility/swaption/swaptionvolcube1.hpp @@ -409,7 +409,7 @@ namespace QuantLib { maxErrors [j][k] = maxError; endCriteria[j][k] = sabrInterpolation->endCriteria(); - QL_ENSURE(endCriteria[j][k]!=EndCriteria::MaxIterations, + QL_ENSURE(endCriteria[j][k] != Integer(EndCriteria::MaxIterations), "global swaptions calibration failed: " "MaxIterations reached: " << "\n" << "option maturity = " << optionDates[j] << ", \n" << @@ -520,7 +520,7 @@ namespace QuantLib { calibrationResult[6]=sabrInterpolation->maxError(); calibrationResult[7]=sabrInterpolation->endCriteria(); - QL_ENSURE(calibrationResult[7]!=EndCriteria::MaxIterations, + QL_ENSURE(calibrationResult[7] != Integer(EndCriteria::MaxIterations), "section calibration failed: " "option tenor " << optionDates[j] << ", swap tenor " << swapTenors[k] << diff --git a/test-suite/amortizingbond.cpp b/test-suite/amortizingbond.cpp index 252e0cdfbda..26eb5af68c2 100644 --- a/test-suite/amortizingbond.cpp +++ b/test-suite/amortizingbond.cpp @@ -74,7 +74,7 @@ void AmortizingBondTest::testAmortizingFixedRateBond() { } // Check the coupon result - Real expectedCoupon = notionals[k] * rates[i] / freq; + Real expectedCoupon = notionals[k] * rates[i] / Integer(freq); error = std::fabs(coupon- expectedCoupon); if(error > tolerance) { diff --git a/test-suite/zerocouponswap.cpp b/test-suite/zerocouponswap.cpp index e53bc803e5f..83acd66346d 100644 --- a/test-suite/zerocouponswap.cpp +++ b/test-suite/zerocouponswap.cpp @@ -132,7 +132,7 @@ namespace zerocouponswap_test { auto subPeriodCpn = vars.createSubPeriodsCoupon(start, end); Real expectedFloatLegNPV = - paymentDate < vars.settlement ? 0.0 : type * discountAtPayment * subPeriodCpn->amount(); + paymentDate < vars.settlement ? 0.0 : Integer(type) * discountAtPayment * subPeriodCpn->amount(); Real expectedNPV = expectedFloatLegNPV + expectedFixedLegNPV;