1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/* This file is part of the KDE project
Copyright (C) 2007 Sascha Pfau <MrPeacock@gmail.com>
Copyright (C) 1998-2002 The KSpread Team <calligra-devel@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; only
version 2 of the License.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
// Local
#include "helper.h"
#include "SheetsDebug.h"
#include <QDate>
#include <math.h>
/* DISABLED - we use KCalendarSystem instead
void addMonths( QDate & date, int months )
{
int d = date.day();
int m = date.month() + months;
int y = date.year();
if ( m > 12 )
{
y += (int) ( m / 12 );
m %= 12;
}
// e.g. 31 Feb: decrease day...
while ( !QDate::isValid( y, m, d ) && d > 0 )
--d;
date.setYMD( y, m, d );
}
void subMonths( QDate & date, int months )
{
int d = date.day();
int m = date.month() - months;
int y = date.year();
while ( m < 1 )
{
m += 12;
y -= 1;
}
// e.g. 31 Feb: decrease day
while ( !QDate::isValid( y, m, d ) && d > 0 )
--d;
date.setYMD( y, m, d );
}
*/
int Calligra::Sheets::daysPerYear(QDate const & date, int basis)
{
switch (basis) {
case 0:
return 360;
case 1:
if (QDate::isLeapYear(date.year()))
return 366;
return 365;
case 2:
return 360;
case 3:
return 365;
case 4:
return 360;
}
return -1;
}
int Calligra::Sheets::daysBetweenDates(QDate const & date1, QDate const & date2, int basis)
{
int day1, day2, month1, month2, year1, year2;
bool isLeapYear = false;
int days, months, years;
day1 = date1.day();
month1 = date1.month();
year1 = date1.year();
day2 = date2.day();
month2 = date2.month();
year2 = date2.year();
years = year2 - year1;
months = month2 - month1 + years * 12;
days = day2 - day1;
isLeapYear = QDate::isLeapYear(year1);
switch (basis) {
case 0:
if (month1 == 2 && month2 != 2 && year1 == year2) {
if (isLeapYear)
return months * 30 + days - 1;
else
return months * 30 + days - 2;
}
return months * 30 + days;
case 1: // TODO: real days for difference between months!
// return ( month2 - month1 ) * 30 + years * 360 + days;
case 2: // TODO: real days for difference between months!
// return ( month2 - month1 ) * 30 + years * 365 + days;
case 3:
return date1.daysTo(date2);
case 4:
return months * 30 + days;
}
return -1;
}
// the days360 method does implement the 30/360days method as used in e.g. the YEARFRAC function
int Calligra::Sheets::days360(int day1, int month1, int year1, bool leapYear1,
int day2, int month2, int year2, bool leapYear2,
bool usaMethod)
{
if (usaMethod) { // US method
if (day1 == 31) {
day1 = 30;
if (day2 == 31) {
day2 = 30;
}
}
else if (day1 == 30 && day2 == 31) {
day2 = 30;
}
else if (month1 == 2 && (day1 == 29 || (day1 == 28 && ! leapYear1))) {
day1 = 30;
if (month2 == 2 && (day2 == 29 || (day2 == 28 && ! leapYear2))) {
day2 = 30;
}
}
} else { // European method
if (day1 == 31) {
day1 = 30;
}
if (day2 == 31) {
day2 = 30;
}
}
return day2 + month2 * 30 + year2 * 360 - day1 - month1 * 30 - year1 * 360;
}
// days360
int Calligra::Sheets::days360(const QDate& _date1, const QDate& _date2, bool european)
{
int day1, month1, year1, day2, month2, year2;
day1 = _date1.day();
month1 = _date1.month();
year1 = _date1.year();
day2 = _date2.day();
month2 = _date2.month();
year2 = _date2.year();
return days360(day1, month1, year1, QDate::isLeapYear(_date1.year()), day2, month2, year2, QDate::isLeapYear(_date2.year()), !european);
}
// // days360
// int Calligra::Sheets::days360( const QDate& _date1, const QDate& _date2, bool european )
// {
// int day1, day2;
// int month1, month2;
// int year1, year2;
// bool negative = false;
// QDate date1( _date1 );
// QDate date2( _date2 );
//
// if (date1.daysTo( date2 ) < 0)
// {
// QDate tmp( date1 );
// date1 = date2;
// date2 = tmp;
// negative = true;
// }
//
// day1 = date1.day();
// day2 = date2.day();
// month1 = date1.month();
// month2 = date2.month();
// year1 = date1.year();
// year2 = date2.year();
//
// if ( european )
// {
// if ( day1 == 31 )
// day1 = 30;
// if ( day2 == 31 )
// day2 = 30;
// }
// else
// {
// // thanks to the Gnumeric developers for this...
// if ( month1 == 2 && month2 == 2
// && date1.daysInMonth() == day1
// && date2.daysInMonth() == day2 )
// day2 = 30;
//
// if ( month1 == 2 && date1.daysInMonth() == day1 )
// day1 = 30;
//
// if ( day2 == 31 && day1 >= 30 )
// day2 = 30;
//
// if ( day1 == 31 )
// day1 = 30;
// }
//
// return ( ( year2 - year1 ) * 12 + ( month2 - month1 ) ) * 30
// + ( day2 - day1 );
// }
// yearFrac
long double Calligra::Sheets::yearFrac(const QDate& refDate, const QDate& startDate, const QDate& endDate, int basis)
{
Q_UNUSED(refDate);
QDate date1 = startDate;
QDate date2 = endDate;
//
// calculation
//
if (date2 < date1) {
// exchange dates
QDate Temp1 = date1;
date1 = date2;
date2 = Temp1;
}
int days = date1.daysTo(date2);
// debugSheetsFormula <<"date1 =" << date1 <<" date2 =" << date2 <<" days =" << days <<" basis =" << basis;
long double res = 0;
long double peryear = 0;
int nYears = 0;
switch (basis) {
case 1: {
nYears = date2.year() - date1.year() + 1;
for (int y = date1.year(); y <= date2.year(); ++y) {
peryear += QDate::isLeapYear(y) ? 366 : 365;
}
// less than one year - even if it does span two consequentive years ...
if (QDate(date1.year() + 1, date1.month(), date1.day()) >= date2) {
nYears = 1;
peryear = 365;
if (QDate::isLeapYear(date1.year()) && date1.month() <= 2) peryear = 366;
else if (QDate::isLeapYear(date2.year()) && date2.month() > 2) peryear = 366;
else if (date2.month() == 2 && date2.day() == 29) peryear = 366;
}
peryear = peryear / (long double) nYears;
nYears = 0;
break;
}
case 2: {
// Actual/360
peryear = 360;
break;
}
case 3: {
// Actual/365
peryear = 365;
break;
}
case 4: {
// 30/360 Europe
// calc datedif360 (start, end, Europe)
days = days360(date1, date2, 1);
peryear = 360;
break;
}
default: {
// NASD 30/360
//basis = 0;
// calc datedif360 (start, end, US)
days = days360(date1, date2, 0);
peryear = 360;
}
}
res = (long double)(nYears) + (long double)days / (long double) peryear;
// debugSheetsFormula<<"getYearFrac res="<<res;
return res;
}
// pow1p calculate (1+x)^y accurately
long double Calligra::Sheets::pow1p(const long double& x, const long double& y)
{
if (fabs(x) > 0.5)
return pow(1 + x, y);
else
return exp(y * log1p(x));
}
// pow1pm1 calculate ((1+x)^y)-1 accurately
long double Calligra::Sheets::pow1pm1(const long double& x, const long double& y)
{
if (x <= -1)
return pow(1 + x, y) - 1;
else
return expm1(y * log1p(x));
}
long double Calligra::Sheets::duration(const QDate& refDate, const QDate& settlement, const QDate& maturity,
const long double& coup_, const long double& yield_, const int& freq, const int& basis, const long double& numOfCoups)
{
long double yield = yield_;
long double coup = coup_;
// debugSheetsFormula<<"DURATION_HELPER";
// debugSheetsFormula<<"sett ="<<settlement<<" mat ="<<maturity<<" coup ="<<coup<<" yield ="<<yield<<" freq ="<<freq<<" basis ="<<basis;
long double yearfrac = yearFrac(refDate, settlement, maturity, basis);
long double res = 0.0l;
const long double f100 = 100.0l;
coup *= f100 / (long double)(freq);
yield /= freq;
yield += 1.0;
long double diff = yearfrac * freq - numOfCoups;
long double t;
for (t = 1.0l ; t < numOfCoups ; t += 1.0l)
res += (t + diff) * (coup) / pow(yield, t + diff);
res += (numOfCoups + diff) * (coup + f100) / pow(yield, numOfCoups + diff);
long double p = 0.0l;
for (t = 1.0l ; t < numOfCoups ; t += 1.0l)
p += coup / pow(yield, t + diff);
p += (coup + f100) / pow(yield, numOfCoups + diff);
res /= p;
res /= (long double)(freq);
return(res);
}
|