From 058e9fe154ff19ffce42cef0b0e0150fd2c20c6f Mon Sep 17 00:00:00 2001 From: Luigi Ballabio Date: Tue, 26 Jan 2021 23:09:32 +0100 Subject: [PATCH] Use available C++11 std::lround function --- .clang-tidy | 2 +- .../analytic_cont_geom_av_price_heston.cpp | 2 +- .../credit/randomlosslatentmodel.hpp | 13 ++++++------- .../inflation/yoycapfloortermpricesurface.hpp | 5 +++-- ql/experimental/volatility/noarbsabr.cpp | 4 ++-- .../meshers/concentrating1dmesher.cpp | 7 +++---- .../swaption/basketgeneratingengine.cpp | 3 ++- .../vanilla/analyticgjrgarchengine.cpp | 3 ++- ql/time/daycounters/actual365fixed.cpp | 3 ++- ql/time/daycounters/actualactual.cpp | 7 ++++--- ql/timegrid.hpp | 3 ++- test-suite/americanoption.cpp | 11 ++++------- test-suite/asianoptions.cpp | 14 +++++++------- test-suite/barrieroption.cpp | 6 +++--- test-suite/basketoption.cpp | 8 ++++---- test-suite/binaryoption.cpp | 8 +++----- test-suite/blackdeltacalculator.cpp | 5 ----- test-suite/compoundoption.cpp | 4 ---- test-suite/daycounters.cpp | 4 ++-- test-suite/digitaloption.cpp | 17 ++++++++--------- test-suite/doublebarrieroption.cpp | 4 ++-- test-suite/doublebinaryoption.cpp | 2 +- test-suite/europeanoption.cpp | 5 ----- test-suite/fdheston.cpp | 2 +- test-suite/fdmlinearop.cpp | 2 +- test-suite/forwardoption.cpp | 8 ++++---- test-suite/hestonmodel.cpp | 2 +- test-suite/hybridhestonhullwhiteprocess.cpp | 5 +++-- test-suite/interestrates.cpp | 2 +- test-suite/jumpdiffusion.cpp | 4 ++-- test-suite/lookbackoptions.cpp | 18 +++++++++--------- test-suite/margrabeoption.cpp | 8 ++------ test-suite/quantooption.cpp | 16 ++++++++-------- test-suite/utilities.hpp | 6 ++++++ test-suite/variancegamma.cpp | 2 +- test-suite/varianceswaps.cpp | 6 +++--- 36 files changed, 104 insertions(+), 117 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index a8e165890ca..8091eea18e9 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,5 +1,5 @@ --- -Checks: '-*,readability-*,bugprone-*,misc-*,performance-*,modernize-redundant-void-arg,modernize-deprecated-headers,-readability-else-after-return,-readability-named-parameter,-readability-braces-around-statements,-readability-inconsistent-declaration-parameter-name,-readability-isolate-declaration,-readability-magic-numbers,-readability-convert-member-functions-to-static,-bugprone-macro-parentheses,-bugprone-narrowing-conversions,-bugprone-incorrect-roundings,-bugprone-branch-clone,-misc-non-private-member-variables-in-classes,-misc-unused-parameters' +Checks: '-*,readability-*,bugprone-*,misc-*,performance-*,modernize-redundant-void-arg,modernize-deprecated-headers,-readability-else-after-return,-readability-named-parameter,-readability-braces-around-statements,-readability-inconsistent-declaration-parameter-name,-readability-isolate-declaration,-readability-magic-numbers,-readability-convert-member-functions-to-static,-bugprone-macro-parentheses,-bugprone-narrowing-conversions,-bugprone-branch-clone,-misc-non-private-member-variables-in-classes,-misc-unused-parameters' WarningsAsErrors: '' HeaderFilterRegex: '.*' AnalyzeTemporaryDtors: false diff --git a/ql/experimental/asian/analytic_cont_geom_av_price_heston.cpp b/ql/experimental/asian/analytic_cont_geom_av_price_heston.cpp index a1bceb4e911..63a6fbd3323 100644 --- a/ql/experimental/asian/analytic_cont_geom_av_price_heston.cpp +++ b/ql/experimental/asian/analytic_cont_geom_av_price_heston.cpp @@ -135,7 +135,7 @@ namespace QuantLib { Real prefactor = -0.5*sigma_*sigma_*tau*tau / (n*(n-1)); // For each offset, look up the value in the map and only evaluate function if it's not there - for (Size offset=1; offset<5; offset++) { + for (int offset=1; offset<5; offset++) { int location = n-offset; std::map >::const_iterator position = fLookupTable_.find(location); if (position != fLookupTable_.end()) { diff --git a/ql/experimental/credit/randomlosslatentmodel.hpp b/ql/experimental/credit/randomlosslatentmodel.hpp index bbb4b0b3a96..b53aaa6f8a8 100644 --- a/ql/experimental/credit/randomlosslatentmodel.hpp +++ b/ql/experimental/credit/randomlosslatentmodel.hpp @@ -19,16 +19,15 @@ #ifndef quantlib_randomloss_latent_model_hpp #define quantlib_randomloss_latent_model_hpp -#include #include -#include -#include -#include - #include #include - +#include +#include +#include #include +#include +#include namespace QuantLib { @@ -39,7 +38,7 @@ namespace QuantLib { simEvent(unsigned int n, unsigned int d, Real r) : nameIdx(n), dayFromRef(d), // truncates the value: - compactRR(static_cast(r/rrGranular+.5)) {} + compactRR(std::lround(r/rrGranular)) {} unsigned int nameIdx : 12; // can index up to 4095 names unsigned int dayFromRef : 12; // can index up to 4095 days = 11 yrs private: diff --git a/ql/experimental/inflation/yoycapfloortermpricesurface.hpp b/ql/experimental/inflation/yoycapfloortermpricesurface.hpp index 55730573cde..bf7d8762874 100644 --- a/ql/experimental/inflation/yoycapfloortermpricesurface.hpp +++ b/ql/experimental/inflation/yoycapfloortermpricesurface.hpp @@ -29,6 +29,7 @@ #include #include #include +#include namespace QuantLib { @@ -373,7 +374,7 @@ namespace QuantLib { for (Size i = 0; i < cfMaturities_.size(); i++) { Time t = cfMaturityTimes_[i]; // determine the sum of discount factors - Size numYears = (Size)(t + 0.5); + Size numYears = (Size)std::lround(t); Real sumDiscount = 0.0; for (Size j=0; jdiscount(j + 1.0); @@ -509,7 +510,7 @@ namespace QuantLib { // which yoy-swap points to use in building the yoy-fwd curve? // for now pick every year - Size nYears = (Size)(0.5+timeFromReference(referenceDate()+cfMaturities_.back())); + Size nYears = (Size)std::lround(timeFromReference(referenceDate()+cfMaturities_.back())); std::vector > > YYhelpers; for (Size i=1; i<=nYears; i++) { diff --git a/ql/experimental/volatility/noarbsabr.cpp b/ql/experimental/volatility/noarbsabr.cpp index b665e608292..b97fef2944f 100644 --- a/ql/experimental/volatility/noarbsabr.cpp +++ b/ql/experimental/volatility/noarbsabr.cpp @@ -237,7 +237,7 @@ Real D0Interpolator::operator()() const { Real tauL = (expiryTimeTmp - tauG_[tauInd - 1]) / (tauG_[tauInd] - tauG_[tauInd - 1]); - int sigmaIInd = + Size sigmaIInd = sigmaIG_.size() - (std::upper_bound(sigmaIG_.rbegin(), sigmaIG_.rend(), sigmaI_) - sigmaIG_.rbegin()); @@ -246,7 +246,7 @@ Real D0Interpolator::operator()() const { Real sigmaIL = (sigmaI_ - sigmaIG_[sigmaIInd - 1]) / (sigmaIG_[sigmaIInd] - sigmaIG_[sigmaIInd - 1]); - int rhoInd = + Size rhoInd = rhoG_.size() - (std::upper_bound(rhoG_.rbegin(), rhoG_.rend(), rho_) - rhoG_.rbegin()); if (rhoInd == 0) { diff --git a/ql/methods/finitedifferences/meshers/concentrating1dmesher.cpp b/ql/methods/finitedifferences/meshers/concentrating1dmesher.cpp index 0367febe20c..b0590498a78 100644 --- a/ql/methods/finitedifferences/meshers/concentrating1dmesher.cpp +++ b/ql/methods/finitedifferences/meshers/concentrating1dmesher.cpp @@ -77,10 +77,9 @@ namespace QuantLib { if (!close(cPoint, start) && !close(cPoint, end)) { const Real z0 = -c1 / (c2 - c1); const Real u0 = - std::max( - std::min(static_cast(z0 * (size - 1) + 0.5), - static_cast(size) - 2), - 1) / + std::max(std::min(std::lround(z0 * (size - 1)), + static_cast(size) - 2), + 1L) / ((Real)(size - 1)); u.push_back(u0); z.push_back(z0); diff --git a/ql/pricingengines/swaption/basketgeneratingengine.cpp b/ql/pricingengines/swaption/basketgeneratingengine.cpp index 9d7e4947447..3e1e2bd2f97 100644 --- a/ql/pricingengines/swaption/basketgeneratingengine.cpp +++ b/ql/pricingengines/swaption/basketgeneratingengine.cpp @@ -24,6 +24,7 @@ #include #include #include +#include using std::exp; using std::fabs; @@ -76,7 +77,7 @@ namespace QuantLib { ext::shared_ptr sec = swaptionVolatility->smileSection( expiry, - static_cast(swapLength * 12.0 + 0.5) * Months, + static_cast(std::lround(swapLength * 12.0)) * Months, true); Real atmStrike = sec->atmLevel(); Real atmVol; diff --git a/ql/pricingengines/vanilla/analyticgjrgarchengine.cpp b/ql/pricingengines/vanilla/analyticgjrgarchengine.cpp index 0e6d61bcc26..4f7b6db321c 100644 --- a/ql/pricingengines/vanilla/analyticgjrgarchengine.cpp +++ b/ql/pricingengines/vanilla/analyticgjrgarchengine.cpp @@ -25,6 +25,7 @@ #include #include #include +#include using std::exp; using std::pow; @@ -58,7 +59,7 @@ namespace QuantLib { QL_REQUIRE(spotPrice > 0.0, "negative or null underlying given"); const Real strikePrice = payoff->strike(); const Real term = process->time(arguments_.exercise->lastDate()); - Size T = Size(process->daysPerYear()*term+0.5); + Size T = Size(std::lround(process->daysPerYear()*term)); Real r = -std::log(riskFreeDiscount/dividendDiscount)/(process->daysPerYear()*term); Real h1 = process->v0(); Real b0 = process->omega(); diff --git a/ql/time/daycounters/actual365fixed.cpp b/ql/time/daycounters/actual365fixed.cpp index 491e8458131..dd9a64c6793 100644 --- a/ql/time/daycounters/actual365fixed.cpp +++ b/ql/time/daycounters/actual365fixed.cpp @@ -19,6 +19,7 @@ */ #include +#include namespace QuantLib { @@ -49,7 +50,7 @@ namespace QuantLib { Time dcs = daysBetween(d1,d2); Time dcc = daysBetween(refPeriodStart,refPeriodEnd); - Integer months = Integer(0.5+12*dcc/365); + Integer months = Integer(std::lround(12*dcc/365)); QL_REQUIRE(months != 0, "invalid reference period for Act/365 Canadian; " "must be longer than a month"); diff --git a/ql/time/daycounters/actualactual.cpp b/ql/time/daycounters/actualactual.cpp index 330e0a7dc64..5f87c4d34b5 100644 --- a/ql/time/daycounters/actualactual.cpp +++ b/ql/time/daycounters/actualactual.cpp @@ -19,6 +19,7 @@ #include #include +#include namespace QuantLib { @@ -30,8 +31,8 @@ namespace QuantLib { Integer findCouponsPerYear(const T& impl, Date refStart, Date refEnd) { // This will only work for day counts longer than 15 days. - Integer months = Integer(0.5 + 12 * Real(impl.dayCount(refStart, refEnd))/365.0); - return (Integer)(0.5 + 12.0 / Real(months)); + Integer months = (Integer)std::lround(12 * Real(impl.dayCount(refStart, refEnd))/365.0); + return (Integer)std::lround(12.0 / Real(months)); } /* An ISMA day counter either needs a schedule or to have @@ -172,7 +173,7 @@ namespace QuantLib { // estimate roughly the length in months of a period Integer months = - Integer(0.5+12*Real(refPeriodEnd-refPeriodStart)/365); + (Integer)std::lround(12*Real(refPeriodEnd-refPeriodStart)/365); // for short periods... if (months == 0) { diff --git a/ql/timegrid.hpp b/ql/timegrid.hpp index 350cf80a406..e9d763127c1 100644 --- a/ql/timegrid.hpp +++ b/ql/timegrid.hpp @@ -30,6 +30,7 @@ #include #include #include +#include namespace QuantLib { @@ -121,7 +122,7 @@ namespace QuantLib { Time periodEnd = *t; if (periodEnd != 0.0) { // the nearest integer, at least 1 - Size nSteps = std::max(Size((periodEnd - periodBegin)/dtMax+0.5), Size(1)); + Size nSteps = std::max(Size(std::lround((periodEnd - periodBegin)/dtMax)), Size(1)); Time dt = (periodEnd - periodBegin)/nSteps; for (Size n=1; n<=nSteps; ++n) times_.push_back(periodBegin + n*dt); diff --git a/test-suite/americanoption.cpp b/test-suite/americanoption.cpp index aca6908cb1c..765bcb3d089 100644 --- a/test-suite/americanoption.cpp +++ b/test-suite/americanoption.cpp @@ -143,8 +143,7 @@ void AmericanOptionTest::testBaroneAdesiWhaleyValues() { ext::shared_ptr payoff(new PlainVanillaPayoff(values[i].type, values[i].strike)); - // FLOATING_POINT_EXCEPTION - Date exDate = today + Integer(values[i].t*360+0.5); + Date exDate = today + timeToDays(values[i].t); ext::shared_ptr exercise( new AmericanExercise(today, exDate)); @@ -217,8 +216,7 @@ void AmericanOptionTest::testBjerksundStenslandValues() { ext::shared_ptr payoff(new PlainVanillaPayoff(values[i].type, values[i].strike)); - //FLOATING_POINT_EXCEPTION - Date exDate = today + Integer(values[i].t*360+0.5); + Date exDate = today + timeToDays(values[i].t); ext::shared_ptr exercise( new AmericanExercise(today, exDate)); @@ -348,8 +346,7 @@ void AmericanOptionTest::testJuValues() { ext::shared_ptr payoff(new PlainVanillaPayoff(juValues[i].type, juValues[i].strike)); - //FLOATING_POINT_EXCEPTION - Date exDate = today + Integer(juValues[i].t*360+0.5); + Date exDate = today + timeToDays(juValues[i].t); ext::shared_ptr exercise( new AmericanExercise(today, exDate)); @@ -404,7 +401,7 @@ void AmericanOptionTest::testFdValues() { ext::shared_ptr payoff(new PlainVanillaPayoff(juValues[i].type, juValues[i].strike)); - Date exDate = today + Integer(juValues[i].t*360+0.5); + Date exDate = today + timeToDays(juValues[i].t); ext::shared_ptr exercise( new AmericanExercise(today, exDate)); diff --git a/test-suite/asianoptions.cpp b/test-suite/asianoptions.cpp index 966db267643..94d7a5843c4 100644 --- a/test-suite/asianoptions.cpp +++ b/test-suite/asianoptions.cpp @@ -368,7 +368,7 @@ void AsianOptionTest::testAnalyticDiscreteGeometricAveragePrice() { ext::shared_ptr exercise(new EuropeanExercise(exerciseDate)); std::vector fixingDates(futureFixings); - Integer dt = Integer(360.0/futureFixings+0.5); + Integer dt = (Integer)std::lround(360.0/futureFixings); fixingDates[0] = today + dt; for (Size j=1; j exercise(new EuropeanExercise(exerciseDate)); std::vector fixingDates(futureFixings); - Integer dt = Integer(360.0/futureFixings+0.5); + Integer dt = (Integer)std::lround(360.0/futureFixings); fixingDates[0] = today + dt; for (Size j=1; j exercise(new EuropeanExercise(exerciseDate)); std::vector fixingDates(futureFixings); - Integer dt = Integer(360.0/futureFixings+0.5); + Integer dt = (Integer)std::lround(360.0/futureFixings); fixingDates[0] = today + dt; for (Size j=1; j timeIncrements(cases4[l].fixings); std::vector fixingDates(cases4[l].fixings); timeIncrements[0] = cases4[l].first; - fixingDates[0] = today + Integer(timeIncrements[0]*360+0.5); + fixingDates[0] = today + timeToDays(timeIncrements[0]); for (Size i=1; i exercise(new EuropeanExercise(fixingDates[cases4[l].fixings-1])); @@ -1119,10 +1119,10 @@ void AsianOptionTest::testMCDiscreteArithmeticAverageStrike() { std::vector