thanks for catching this. Fix+test now in SVN on R1000 branch, and trunk.
Chris
------------------------------
Message: 5
Date: Wed, 10 Nov 2010 15:59:12 +0000
From: Niall O'Sullivan <
[hidden email]>
Subject: [Quantlib-dev] Small bug in inflationPeriod() in
termstructures/inflationtermstructure.cpp
To:
[hidden email]Message-ID:
<
[hidden email]>
Content-Type: text/plain;
charset="us-ascii"
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.