Small bug in inflationPeriod() in termstructures/inflationtermstructure.cpp

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Small bug in inflationPeriod() in termstructures/inflationtermstructure.cpp

Niall O'Sullivan

Hi all,

I believe there is a small bug in the function inflationPeriod() in infaltiontermstructure.cpp.

The following example program fails in QL 1.0.1.

#include<ql/termstructures/inflationtermstructure.hpp>
using namespace QuantLib;
int main () {
Date d = Date(1,Dec,2009);
Frequency f = Quarterly;
std::pair<Date,Date> res = inflationPeriod (d,f);
return 0;
}

with the error message:
terminate called after throwing an instance of 'Quantlib::Error'
what (): month 14 outside January-December range [1,12]

The problem occurs when the month is December and the frequency is either Quarterly or Semiannual (for some other months you don't get an exception, but the function returns the incorrect period). We have the following in inflationtermstructure.cpp:

case Quarterly:
startMonth = Month(3*(month-1)/3 + 1);
endMonth = Month(startMonth + 2);

When month is 12 then startMonth is (incorrectly) set to
3*(12-1)/3+1 = 12
and endMonth is set to 14 which ultimately causes the above exception.

The bug is the order of evaluation which should be something like
3*((month-1)/3)+1
giving us startMonth = 3*((12-1)/3)+1 = 10 and thus endMonth = 12.

The Semiannual block of code has the same problem, to fix both I propose adding 2 pairs of brackets as below:

< startMonth = Month(6*(month-1)/6 + 1);
< startMonth = Month(3*(month-1)/3 + 1);
--
> startMonth = Month(6*((month-1)/6) + 1);
> startMonth = Month(3*((month-1)/3) + 1);

Regards,
Niall.
------------------------------------------------------------------------------
The Next 800 Companies to Lead America's Growth: New Video Whitepaper
David G. Thomson, author of the best-selling book "Blueprint to a
Billion" shares his insights and actions to help propel your
business during the next growth cycle. Listen Now!
http://p.sf.net/sfu/SAP-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev