• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lballabio / QuantLib / 26458355582

26 May 2026 03:35PM UTC coverage: 74.706% (+0.01%) from 74.692%
26458355582

Pull #2605

github

web-flow
Merge a6e957050 into 8a825ea16
Pull Request #2605: Fix out-of-bounds read in mixed interpolation switch point

1 of 1 new or added line in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

59045 of 79036 relevant lines covered (74.71%)

8587555.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.96
/ql/math/integrals/discreteintegrals.cpp
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2

3
/*
4
 Copyright (C) 2014 Klaus Spanderen
5

6
 This file is part of QuantLib, a free-software/open-source library
7
 for financial quantitative analysts and developers - http://quantlib.org/
8

9
 QuantLib is free software: you can redistribute it and/or modify it
10
 under the terms of the QuantLib license.  You should have received a
11
 copy of the license along with this program; if not, please email
12
 <quantlib-dev@lists.sf.net>. The license is also available online at
13
 <https://www.quantlib.org/license.shtml>.
14

15
 This program is distributed in the hope that it will be useful, but WITHOUT
16
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
 FOR A PARTICULAR PURPOSE.  See the license for more details.
18
*/
19

20
#include <ql/math/integrals/discreteintegrals.hpp>
21

22
namespace QuantLib {
23

24
    Real DiscreteTrapezoidIntegral::operator()(
64✔
25
        const Array& x, const Array& f)    const {
26

27
        const Size n = f.size();
28
        QL_REQUIRE(n == x.size(), "inconsistent size");
64✔
29

30
        if (n < 2)
64✔
31
            return 0.0;
32

33
        Real sum = 0.0;
34

35
        for (Size i=0; i < n-1; ++i) {
1,221✔
36
            sum += (x[i+1]-x[i])*(f[i]+f[i+1]);
1,159✔
37
        }
38

39
        return 0.5*sum;
62✔
40
    }
41

42
    Real DiscreteSimpsonIntegral::operator()(
137,639✔
43
        const Array& x, const Array& f)    const {
44

45
        const Size n = f.size();
46
        QL_REQUIRE(n == x.size(), "inconsistent size");
137,639✔
47

48
        if (n < 2)
137,639✔
49
            return 0.0;
50

51
        Real sum = 0.0;
52

53
        for (Size j=0; j < n-2; j+=2) {
11,377,997✔
54
            const Real dxj   = x[j+1]-x[j];
11,240,360✔
55
            const Real dxjp1 = x[j+2]-x[j+1];
11,240,360✔
56

57
            const Real alpha = dxjp1*(2*dxj-dxjp1);
11,240,360✔
58
            const Real dd = dxj+dxjp1;
11,240,360✔
59
            const Real k = dd/(6*dxjp1*dxj);
11,240,360✔
60
            const Real beta = dd*dd;
11,240,360✔
61
            const Real gamma = dxj*(2*dxjp1-dxj);
11,240,360✔
62

63
            sum += k*(alpha*f[j]+beta*f[j+1]+gamma*f[j+2]);
11,240,360✔
64
        }
65
        if ((n & 1) == 0U) {
137,637✔
66
            sum += 0.5*(x[n-1]-x[n-2])*(f[n-1]+f[n-2]);
20✔
67
        }
68

69
        return sum;
70
    }
71

72
    Real DiscreteTrapezoidIntegrator::integrate(
58,259✔
73
        const std::function<Real (Real)>& f, Real a, Real b) const {
74
            const Size n=maxEvaluations()-1;
58,259✔
75
            const Real d=(b-a)/n;
58,259✔
76
            
77
            Real sum = f(a)*0.5;
58,259✔
78
            
79
            for(Size i=0;i<n-1;++i) {
7,191,427✔
80
                a += d;
7,133,168✔
81
                sum += f(a);
7,133,168✔
82
            }
83
            sum += f(b)*0.5;
58,259✔
84
            
85
            increaseNumberOfEvaluations(maxEvaluations());
58,259✔
86
            
87
            return d * sum;
58,259✔
88
    }
89

90
    Real DiscreteSimpsonIntegrator::integrate(
75✔
91
        const std::function<Real (Real)>& f, Real a, Real b) const {
92
            const Size n=maxEvaluations()-1;
75✔
93
            const Real d=(b-a)/n, d2=d*2;
75✔
94
            
95
            Real sum = 0.0, x = a + d;
75✔
96
            for(Size i=1;i<n;i+=2) {//to time 4
9,936✔
97
                sum += f(x);
9,861✔
98
                x += d2;
9,861✔
99
            }
100
            sum *= 2;
75✔
101

102
            x = a+d2;
75✔
103
            for(Size i=2;i<n-1;i+=2) {//to time 2
9,861✔
104
                sum += f(x);
9,786✔
105
                x += d2;
9,786✔
106
            }
107
            sum *= 2;
75✔
108

109
            sum += f(a);
75✔
110
            if((n&1) != 0U)
75✔
111
                sum += 1.5*f(b)+2.5*f(b-d);
75✔
112
            else
UNCOV
113
                sum += f(b);
×
114
            
115
            increaseNumberOfEvaluations(maxEvaluations());
75✔
116
            
117
            return d/3 * sum;
75✔
118
    }
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc