Skip to content

Commit

Permalink
Use available C++11 std::lround function
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Jan 27, 2021
1 parent e781ae5 commit 058e9fe
Show file tree
Hide file tree
Showing 36 changed files with 104 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .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
Expand Down
Expand Up @@ -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<int, std::complex<Real> >::const_iterator position = fLookupTable_.find(location);
if (position != fLookupTable_.end()) {
Expand Down
13 changes: 6 additions & 7 deletions ql/experimental/credit/randomlosslatentmodel.hpp
Expand Up @@ -19,16 +19,15 @@
#ifndef quantlib_randomloss_latent_model_hpp
#define quantlib_randomloss_latent_model_hpp

#include <ql/math/solvers1d/brent.hpp>
#include <ql/experimental/credit/basket.hpp>
#include <ql/experimental/math/latentmodel.hpp>
#include <ql/experimental/math/gaussiancopulapolicy.hpp>
#include <ql/experimental/math/tcopulapolicy.hpp>

#include <ql/experimental/credit/randomdefaultlatentmodel.hpp>
#include <ql/experimental/credit/spotlosslatentmodel.hpp>

#include <ql/experimental/math/gaussiancopulapolicy.hpp>
#include <ql/experimental/math/latentmodel.hpp>
#include <ql/experimental/math/tcopulapolicy.hpp>
#include <ql/math/randomnumbers/mt19937uniformrng.hpp>
#include <ql/math/solvers1d/brent.hpp>
#include <cmath>

namespace QuantLib {

Expand All @@ -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<unsigned int>(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:
Expand Down
5 changes: 3 additions & 2 deletions ql/experimental/inflation/yoycapfloortermpricesurface.hpp
Expand Up @@ -29,6 +29,7 @@
#include <ql/termstructures/inflation/piecewiseyoyinflationcurve.hpp>
#include <ql/termstructures/inflation/inflationhelpers.hpp>
#include <ql/experimental/inflation/polynomial2Dspline.hpp>
#include <cmath>

namespace QuantLib {

Expand Down Expand Up @@ -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; j<numYears; ++j)
sumDiscount += nominalTS_->discount(j + 1.0);
Expand Down Expand Up @@ -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<ext::shared_ptr<BootstrapHelper<YoYInflationTermStructure> > > YYhelpers;
for (Size i=1; i<=nYears; i++) {
Expand Down
4 changes: 2 additions & 2 deletions ql/experimental/volatility/noarbsabr.cpp
Expand Up @@ -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());
Expand All @@ -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) {
Expand Down
Expand Up @@ -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<int>(z0 * (size - 1) + 0.5),
static_cast<int>(size) - 2),
1) /
std::max(std::min(std::lround(z0 * (size - 1)),
static_cast<long>(size) - 2),
1L) /
((Real)(size - 1));
u.push_back(u0);
z.push_back(z0);
Expand Down
3 changes: 2 additions & 1 deletion ql/pricingengines/swaption/basketgeneratingengine.cpp
Expand Up @@ -24,6 +24,7 @@
#include <ql/models/shortrate/calibrationhelpers/swaptionhelper.hpp>
#include <ql/termstructures/volatility/swaption/swaptionvolcube.hpp>
#include <ql/quotes/simplequote.hpp>
#include <cmath>

using std::exp;
using std::fabs;
Expand Down Expand Up @@ -76,7 +77,7 @@ namespace QuantLib {
ext::shared_ptr<SmileSection> sec =
swaptionVolatility->smileSection(
expiry,
static_cast<Size>(swapLength * 12.0 + 0.5) * Months,
static_cast<Size>(std::lround(swapLength * 12.0)) * Months,
true);
Real atmStrike = sec->atmLevel();
Real atmVol;
Expand Down
3 changes: 2 additions & 1 deletion ql/pricingengines/vanilla/analyticgjrgarchengine.cpp
Expand Up @@ -25,6 +25,7 @@
#include <ql/pricingengines/vanilla/analyticgjrgarchengine.hpp>
#include <ql/math/distributions/normaldistribution.hpp>
#include <ql/instruments/payoffs.hpp>
#include <cmath>

using std::exp;
using std::pow;
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion ql/time/daycounters/actual365fixed.cpp
Expand Up @@ -19,6 +19,7 @@
*/

#include <ql/time/daycounters/actual365fixed.hpp>
#include <cmath>

namespace QuantLib {

Expand Down Expand Up @@ -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");
Expand Down
7 changes: 4 additions & 3 deletions ql/time/daycounters/actualactual.cpp
Expand Up @@ -19,6 +19,7 @@

#include <ql/time/daycounters/actualactual.hpp>
#include <algorithm>
#include <cmath>

namespace QuantLib {

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion ql/timegrid.hpp
Expand Up @@ -30,6 +30,7 @@
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>

namespace QuantLib {

Expand Down Expand Up @@ -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);
Expand Down
11 changes: 4 additions & 7 deletions test-suite/americanoption.cpp
Expand Up @@ -143,8 +143,7 @@ void AmericanOptionTest::testBaroneAdesiWhaleyValues() {

ext::shared_ptr<StrikedTypePayoff> 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> exercise(
new AmericanExercise(today, exDate));

Expand Down Expand Up @@ -217,8 +216,7 @@ void AmericanOptionTest::testBjerksundStenslandValues() {

ext::shared_ptr<StrikedTypePayoff> 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> exercise(
new AmericanExercise(today, exDate));

Expand Down Expand Up @@ -348,8 +346,7 @@ void AmericanOptionTest::testJuValues() {

ext::shared_ptr<StrikedTypePayoff> 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> exercise(
new AmericanExercise(today, exDate));

Expand Down Expand Up @@ -404,7 +401,7 @@ void AmericanOptionTest::testFdValues() {
ext::shared_ptr<StrikedTypePayoff> 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> exercise(
new AmericanExercise(today, exDate));

Expand Down
14 changes: 7 additions & 7 deletions test-suite/asianoptions.cpp
Expand Up @@ -368,7 +368,7 @@ void AsianOptionTest::testAnalyticDiscreteGeometricAveragePrice() {
ext::shared_ptr<Exercise> exercise(new EuropeanExercise(exerciseDate));

std::vector<Date> 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<futureFixings; j++)
fixingDates[j] = fixingDates[j-1] + dt;
Expand Down Expand Up @@ -427,7 +427,7 @@ void AsianOptionTest::testAnalyticDiscreteGeometricAverageStrike() {
ext::shared_ptr<Exercise> exercise(new EuropeanExercise(exerciseDate));

std::vector<Date> 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<futureFixings; j++)
fixingDates[j] = fixingDates[j-1] + dt;
Expand Down Expand Up @@ -493,7 +493,7 @@ void AsianOptionTest::testMCDiscreteGeometricAveragePrice() {
ext::shared_ptr<Exercise> exercise(new EuropeanExercise(exerciseDate));

std::vector<Date> 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<futureFixings; j++)
fixingDates[j] = fixingDates[j-1] + dt;
Expand Down Expand Up @@ -773,10 +773,10 @@ void AsianOptionTest::testMCDiscreteArithmeticAveragePrice() {
std::vector<Time> timeIncrements(cases4[l].fixings);
std::vector<Date> 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<cases4[l].fixings; i++) {
timeIncrements[i] = i*dt + cases4[l].first;
fixingDates[i] = today + Integer(timeIncrements[i]*360+0.5);
fixingDates[i] = today + timeToDays(timeIncrements[i]);
}
ext::shared_ptr<Exercise> exercise(new
EuropeanExercise(fixingDates[cases4[l].fixings-1]));
Expand Down Expand Up @@ -1119,10 +1119,10 @@ void AsianOptionTest::testMCDiscreteArithmeticAverageStrike() {
std::vector<Time> timeIncrements(cases5[l].fixings);
std::vector<Date> fixingDates(cases5[l].fixings);
timeIncrements[0] = cases5[l].first;
fixingDates[0] = today + Integer(timeIncrements[0]*360+0.5);
fixingDates[0] = today + timeToDays(timeIncrements[0]);
for (Size i=1; i<cases5[l].fixings; i++) {
timeIncrements[i] = i*dt + cases5[l].first;
fixingDates[i] = today + Integer(timeIncrements[i]*360+0.5);
fixingDates[i] = today + timeToDays(timeIncrements[i]);
}
ext::shared_ptr<Exercise> exercise(new
EuropeanExercise(fixingDates[cases5[l].fixings-1]));
Expand Down
6 changes: 3 additions & 3 deletions test-suite/barrieroption.cpp
Expand Up @@ -391,7 +391,7 @@ void BarrierOptionTest::testHaugValues() {
ext::shared_ptr<BlackVolTermStructure> volTS = flatVol(today, vol, dc);

for (Size i=0; i<LENGTH(values); i++) {
Date exDate = today + Integer(values[i].t*360+0.5);
Date exDate = today + timeToDays(values[i].t);

spot ->setValue(values[i].s);
qRate->setValue(values[i].q);
Expand Down Expand Up @@ -1113,9 +1113,9 @@ void BarrierOptionTest::testVannaVolgaSimpleBarrierValues() {

ext::shared_ptr<StrikedTypePayoff> payoff =
ext::make_shared<PlainVanillaPayoff>(values[i].type,
values[i].strike);
values[i].strike);

Date exDate = today + Integer(values[i].t*365+0.5);
Date exDate = today + timeToDays(values[i].t, 365);
ext::shared_ptr<Exercise> exercise =
ext::make_shared<EuropeanExercise>(exDate);

Expand Down
8 changes: 4 additions & 4 deletions test-suite/basketoption.cpp
Expand Up @@ -284,7 +284,7 @@ void BasketOptionTest::testEuroTwoValues() {
ext::shared_ptr<PlainVanillaPayoff> payoff(new
PlainVanillaPayoff(values[i].type, values[i].strike));

Date exDate = today + Integer(values[i].t*360+0.5);
Date exDate = today + timeToDays(values[i].t);
ext::shared_ptr<Exercise> exercise(new EuropeanExercise(exDate));

spot1 ->setValue(values[i].s1);
Expand Down Expand Up @@ -663,7 +663,7 @@ void BasketOptionTest::testTavellaValues() {
ext::shared_ptr<PlainVanillaPayoff> payoff(new
PlainVanillaPayoff(values[0].type, values[0].strike));

Date exDate = today + Integer(values[0].t*360+0.5);
Date exDate = today + timeToDays(values[0].t);
ext::shared_ptr<Exercise> exercise(new AmericanExercise(today, exDate));

spot1 ->setValue(values[0].s1);
Expand Down Expand Up @@ -828,7 +828,7 @@ void BasketOptionTest::testOneDAmericanValues(std::size_t from, std::size_t to)
ext::shared_ptr<PlainVanillaPayoff> payoff(new
PlainVanillaPayoff(oneDataValues[i].type, oneDataValues[i].strike));

Date exDate = today + Integer(oneDataValues[i].t*360+0.5);
Date exDate = today + timeToDays(oneDataValues[i].t);
ext::shared_ptr<Exercise> exercise(new AmericanExercise(today,
exDate));

Expand Down Expand Up @@ -916,7 +916,7 @@ void BasketOptionTest::testOddSamples() {
ext::shared_ptr<PlainVanillaPayoff> payoff(new
PlainVanillaPayoff(values[i].type, values[i].strike));

Date exDate = today + Integer(values[i].t*360+0.5);
Date exDate = today + timeToDays(values[i].t);
ext::shared_ptr<Exercise> exercise(new AmericanExercise(today,
exDate));

Expand Down
8 changes: 3 additions & 5 deletions test-suite/binaryoption.cpp
Expand Up @@ -156,7 +156,7 @@ void BinaryOptionTest::testCashOrNothingHaugValues() {
ext::shared_ptr<StrikedTypePayoff> payoff(new CashOrNothingPayoff(
values[i].type, values[i].strike, values[i].cash));

Date exDate = today + Integer(values[i].t*360+0.5);
Date exDate = today + timeToDays(values[i].t);
ext::shared_ptr<Exercise> amExercise(new AmericanExercise(today,
exDate,
true));
Expand Down Expand Up @@ -244,10 +244,8 @@ void BinaryOptionTest::testAssetOrNothingHaugValues() {
ext::shared_ptr<StrikedTypePayoff> payoff(new AssetOrNothingPayoff(
values[i].type, values[i].strike));

Date exDate = today + Integer(values[i].t*360+0.5);
ext::shared_ptr<Exercise> amExercise(new AmericanExercise(today,
exDate,
true));
Date exDate = today + timeToDays(values[i].t);
ext::shared_ptr<Exercise> amExercise(new AmericanExercise(today, exDate, true));

spot ->setValue(values[i].s);
qRate->setValue(values[i].q);
Expand Down
5 changes: 0 additions & 5 deletions test-suite/blackdeltacalculator.cpp
Expand Up @@ -36,11 +36,6 @@ using std::sqrt;

namespace black_delta_calculator_test {

Integer timeToDays(Time t) {
// FLOATING_POINT_EXCEPTION
return Integer(t*360+0.5);
}

struct DeltaData {
Option::Type ot;
DeltaVolQuote::DeltaType dt;
Expand Down
4 changes: 0 additions & 4 deletions test-suite/compoundoption.cpp
Expand Up @@ -57,10 +57,6 @@ using namespace boost::unit_test_framework;

namespace compound_option_test {

Integer timeToDays(Time t) {
return Integer(t*360+0.5);
}

struct CompoundOptionData {
Option::Type typeMother;
Option::Type typeDaughter;
Expand Down

0 comments on commit 058e9fe

Please sign in to comment.