LibreOffice Module sc (master)  1
opinlinefun_finacial.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 
10 #ifndef SC_OPENCL_OPINLINFUN_finacial
11 #define SC_OPENCL_OPINLINFUN_finacial
12 
13 std::string nCorrValDecl ="double constant nCorrVal[]"
14 "= {0, 9e-1, 9e-2, 9e-3, 9e-4, 9e-5, 9e-6, 9e-7, "
15 "9e-8,9e-9, 9e-10, 9e-11, 9e-12, 9e-13, 9e-14, 9e-15};\n";
16 
17 std::string SCdEpsilonDecl =
18 "constant double SCdEpsilon = 1.0E-7;\n";
19 
20 std::string RoundDecl = "double Round(double fValue);\n";
21 
22 std::string Round =
23 "double Round(double fValue)\n"
24 "{\n"
25 " if ( fValue == 0.0 )\n"
26 " return fValue;\n"
27 "\n"
28 " double fFac = 0;\n"
29 " int nExp;\n"
30 " if ( fValue > 0.0 )\n"
31 " nExp = ( floor( log10( fValue ) ) );\n"
32 " else\n"
33 " nExp = 0;\n"
34 " int nIndex = 15 - nExp;\n"
35 " if ( nIndex > 15 )\n"
36 " nIndex = 15;\n"
37 " else if ( nIndex <= 1 )\n"
38 " nIndex = 0;\n"
39 " fValue = floor( fValue + 0.5 + nCorrVal[nIndex] );\n"
40 " return fValue;\n"
41 "}\n";
42 
43 std::string GetPMTDecl =
44 "double GetPMT( double fRate, double fNper, double fPv, double fFv, int nPayType );\n";
45 
46 std::string GetPMT=
47 "double GetPMT( double fRate, double fNper, double fPv, double fFv, int nPayType )\n"
48 "{\n"
49 " double fPmt;\n"
50 " if( fRate == 0.0 )\n"
51 " fPmt = ( fPv + fFv ) / fNper;\n"
52 " else\n"
53 " {\n"
54 " double fTerm = pow( 1.0 + fRate, fNper );\n"
55 " if( nPayType > 0 )\n"
56 " fPmt = ( fFv * fRate / ( fTerm - 1.0 ) + fPv * fRate / ( 1.0 - 1."
57 "0 / fTerm ) ) / ( 1.0 + fRate );\n"
58 " else\n"
59 " fPmt = fFv * fRate / ( fTerm - 1.0 ) + fPv * fRate /( 1.0 - 1.0 "
60 "/ fTerm );\n"
61 " }\n"
62 " return -fPmt;\n"
63 "}\n";
64 
65 std::string GetPMT_newDecl =
66 "double GetPMT_new( double fRate, double fNper, double fPv, double fFv,"
67 "int nPayType );\n";
68 std::string GetPMT_new=
69 "double GetPMT_new( double fRate, double fNper, double fPv, double fFv,"
70 "int nPayType)\n"
71 "{\n"
72 " double fPmt;\n"
73 " double fTerm = pow( 1.0 + fRate, fNper );\n"
74 " if( nPayType > 0 )\n"
75 " fPmt = ( fFv * fRate *pow ( fTerm - 1.0,-1 ) + fPv * fRate *pow( "
76 "( 1.0 - pow( fTerm,-1) ),-1) )* pow ( 1.0 + fRate,-1 );\n"
77 " else\n"
78 " fPmt = fFv * fRate *pow ( fTerm - 1.0 ,-1) + fPv * fRate *pow( "
79 "1.0 - pow( fTerm,-1),-1 );\n"
80 " return -fPmt;\n"
81 "}\n";
82 std::string GetFVDecl =
83 "double GetFV( double fRate, double fNper, double fPmt,"
84 "double fPv, int nPayType );\n";
85 
86 std::string GetFV =
87 "double GetFV( double fRate, double fNper, double fPmt,"
88 "double fPv, int nPayType )\n"
89 "{\n"
90 " double fFv;\n"
91 " if( fRate == 0.0 )\n"
92 " fFv = fPv + fPmt * fNper;\n"
93 " else\n"
94 " {\n"
95 " double fTerm = pow( 1.0 + fRate, fNper );\n"
96 " if( nPayType > 0 )\n"
97 " fFv = fPv * fTerm + fPmt * ( 1.0 + fRate ) *( fTerm - 1.0 ) "
98 "/ fRate;\n"
99 " else\n"
100 " fFv = fPv * fTerm + fPmt * ( fTerm - 1.0 ) / fRate;\n"
101 " }\n"
102 " return -fFv;\n"
103 "}\n";
104 
105 std::string GetFV_newDecl =
106 "double GetFV_new( double fRate, double fNper, double fPmt,"
107 "double fPv, int nPayType );\n";
108 
109 std::string GetFV_new =
110 "double GetFV_new( double fRate, double fNper, double fPmt,"
111 "double fPv, int nPayType )\n"
112 "{\n"
113 " double fFv;\n"
114 " double fTerm = pow( 1.0 + fRate, fNper );\n"
115 " if( nPayType > 0 )\n"
116 " fFv = fPv * fTerm + fPmt * ( 1.0 + fRate ) *( fTerm - 1.0 ) "
117 "*pow( fRate,-1);\n"
118 " else\n"
119 " fFv = fPv * fTerm + fPmt * ( fTerm - 1.0 ) *pow( fRate,-1);\n"
120 " return -fFv;\n"
121 "}\n";
122 
123 std::string IsLeapYearDecl =
124 "bool IsLeapYear( int n );\n";
125 
126 std::string IsLeapYear =
127 "bool IsLeapYear( int n )\n"
128 "{\n"
129 " return ( (( ( n % 4 ) == 0 ) && ( ( n % 100 ) != 0)) || ( ( n % 400 ) == "
130 "0 ) );\n"
131 "}\n";
132 
133 std::string DaysInMonthDecl=
134 "int DaysInMonth( int nMonth, int nYear );\n";
135 
136 std::string DaysInMonth =
137 "int DaysInMonth( int nMonth, int nYear )\n"
138 "{\n"
139 " int aDaysInMonth[12] = { 31, 28, 31, 30, 31, 30,\n"
140 " 31, 31, 30, 31, 30, 31 };\n"
141 "\n"
142 " if ( nMonth != 2 )\n"
143 " return aDaysInMonth[nMonth-1];\n"
144 " else\n"
145 " {\n"
146 " if ( IsLeapYear(nYear) )\n"
147 " return aDaysInMonth[nMonth-1] + 1;\n"
148 " else\n"
149 " return aDaysInMonth[nMonth-1];\n"
150 " }\n"
151 "}\n";
153 "int DaysInMonth( int nMonth, int nYear );\n";
154 
155 std::string DaysInMonth_new =
156 "int DaysInMonth( int nMonth, int nYear )\n"
157 "{\n"
158 " int tmp = 0;\n"
159 " switch(nMonth)\n"
160 " {\n"
161 " case 1:\n"
162 " case 3:\n"
163 " case 5:\n"
164 " case 7:\n"
165 " case 8:\n"
166 " case 10:\n"
167 " case 12:\n"
168 " tmp = 31;\n"
169 " break;\n"
170 " case 4:\n"
171 " case 6:\n"
172 " case 9:\n"
173 " case 11:\n"
174 " tmp =30;\n"
175 " break;\n"
176 " case 2:\n"
177 " if ( IsLeapYear(nYear)==1)\n"
178 " tmp = 29;\n"
179 " else\n"
180 " tmp = 28;\n"
181 " break;\n"
182 " }\n"
183 " return tmp;\n"
184 "}\n";
185 
186 std::string DaysToDateDecl =
187 "void DaysToDate( int nDays, int *rDay, int* rMonth, int* rYear );\n";
188 
189 std::string DaysToDate =
190 "void DaysToDate( int nDays, int *rDay, int* rMonth, int* rYear )\n"
191 "{\n"
192 "\n"
193 " int nTempDays;\n"
194 " int i = 0;\n"
195 " bool bCalc;\n"
196 
197 " do\n"
198 " {\n"
199 " nTempDays = nDays;\n"
200 " *rYear = (int)((nTempDays / 365) - i);\n"
201 " nTempDays -= ((int) *rYear -1) * 365;\n"
202 " nTempDays -= (( *rYear -1) / 4) - (( *rYear -1) / 100) +"
203 "((*rYear -1) / 400);\n"
204 " bCalc = false;\n"
205 " if ( nTempDays < 1 )\n"
206 " {\n"
207 " i++;\n"
208 " bCalc = true;\n"
209 " }\n"
210 " else\n"
211 " {\n"
212 " if ( nTempDays > 365 )\n"
213 " {\n"
214 " if ( (nTempDays != 366) || !IsLeapYear( *rYear ) )\n"
215 " {\n"
216 " i--;\n"
217 " bCalc = true;\n"
218 " }\n"
219 " }\n"
220 " }\n"
221 " }\n"
222 " while ( bCalc );\n"
223 " if(nTempDays!=0){\n"
224 " for (*rMonth = 1; (int)nTempDays > DaysInMonth( *rMonth, *rYear );"
225 "*rMonth+=1)\n"
226 " {\n"
227 " nTempDays -= DaysInMonth( *rMonth, *rYear );\n"
228 " }\n"
229 " *rDay = (int)nTempDays;\n"
230 " }\n"
231 "}\n";
232 
233 std::string DateToDaysDecl=
234 "int DateToDays( int nDay, int nMonth, int nYear );\n";
235 
236 std::string DateToDays=
237 "int DateToDays( int nDay, int nMonth, int nYear )\n"
238 "{\n"
239 " int nDays = ((int)nYear-1) * 365;\n"
240 " nDays += ((nYear-1) / 4) - ((nYear-1) / 100) + ((nYear-1) / 400);\n"
241 " for( int i = 1; i < nMonth; i++ )\n"
242 " nDays += DaysInMonth(i,nYear);\n"
243 " nDays += nDay;\n"
244 "\n"
245 " return nDays;\n"
246 "}\n";
247 
248 std::string DateToDays_newDecl=
249 "int DateToDays_new( int nDay, int nMonth, int nYear );\n";
250 
251 std::string DateToDays_new=
252 "int DateToDays_new( int nDay, int nMonth, int nYear )\n"
253 "{\n"
254 " int nDays = (nYear-1) * 365;\n"
255 " nDays += (int)((nYear-1) *pow(4.0,-1.0)- (nYear-1) *pow( 100.0,-1.0)"
256 "+ (nYear-1) *pow(400.0,-1.0));\n"
257 " for( int i = 1; i < nMonth; i++ )\n"
258 " nDays += DaysInMonth(i,nYear);\n"
259 " nDays += nDay;\n"
260 "\n"
261 " return nDays;\n"
262 "}\n";
263 
264 std::string GetNullDateDecl=
265 "int GetNullDate();\n";
266 
267 std::string GetNullDate=
268 "int GetNullDate()\n"
269 "{\n"
270 " return DateToDays(30,12,1899 );\n"
271 "}\n";
273 "int GetNullDate_new();\n";
274 
275 std::string GetNullDate_new=
276 "int GetNullDate_new()\n"
277 "{\n"
278 " return DateToDays_new(30,12,1899 );\n"
279 "}\n";
280 
281 std::string ScaDateDecl=
282 "void ScaDate( int nNullDate, int nDate, int nBase,int *nOrigDay, "
283 "int *nMonth,int *nYear,int *bLastDayMode,int *bLastDay,"
284 "int *b30Days,int *bUSMode,int *nDay);\n";
285 
286 std::string ScaDate=
287 "void ScaDate( int nNullDate, int nDate, int nBase,int *nOrigDay, "
288 "int *nMonth,int *nYear,int *bLastDayMode,int *bLastDay,"
289 "int *b30Days,int *bUSMode,int *nDay)\n"
290 "{\n"
291 " DaysToDate( nNullDate + nDate, nOrigDay, nMonth, nYear );\n"
292 " *bLastDayMode = (nBase != 5);\n"
293 " *bLastDay = (*nOrigDay >= DaysInMonth( *nMonth, *nYear ));\n"
294 " *b30Days = (nBase == 0) || (nBase == 4);\n"
295 " *bUSMode = (nBase == 0);\n"
296 " if( *b30Days)\n"
297 " {\n"
298 " *nDay = min( *nOrigDay, 30);\n"
299 " if( *bLastDay || (*nDay >=DaysInMonth( *nMonth, *nYear )) )\n"
300 " *nDay = 30;\n"
301 " }\n"
302 " else\n"
303 " {\n"
304 " int nLastDay = DaysInMonth( *nMonth, *nYear );\n"
305 " *nDay = *bLastDay ? nLastDay : min( *nOrigDay, nLastDay );\n"
306 " }\n"
307 "}\n";
308 
309 std::string ScaDate2Decl=
310 "void ScaDate2( int nNullDate, int nDate, int nBase,int *bLastDayMode,int *"
311 "bLastDay,int *b30Days,int *bUSMode);\n";
312 
313 std::string ScaDate2=
314 "void ScaDate2( int nNullDate, int nDate, int nBase,int *bLastDayMode,int *"
315 "bLastDay,int *b30Days,int *bUSMode)\n"
316 "{\n"
317 " int nOrigDay=0, nMonth=0, nYear=0;\n"
318 " DaysToDate( nNullDate + nDate, &nOrigDay, &nMonth, &nYear );\n"
319 " *bLastDayMode = (nBase != 5);\n"
320 " *bLastDay = (nOrigDay >= DaysInMonth( nMonth, nYear ));\n"
321 " *b30Days = (nBase == 0) || (nBase == 4);\n"
322 " *bUSMode = (nBase == 0);\n"
323 "}\n";
324 
325 std::string lcl_GetCouppcdDecl=
326 "int lcl_GetCouppcd(int nNullDate,int nSettle,int nMat,int nFreq,int nBase);\n";
327 
328 std::string lcl_GetCouppcd=
329 "int lcl_GetCouppcd(int nNullDate,int nSettle, int nMat,int nFreq,int nBase)\n"
330 "{\n"
331 " int aDate = nMat;\n"
332 " int rDay=0,rMonth=0, rYear=0,rbLastDayMode=0, rbLastDay=0,rb30Days=0,"
333 "rbUSMode=0,rnDay=0;\n"
334 " int sDay=0,sMonth=0, sYear=0,sbLastDayMode=0, sbLastDay=0,sb30Days=0,"
335 "sbUSMode=0,snDay=0;\n"
336 " ScaDate( nNullDate,nSettle,nBase,&sDay,&sMonth,&sYear,&sbLastDayMode,"
337 "&sbLastDay,&sb30Days,&sbUSMode,&snDay);\n"
338 " ScaDate( nNullDate,aDate,nBase,&rDay,&rMonth,&rYear,&rbLastDayMode,"
339 "&rbLastDay,&rb30Days,&rbUSMode,&rnDay);\n"
340 " rYear=sYear;\n"
341 " setDay(rDay,rMonth,rYear,rbLastDay,rb30Days,&rnDay);\n"
342 " if(checklessthan(rYear,sYear,rMonth,sMonth,rnDay,snDay,rbLastDay,"
343 "sbLastDay,rDay,sDay))\n"
344 " {\n"
345 " rYear+=1;\n"
346 " setDay(rDay,rMonth,rYear,rbLastDay,rb30Days,&rnDay);\n"
347 " }\n"
348 " while(checklessthan(sYear,rYear,sMonth,rMonth,snDay,rnDay,sbLastDay,"
349 "rbLastDay,sDay,rDay))\n"
350 " {\n"
351 " double d = -1*(12/nFreq);\n"
352 " addMonths(rb30Days,rbLastDay,&rnDay,rDay,&rMonth,d,&rYear);\n"
353 " }\n"
354 " int nLastDay = DaysInMonth( rMonth, rYear );\n"
355 " int nRealDay = (rbLastDayMode && rbLastDay) ? nLastDay :"
356 "min( nLastDay, rDay );\n"
357 " return DateToDays( nRealDay, rMonth, rYear ) - nNullDate;\n"
358 "}\n";
359 
360 std::string lcl_GetCoupncdDecl=
361 "int lcl_GetCoupncd(int nNullDate,int nSettle,int nMat,int nFreq,int nBase);\n";
362 
363 std::string lcl_GetCoupncd=
364 "int lcl_GetCoupncd(int nNullDate,int nSettle, int nMat,int nFreq,int nBase)\n"
365 "{\n"
366 " int aDate = nMat;\n"
367 " int rDay=0,rMonth=0, rYear=0,rbLastDayMode=0, rbLastDay=0,rb30Days=0,"
368 "rbUSMode=0,rnDay=0;\n"
369 " int sDay=0,sMonth=0, sYear=0,sbLastDayMode=0, sbLastDay=0,sb30Days=0,"
370 "sbUSMode=0,snDay=0;\n"
371 " ScaDate( nNullDate,nSettle,nBase,&sDay,&sMonth,&sYear,&sbLastDayMode,"
372 "&sbLastDay,&sb30Days,&sbUSMode,&snDay);\n"
373 " ScaDate( nNullDate,aDate,nBase,&rDay,&rMonth,&rYear,&rbLastDayMode,"
374 "&rbLastDay,&rb30Days,&rbUSMode,&rnDay);\n"
375 " rYear=sYear;\n"
376 " setDay(rDay,rMonth,rYear,rbLastDay,rb30Days,&rnDay);\n"
377 " if(checklessthan(sYear,rYear,sMonth,rMonth,snDay,rnDay,sbLastDay,rbLastDay"
378 ",sDay,rDay))\n"
379 " {\n"
380 " rYear-=1;\n"
381 " setDay(rDay,rMonth,rYear,rbLastDay,rb30Days,&rnDay);\n"
382 " }\n"
383 " while(!checklessthan(sYear,rYear,sMonth,rMonth,snDay,rnDay,sbLastDay,"
384 "rbLastDay,sDay,rDay))\n"
385 " {\n"
386 " addMonths(rb30Days,rbLastDay,&rnDay,rDay,&rMonth,12/nFreq,&rYear);\n"
387 " }\n"
388 " int nLastDay = DaysInMonth( rMonth, rYear );\n"
389 " int nRealDay = (rbLastDayMode && rbLastDay) ? nLastDay :"
390 "min( nLastDay, rDay );\n"
391 " return DateToDays( nRealDay, rMonth, rYear ) - nNullDate;\n"
392 "}\n";
393 
394 std::string addMonthsDecl=
395 "void addMonths(int b30Days,int bLastDay,int *nDay,int nOrigDay,"
396 "int *nMonth,int nMonthCount,int *year);\n";
397 
398 std::string addMonths=
399 "void addMonths(int b30Days,int bLastDay,int *nDay,int nOrigDay,"
400 "int *nMonth,int nMonthCount,int *year)\n"
401 "{\n"
402 " int nNewMonth = nMonthCount + *nMonth;\n"
403 " if( nNewMonth > 12 )\n"
404 " {\n"
405 " --nNewMonth;\n"
406 " *year+=nNewMonth / 12 ;\n"
407 " *nMonth = ( nNewMonth % 12 ) + 1;\n"
408 " }\n"
409 " else if( nNewMonth < 1 )\n"
410 " {\n"
411 " *year+= nNewMonth / 12 - 1 ;\n"
412 " *nMonth = nNewMonth % 12 + 12 ;\n"
413 " }\n"
414 " else\n"
415 " *nMonth = nNewMonth ;\n"
416 " if( b30Days )\n"
417 " {\n"
418 " *nDay = min( nOrigDay, 30);\n"
419 " if( bLastDay || (*nDay >= DaysInMonth( *nMonth, *year )) )\n"
420 " *nDay = 30;\n"
421 " }\n"
422 " else\n"
423 " {\n"
424 " int nLastDay = DaysInMonth( *nMonth, *year );\n"
425 " *nDay = bLastDay ? nLastDay : min( nOrigDay, nLastDay );\n"
426 " }\n"
427 "}\n";
428 
430 "int getDaysInMonthRange( int nFrom, int nTo,int b30Days,int year);\n";
431 
433 "int getDaysInMonthRange( int nFrom, int nTo,int b30Days,int year)\n"
434 "{\n"
435 " if( nFrom > nTo )\n"
436 " return 0;\n"
437 " int nRet = 0;\n"
438 " if( b30Days )\n"
439 " nRet = (nTo - nFrom + 1) * 30;\n"
440 " else\n"
441 " {\n"
442 " for( int nMonthIx = nFrom; nMonthIx <= nTo; ++nMonthIx )\n"
443 " nRet += b30Days ? 30 : DaysInMonth( nMonthIx, year );\n"
444 " }\n"
445 " return nRet;\n"
446 "}\n";
447 
448 std::string GetDaysInYearsDecl=
449 "int GetDaysInYears( int nYear1, int nYear2 );\n";
450 
451 std::string GetDaysInYears=
452 "int GetDaysInYears( int nYear1, int nYear2 )\n"
453 "{\n"
454 " int nLeaps = 0;\n"
455 " for( int n = nYear1 ; n <= nYear2 ; n++ )\n"
456 " {\n"
457 " if( IsLeapYear( n ) )\n"
458 " nLeaps++;\n"
459 " }\n"
460 " int nSum = 1;\n"
461 " nSum += nYear2;\n"
462 " nSum -= nYear1;\n"
463 " nSum *= 365;\n"
464 " nSum += nLeaps;\n"
465 " return nSum;\n"
466 "}\n";
467 
468 std::string GetDaysInYearDecl=
469 "int GetDaysInYear( int nNullDate, int nDate, int nMode );\n";
470 
471 std::string GetDaysInYear=
472 "int GetDaysInYear( int nNullDate, int nDate, int nMode )\n"
473 "{\n"
474 " switch( nMode )\n"
475 " {\n"
476 " case 0:\n"
477 " case 2:\n"
478 " case 4:\n"
479 " return 360;\n"
480 " case 1:\n"
481 " {\n"
482 " int nD=0, nM=0, nY=0;\n"
483 " nDate += nNullDate;\n"
484 " DaysToDate( nDate, &nD, &nM, &nY );\n"
485 " return IsLeapYear( nY )? 366 : 365;\n"
486 " }\n"
487 " case 3:\n"
488 " return 365;\n"
489 " }\n"
490 "}\n";
491 
493 "int getDaysInYearRange( int nFrom, int nTo,int b30Days );\n";
494 
495 std::string getDaysInYearRange=
496 "int getDaysInYearRange( int nFrom, int nTo,int b30Days )\n"
497 "{\n"
498 " if( nFrom > nTo )\n"
499 " return 0;\n"
500 " return b30Days ? ((nTo - nFrom + 1) * 360) : GetDaysInYears( nFrom, nTo)"
501 ";\n"
502 "}\n";
503 
504 std::string getDiffDecl=
505 "int getDiff(int rFrom,int rTo,int fDay,int fMonth,int fYear,int fbLastDayMode,"
506 "int fbLastDay,int fb30Days,int fbUSMode,int fnDay,int tDay,int tMonth,"
507 "int tYear,int tbLastDayMode,int tbLastDay,int tb30Days,"
508 "int tbUSMode,int tnDay);\n";
509 
510 std::string getDiff=
511 "int getDiff(int rFrom,int rTo,int fDay,int fMonth,int fYear,int fbLastDayMode,"
512 "int fbLastDay,int fb30Days,int fbUSMode,int fnDay,int tDay,int tMonth,"
513 "int tYear,int tbLastDayMode,int tbLastDay,int tb30Days,"
514 "int tbUSMode,int tnDay)\n"
515 "{\n"
516 " if(rFrom>rTo)\n"
517 " {\n"
518 " int d=fDay;fDay=tDay;tDay=d;\n"
519 " int m=fMonth;fMonth=tMonth;tMonth=m;\n"
520 " int y=fYear;fYear=tYear;tYear=y;\n"
521 " int a=fbLastDayMode;fbLastDayMode=tbLastDayMode;tbLastDayMode=a;\n"
522 " int b=fbLastDay;fbLastDay=tbLastDay;tbLastDay=b;\n"
523 " int c=fb30Days;fb30Days=tb30Days;tb30Days=c;\n"
524 " int e=fbUSMode;fbUSMode=tbUSMode;tbUSMode=e;\n"
525 " int f=fnDay;fnDay=tnDay;tnDay=f;\n"
526 " }\n"
527 " int nDiff=0;\n"
528 " if( tb30Days )\n"
529 " {\n"
530 " if( tbUSMode )\n"
531 " {\n"
532 " if( ((fMonth == 2) || (fnDay < 30)) && (tDay == 31) )\n"
533 " tnDay = 31;\n"
534 " else if( (tMonth == 2) && tbLastDay )\n"
535 " tnDay = DaysInMonth( 2, tYear );\n"
536 " }\n"
537 " else\n"
538 " {\n"
539 " if( (fMonth == 2) && (fnDay == 30) )\n"
540 " fnDay = DaysInMonth( 2, fYear );\n"
541 " if( (tMonth == 2) && (tnDay == 30) )\n"
542 " tnDay = DaysInMonth( 2, tYear );\n"
543 " }\n"
544 " }\n"
545 " if( (fYear < tYear) || ((fYear == tYear) && (fMonth < tMonth)) )\n"
546 " {\n"
547 " int d = fb30Days ? 30:DaysInMonth(fMonth,fYear);\n"
548 " nDiff = d- fnDay + 1;\n"
549 " fDay = fnDay = 1;\n"
550 " fbLastDay = 0;\n"
551 " addMonths(fb30Days,fbLastDay,&fnDay,fDay,&fMonth,1,&fYear);\n"
552 " if( fYear < tYear )\n"
553 " {\n"
554 " nDiff += getDaysInMonthRange( fMonth, 12,fb30Days,fYear);\n"
555 " addMonths(fb30Days,fbLastDay,&fnDay,fDay,&fMonth,13-fMonth,&fYear"
556 ");\n"
557 " nDiff += getDaysInYearRange( fYear, tYear - 1,fb30Days);\n"
558 " fYear+=tYear - fYear;\n"
559 " }\n"
560 " nDiff += getDaysInMonthRange(fMonth, tMonth - 1,fb30Days ,fYear );\n"
561 " addMonths(fb30Days,fbLastDay,&fnDay,fDay,&fMonth,tMonth-fMonth,&fYear"
562 ");\n"
563 " }\n"
564 " nDiff += tnDay - fnDay;\n"
565 " return nDiff > 0 ? nDiff : 0;\n"
566 "}\n";
567 
569 "int lcl_Getcoupdaybs(int nNullDate,int nSettle, int nMat,int nFreq,int nBase);\n";
570 
571 std::string lcl_Getcoupdaybs=
572 "int lcl_Getcoupdaybs(int nNullDate,int nSettle, int nMat,int nFreq,int nBase)\n"
573 "{\n"
574 " int aDate = nMat;\n"
575 " int rDay=0,rMonth=0, rYear=0;int mDay=0,mMonth=0, mYear=0;int sDay=0,"
576 "sMonth=0, sYear=0;\n"
577 " int rbLastDayMode=0, rbLastDay=0,rb30Days=0,rbUSMode=0,rnDay=0;\n"
578 " int sbLastDayMode=0, sbLastDay=0,sb30Days=0,sbUSMode=0,snDay=0;\n"
579 " ScaDate( nNullDate,aDate,nBase,&rDay,&rMonth,&rYear,&rbLastDayMode,&"
580 "rbLastDay,&rb30Days,&rbUSMode,&rnDay);\n"
581 " ScaDate( nNullDate,nSettle,nBase,&sDay,&sMonth,&sYear,&sbLastDayMode,&"
582 "sbLastDay,&sb30Days,&sbUSMode,&snDay);\n"
583 " rYear= sYear;\n"
584 " nSettle=nSettle+nNullDate;\n"
585 " aDate=DateToDays( rDay,rMonth,rYear );\n"
586 " if( aDate < nSettle )\n"
587 " {\n"
588 " rYear+= 1;\n"
589 " aDate=DateToDays( rDay,rMonth,rYear );\n"
590 " }\n"
591 " while(aDate > nSettle )\n"
592 " {\n"
593 " addMonths(rb30Days,rbLastDay,&rnDay,rDay,&rMonth,-1*(12/nFreq),&rYear"
594 ");\n"
595 " aDate=DateToDays( rDay,rMonth,rYear );\n"
596 " }\n"
597 " return getDiff( aDate, nSettle, rDay, rMonth, rYear, rbLastDayMode, "
598 "rbLastDay, rb30Days, rbUSMode, rnDay, sDay, sMonth, sYear, sbLastDayMode,"
599 "sbLastDay, sb30Days, sbUSMode, snDay);\n"
600 "}\n";
601 
603 "int lcl_Getcoupdaybs_new(int nNullDate,int nSettle,int nMat,int nFreq,"
604 "int nBase);\n";
605 
607 "int lcl_Getcoupdaybs_new(int nNullDate,int nSettle,int nMat,int nFreq,"
608 "int nBase)\n"
609 "{\n"
610 " int aDate = nMat;\n"
611 " int rDay=0,rMonth=0, rYear=0,rbLastDayMode=0, rbLastDay=0,rb30Days=0,"
612 "rbUSMode=0,rnDay=0;\n"
613 " int sDay=0,sMonth=0, sYear=0,sbLastDayMode=0, sbLastDay=0,sb30Days=0,"
614 "sbUSMode=0,snDay=0;\n"
615 " ScaDate( nNullDate,nSettle,nBase,&sDay,&sMonth,&sYear,&sbLastDayMode,"
616 "&sbLastDay,&sb30Days,&sbUSMode,&snDay);\n"
617 " ScaDate( nNullDate,aDate,nBase,&rDay,&rMonth,&rYear,&rbLastDayMode,"
618 "&rbLastDay,&rb30Days,&rbUSMode,&rnDay);\n"
619 " rYear=sYear;\n"
620 " setDay(rDay,rMonth,rYear,rbLastDay,rb30Days,&rnDay);\n"
621 " aDate=DateToDays_new( rnDay,rMonth,rYear);\n"
622 " if(checklessthan(rYear,sYear,rMonth,sMonth,rnDay,snDay,rbLastDay,"
623 "sbLastDay,rDay,sDay))\n"
624 " {\n"
625 " rYear+=1;\n"
626 " setDay(rDay,rMonth,rYear,rbLastDay,rb30Days,&rnDay);\n"
627 " aDate=DateToDays_new( rnDay,rMonth,rYear );\n"
628 " }\n"
629 " while(checklessthan(sYear,rYear,sMonth,rMonth,snDay,rnDay,sbLastDay,"
630 "rbLastDay,sDay,rDay))\n"
631 " {\n"
632 " double d = -1*(12/nFreq);\n"
633 " addMonths(rb30Days,rbLastDay,&rnDay,rDay,&rMonth,d,&rYear);\n"
634 " aDate=DateToDays_new( rnDay,rMonth,rYear );\n"
635 " }\n"
636 " return getDiff( aDate,nSettle+nNullDate,rDay,rMonth,rYear,rbLastDayMode,"
637 "rbLastDay,rb30Days,rbUSMode,rnDay,sDay,sMonth,sYear,sbLastDayMode,sbLastDay,"
638 "sb30Days,sbUSMode, snDay);\n"
639 "}\n";
640 
642 "int lcl_Getcoupdays(int nNullDate,int nSettle, "
643 "int nMat,int nFreq,int nBase);\n";
644 
645 std::string lcl_Getcoupdays=
646 "int lcl_Getcoupdays(int nNullDate,int nSettle, "
647 "int nMat,int nFreq,int nBase)\n"
648 "{\n"
649 " int aDate = nMat;\n"
650 " int rDay=0,rMonth=0, rYear=0;int mDay=0,mMonth=0, mYear=0;int sDay=0,"
651 "sMonth=0, sYear=0;\n"
652 " int rbLastDayMode=0, rbLastDay=0,rb30Days=0,rbUSMode=0,rnDay=0;\n"
653 " int sbLastDayMode=0, sbLastDay=0,sb30Days=0,sbUSMode=0,snDay=0;\n"
654 " ScaDate( nNullDate,aDate,nBase,&rDay,&rMonth,&rYear,&rbLastDayMode,&"
655 "rbLastDay,&rb30Days,&rbUSMode,&rnDay);\n"
656 " ScaDate( nNullDate,nSettle,nBase,&sDay,&sMonth,&sYear,&sbLastDayMode,&"
657 "sbLastDay,&sb30Days,&sbUSMode,&snDay);\n"
658 " rYear= sYear;\n"
659 " nSettle=nSettle+nNullDate;\n"
660 " aDate=DateToDays( rDay,rMonth,rYear );\n"
661 " if( aDate < nSettle )\n"
662 " { \n"
663 " rYear+= 1;\n"
664 " aDate=DateToDays( rDay,rMonth,rYear );\n"
665 " }\n"
666 " while(aDate > nSettle )\n"
667 " {\n"
668 " addMonths(rb30Days,rbLastDay,&rnDay,rDay,&rMonth,-1*(12/nFreq),&rYear"
669 ");\n"
670 " aDate=DateToDays( rDay,rMonth,rYear );\n"
671 " }\n"
672 " int aNextDate=aDate;int aDay=rDay,aMonth=rMonth, aYear=rYear;\n"
673 " int abLastDayMode=rbLastDayMode, abLastDay=rbLastDay,ab30Days=rb30Days,"
674 "abUSMode=rbUSMode,anDay=rnDay;\n"
675 " addMonths(ab30Days,abLastDay,&anDay,aDay,&aMonth,12/nFreq,&aYear);\n"
676 " return getDiff( aDate, aNextDate, rDay, rMonth, rYear, rbLastDayMode, "
677 "rbLastDay, rb30Days, rbUSMode, rnDay, aDay, aMonth, aYear, abLastDayMode,"
678 "abLastDay, ab30Days, abUSMode, anDay);\n"
679 "}\n";
680 
682 "int lcl_Getcoupdays_new(int nNullDate,int nSettle, "
683 "int nMat,int nFreq,int nBase);\n";
684 
686 "int lcl_Getcoupdays_new(int nNullDate,int nSettle, "
687 "int nMat,int nFreq,int nBase)\n"
688 "{\n"
689 " int aDate = nMat;\n"
690 " int rDay=0,rMonth=0, rYear=0,rbLastDayMode=0, rbLastDay=0,rb30Days=0,"
691 "rbUSMode=0,rnDay=0;\n"
692 " int sDay=0,sMonth=0, sYear=0,sbLastDayMode=0, sbLastDay=0,sb30Days=0,"
693 "sbUSMode=0,snDay=0;\n"
694 " ScaDate( nNullDate,nSettle,nBase,&sDay,&sMonth,&sYear,&sbLastDayMode,"
695 "&sbLastDay,&sb30Days,&sbUSMode,&snDay);\n"
696 " ScaDate( nNullDate,aDate,nBase,&rDay,&rMonth,&rYear,&rbLastDayMode,"
697 "&rbLastDay,&rb30Days,&rbUSMode,&rnDay);\n"
698 " rYear=sYear;\n"
699 " setDay(rDay,rMonth,rYear,rbLastDay,rb30Days,&rnDay);\n"
700 " aDate=DateToDays_new( rnDay,rMonth,rYear);\n"
701 " if(checklessthan(rYear,sYear,rMonth,sMonth,rnDay,snDay,rbLastDay,"
702 "sbLastDay,rDay,sDay))\n"
703 " {\n"
704 " rYear+=1;\n"
705 " setDay(rDay,rMonth,rYear,rbLastDay,rb30Days,&rnDay);\n"
706 " aDate=DateToDays_new( rnDay,rMonth,rYear );\n"
707 " }\n"
708 " while(checklessthan(sYear,rYear,sMonth,rMonth,snDay,rnDay,sbLastDay,"
709 "rbLastDay,sDay,rDay))\n"
710 " {\n"
711 " double d = -1*12*pow((double)nFreq,-1.0);\n"
712 " addMonths(rb30Days,rbLastDay,&rnDay,rDay,&rMonth,d,&rYear);\n"
713 " aDate=DateToDays_new( rnDay,rMonth,rYear );\n"
714 " }\n"
715 " int aNextDate=aDate;int aDay=rDay,aMonth=rMonth, aYear=rYear;\n"
716 " int abLastDayMode=rbLastDayMode, abLastDay=rbLastDay,ab30Days=rb30Days,"
717 "abUSMode=rbUSMode,anDay=rnDay;\n"
718 " int tmp = (int)(12*pow((double)nFreq,-1.0));\n"
719 " addMonths(ab30Days,abLastDay,&anDay,aDay,&aMonth,tmp,&aYear);\n"
720 " return getDiff( aDate, aNextDate, rDay, rMonth, rYear, rbLastDayMode, "
721 "rbLastDay, rb30Days, rbUSMode, rnDay, aDay, aMonth, aYear, abLastDayMode,"
722 "abLastDay, ab30Days, abUSMode, anDay);\n"
723 "}\n";
724 
725 std::string lcl_GetcoupnumDecl=
726 "int lcl_Getcoupnum(int nNullDate,int nSettle, int nMat,int nFreq);\n";
727 
728 std::string lcl_Getcoupnum=
729 "int lcl_Getcoupnum(int nNullDate,int nSettle, int nMat,int nFreq)\n"
730 "{\n"
731 " int aDate = nMat;int rDay=0,rMonth=0, rYear=0;int mDay=0,mMonth=0, mYear="
732 "0;\n"
733 " int sDay=0,sMonth=0, sYear=0;\n"
734 " DaysToDate(aDate+nNullDate,&rDay, &rMonth, &rYear );\n"
735 " DaysToDate(nMat+nNullDate,&mDay, &mMonth, &mYear );\n"
736 " DaysToDate(nSettle+nNullDate,&sDay, &sMonth, &sYear );\n"
737 " rYear= sYear;\n"
738 " nSettle=nSettle+nNullDate;\n"
739 " aDate=DateToDays( rDay,rMonth,rYear );\n"
740 " if( aDate < nSettle )\n"
741 " rYear+= 1;\n"
742 " int d=DateToDays( rDay,rMonth,rYear );\n"
743 " int nMonthCount=-1*(12 / nFreq);\n"
744 " while(d > nSettle )\n"
745 " {\n"
746 " int nNewMonth = nMonthCount + rMonth;\n"
747 " if( nNewMonth > 12 )\n"
748 " {\n"
749 " --nNewMonth;\n"
750 " rYear+=nNewMonth / 12;\n"
751 " rMonth = nNewMonth % 12 + 1;\n"
752 " }\n"
753 " else if( nNewMonth < 1 )\n"
754 " {\n"
755 " rYear+= nNewMonth / 12 - 1;\n"
756 " rMonth = nNewMonth % 12 + 12;\n"
757 " }\n"
758 " else\n"
759 " rMonth = nNewMonth;\n"
760 " d=DateToDays( rDay,rMonth,rYear );\n"
761 " }\n"
762 " int n=(mYear-rYear)*12+mMonth-rMonth;\n"
763 " n=n*nFreq/12;\n"
764 " return n;\n"
765 "}\n";
767 "double lcl_Getcoupnum_new(int nNullDate,int nSettle,int nMat,int nFreq,int"
768 " nBase);\n";
769 std::string lcl_Getcoupnum_new=
770 "double lcl_Getcoupnum_new(int nNullDate,int nSettle, int nMat,int nFreq,int"
771 " nBase)\n"
772 "{\n"
773 " int aDate = nMat;\n"
774 " int mDay=0,mMonth=0, mYear=0;\n"
775 " int rDay=0,rMonth=0, rYear=0,rbLastDayMode=0, rbLastDay=0,rb30Days=0,"
776 "rbUSMode=0,rnDay=0;\n"
777 " int sDay=0,sMonth=0, sYear=0,sbLastDayMode=0, sbLastDay=0,sb30Days=0,"
778 "sbUSMode=0,snDay=0;\n"
779 " ScaDate( nNullDate,nSettle,nBase,&sDay,&sMonth,&sYear,&sbLastDayMode,"
780 "&sbLastDay,&sb30Days,&sbUSMode,&snDay);\n"
781 " ScaDate( nNullDate,aDate,nBase,&rDay,&rMonth,&rYear,&rbLastDayMode,"
782 "&rbLastDay,&rb30Days,&rbUSMode,&rnDay);\n"
783 " mMonth = rMonth, mYear = rYear;\n"
784 " rYear=sYear;\n"
785 " setDay(rDay,rMonth,rYear,rbLastDay,rb30Days,&rnDay);\n"
786 " if(checklessthan(rYear,sYear,rMonth,sMonth,rnDay,snDay,rbLastDay,"
787 "sbLastDay,rDay,sDay))\n"
788 " {\n"
789 " rYear+=1;\n"
790 " setDay(rDay,rMonth,rYear,rbLastDay,rb30Days,&rnDay);\n"
791 " }\n"
792 " int m= checklessthan(sYear,rYear,sMonth,rMonth,snDay,rnDay,sbLastDay,"
793 "rbLastDay,sDay,rDay);\n"
794 " while(m)\n"
795 " {\n"
796 " double d = -1*(12/nFreq);\n"
797 " addMonths(rb30Days,rbLastDay,&rnDay,rDay,&rMonth,d,&rYear);\n"
798 " m = checklessthan(sYear,rYear,sMonth,rMonth,snDay,rnDay,sbLastDay,"
799 "rbLastDay,sDay,rDay);\n"
800 " }\n"
801 " int n=(mYear-rYear)*12+mMonth-rMonth;\n"
802 " double tmp = (double)(n*nFreq)/12.0;\n"
803 " return tmp;\n"
804 "}\n";
805 
806 std::string setDayDecl=
807 "void setDay(int nOrigDay, int nMonth,int nYear,int bLastDay,int b30Days,"
808 "int *nDay);\n";
809 std::string setDay=
810 "void setDay(int nOrigDay, int nMonth,int nYear,int bLastDay,int b30Days,"
811 "int *nDay)\n"
812 "{\n"
813 " if( b30Days )\n"
814 " {\n"
815 " *nDay = min( nOrigDay, 30);\n"
816 " if( bLastDay || (*nDay >= DaysInMonth( nMonth, nYear )) )\n"
817 " *nDay = 30;\n"
818 " }\n"
819 " else\n"
820 " {\n"
821 " int nLastDay = DaysInMonth( nMonth, nYear );\n"
822 " *nDay = bLastDay ? nLastDay : min( nOrigDay, nLastDay );\n"
823 " }\n"
824 "}\n";
825 
826 std::string coupdaysDecl=
827 "double coupdays(int nSettle,int nMat,int nFreq,int nBase);\n";
828 
829 std::string coupdays=
830 "double coupdays(int nSettle,int nMat,int nFreq,int nBase)\n"
831 "{\n"
832 " int nNullDate=GetNullDate();\n"
833 " if( nBase == 1 )\n"
834 " return lcl_Getcoupdays(nNullDate, nSettle, nMat,nFreq, nBase);\n"
835 " else\n"
836 " return (double)GetDaysInYear(0,0,nBase)/nFreq;\n"
837 "}\n";
838 std::string coupdays_newDecl=
839 "double coupdays_new(int nSettle,int nMat,int nFreq,int nBase);\n";
840 
841 std::string coupdays_new=
842 "double coupdays_new(int nSettle,int nMat,int nFreq,int nBase)\n"
843 "{\n"
844 " int nNullDate=693594;\n"
845 " if( nBase == 1 )\n"
846 " return lcl_Getcoupdays_new(nNullDate, nSettle, nMat,nFreq, nBase);\n"
847 " else\n"
848 " return (double)GetDaysInYear(0,0,nBase)*pow((double)nFreq,-1.0);\n"
849 "}\n";
850 
851 std::string coupdaybsDecl=
852 "double coupdaybs( int nSettle,int nMat,int nFreq,int nBase);\n";
853 
854 std::string coupdaybs=
855 "double coupdaybs( int nSettle,int nMat,int nFreq,int nBase)\n"
856 "{\n"
857 " int nNullDate=GetNullDate();\n"
858 " return lcl_Getcoupdaybs(nNullDate, nSettle, nMat,nFreq, nBase);\n"
859 "}\n";
860 
861 std::string coupdaybs_newDecl=
862 "double coupdaybs_new( int nSettle,int nMat,int nFreq,int nBase);\n";
863 
864 std::string coupdaybs_new=
865 "double coupdaybs_new( int nSettle,int nMat,int nFreq,int nBase)\n"
866 "{\n"
867 " int nNullDate=693594;\n"
868 " return lcl_Getcoupdaybs_new(nNullDate, nSettle, nMat,nFreq, nBase);\n"
869 "}\n";
870 
871 std::string coupdaysncDecl=
872 "double coupdaysnc( int nSettle,int nMat,int nFreq,int nBase);\n";
873 
874 std::string coupdaysnc=
875  "double coupdaysnc( int nSettle,int nMat,int nFreq,int nBase)\n"
876 "{\n"
877 " int nNullDate=GetNullDate();\n"
878 " if((nBase != 0) && (nBase != 4))\n"
879 " {\n"
880 " int aDate = nMat;\n"
881 " int rDay=0,rMonth=0, rYear=0;int mDay=0,mMonth=0, mYear=0;int sDay=0,"
882 "sMonth=0, sYear=0;\n"
883 " int rbLastDayMode=0, rbLastDay=0,rb30Days=0,rbUSMode=0,rnDay=0;\n"
884 " int sbLastDayMode=0, sbLastDay=0,sb30Days=0,sbUSMode=0,snDay=0;\n"
885 " ScaDate( nNullDate,aDate,nBase,&rDay,&rMonth,&rYear,&rbLastDayMode,&"
886 "rbLastDay,&rb30Days,&rbUSMode,&rnDay);\n"
887 " ScaDate( nNullDate,nSettle,nBase,&sDay,&sMonth,&sYear,&sbLastDayMode,"
888 "&sbLastDay,&sb30Days,&sbUSMode,&snDay);\n"
889 " rYear= sYear;\n"
890 " nSettle=nSettle+nNullDate;\n"
891 " aDate=DateToDays( rDay,rMonth,rYear );\n"
892 " if( aDate > nSettle )\n"
893 " {\n"
894 " rYear-= 1;\n"
895 " aDate=DateToDays( rDay,rMonth,rYear );\n"
896 " }\n"
897 " while(aDate <= nSettle )\n"
898 " {\n"
899 " addMonths(rb30Days,rbLastDay,&rnDay,rDay,&rMonth,12/nFreq,&rYear)"
900 ";\n"
901 " aDate=DateToDays( rDay,rMonth,rYear );\n"
902 " }\n"
903 " return getDiff( nSettle, aDate, sDay, sMonth, sYear, sbLastDayMode, "
904 "sbLastDay, sb30Days, sbUSMode, snDay, rDay, rMonth, rYear, rbLastDayMode, "
905 "rbLastDay, rb30Days, rbUSMode, rnDay);\n"
906 " }\n"
907 " else\n"
908 " return coupdays(nSettle,nMat,nFreq,nBase)- coupdaybs( nSettle,nMat,"
909 "nFreq,nBase);\n"
910 "}\n";
911 std::string coupdaysnc_newDecl=
912 "double coupdaysnc_new( int nSettle,int nMat,int nFreq,int nBase);\n";
913 
914 std::string coupdaysnc_new=
915 "double coupdaysnc_new( int nSettle,int nMat,int nFreq,int nBase)\n"
916 "{\n"
917 " int nNullDate=693594;\n"
918 " if((nBase != 0) && (nBase != 4))\n"
919 " {\n"
920 " int aDate = nMat;\n"
921 " int rDay=0,rMonth=0, rYear=0,rbLastDayMode=0, rbLastDay=0,rb30Days=0,"
922 "rbUSMode=0,rnDay=0;\n"
923 " int sDay=0,sMonth=0, sYear=0,sbLastDayMode=0, sbLastDay=0,sb30Days=0,"
924 "sbUSMode=0,snDay=0;\n"
925 " ScaDate( nNullDate,aDate,nBase,&rDay,&rMonth,&rYear,&rbLastDayMode,"
926 "&rbLastDay,&rb30Days,&rbUSMode,&rnDay);\n"
927 " ScaDate( nNullDate,nSettle,nBase,&sDay,&sMonth,&sYear,&sbLastDayMode,"
928 "&sbLastDay,&sb30Days,&sbUSMode,&snDay);\n"
929 " rYear=sYear;\n"
930 " setDay(rDay,rMonth,rYear,rbLastDay,rb30Days,&rnDay);\n"
931 " aDate=DateToDays( rnDay,rMonth,rYear);\n"
932 " if(checklessthan(sYear,rYear,sMonth,rMonth,snDay,rnDay,sbLastDay,rbLastDay"
933 ",sDay,rDay))\n"
934 " {\n"
935 " rYear-=1;\n"
936 " setDay(rDay,rMonth,rYear,rbLastDay,rb30Days,&rnDay);\n"
937 " aDate=DateToDays( rnDay,rMonth,rYear );\n"
938 " }\n"
939 " while(!checklessthan(sYear,rYear,sMonth,rMonth,snDay,rnDay,sbLastDay,"
940 "rbLastDay,sDay,rDay))\n"
941 " {\n"
942 " addMonths(rb30Days,rbLastDay,&rnDay,rDay,&rMonth,12/nFreq,&rYear);\n"
943 " aDate=DateToDays( rnDay,rMonth,rYear );\n"
944 " }\n"
945 " return getDiff( nSettle+nNullDate,aDate,sDay,sMonth,sYear,sbLastDayMode, "
946 "sbLastDay, sb30Days, sbUSMode, snDay, rDay, rMonth, rYear, rbLastDayMode, "
947 "rbLastDay, rb30Days, rbUSMode, rnDay);\n"
948 " }\n"
949 " else\n"
950 " return coupdays_new(nSettle,nMat,nFreq,nBase)- coupdaybs_new( nSettle,"
951 "nMat,nFreq,nBase);\n"
952 "}\n";
953 
954 std::string checklessthanDecl=
955 "int checklessthan(int aYear,int bYear,int aMonth,int bMonth,int anDay,int "
956 "bnDay,int abLastDay,int bbLastDay,int anOrigDay,int bnOrigDay);\n";
957 std::string checklessthan=
958 "int checklessthan(int aYear,int bYear,int aMonth,int bMonth,int anDay,int "
959 "bnDay,int abLastDay,int bbLastDay,int anOrigDay,int bnOrigDay)\n"
960 "{\n"
961 " if( aYear != bYear )\n"
962 " return aYear < bYear;\n"
963 " if( aMonth != bMonth )\n"
964 " return aMonth < bMonth;\n"
965 " if( anDay != bnDay )\n"
966 " return anDay < bnDay;\n"
967 " if( abLastDay || bbLastDay )\n"
968 " return !abLastDay && bbLastDay;\n"
969 " return anOrigDay < bnOrigDay;\n"
970 "}\n";
971 
972 std::string coupnumDecl=
973 "double coupnum( int nSettle,int nMat,int nFreq,int nBase);\n";
974 
975 std::string coupnum=
976 "double coupnum( int nSettle,int nMat,int nFreq,int nBase)\n"
977 "{\n"
978 " int nNullDate=GetNullDate();\n"
979 " return lcl_Getcoupnum(nNullDate,nSettle,nMat,nFreq);\n"
980 "}\n";
981 std::string coupnum_newDecl=
982 "double coupnum_new( int nSettle,int nMat,int nFreq,int nBase);\n";
983 
984 std::string coupnum_new=
985 "double coupnum_new( int nSettle,int nMat,int nFreq,int nBase)\n"
986 "{\n"
987 " int nNullDate=693594;\n"
988 " return lcl_Getcoupnum_new(nNullDate,nSettle,nMat,nFreq,nBase);\n"
989 "}\n";
990 
991 std::string getPrice_Decl=
992 "double getPrice_(int nSettle, int nMat, double fRate, double fYield,\n"
993  "double fRedemp, int nFreq, int nBase );\n";
994 
995 std::string getPrice_=
996 "double getPrice_(int nSettle, int nMat, double fRate, double fYield,\n"
997  "double fRedemp, int nFreq, int nBase )\n"
998 "{\n"
999 " double fFreq = nFreq;\n"
1000 " double fE = coupdays( nSettle, nMat, nFreq, nBase );\n"
1001 " double fDSC_E = coupdaysnc( nSettle, nMat, nFreq, nBase ) / fE;\n"
1002 " double fN = coupnum( nSettle, nMat, nFreq, nBase );\n"
1003 " double fA = coupdaybs( nSettle, nMat, nFreq, nBase );\n"
1004 " double fRet = fRedemp / ( pow( 1.0 + fYield / fFreq, fN - 1.0 + "
1005 "fDSC_E ) );\n"
1006 " fRet -= 100.0 * fRate / fFreq * fA / fE;\n"
1007 " double fT1 = 100.0 * fRate / fFreq;\n"
1008 " double fT2 = 1.0 + fYield / fFreq;\n"
1009 " for( double fK = 0.0 ; fK < fN ; fK+=1.0 )\n"
1010 " fRet += fT1 / pow( fT2, fK + fDSC_E );\n"
1011 " return fRet;\n"
1012 "}\n";
1013 std::string getPrice_new_Decl=
1014 "double getPrice_(int nSettle, int nMat, double fRate, double fYield,\n"
1015  "double fRedemp, int nFreq, int nBase );\n";
1016 
1017 std::string getPrice_new=
1018 "double getPrice_(int nSettle, int nMat, double fRate, double fYield,\n"
1019  "double fRedemp, int nFreq, int nBase )\n"
1020 "{\n"
1021 " double fFreq = nFreq;\n"
1022 " double fE = coupdays_new( nSettle, nMat, nFreq, nBase );\n"
1023 " double fDSC_E = coupdaysnc_new( nSettle, nMat, nFreq, nBase ) / fE;\n"
1024 " double fN = coupnum_new( nSettle, nMat, nFreq, nBase );\n"
1025 " double fA = coupdaybs_new( nSettle, nMat, nFreq, nBase );\n"
1026 " double fRet = fRedemp / ( pow( 1.0 + fYield / fFreq, fN - 1.0 + "
1027 "fDSC_E ) );\n"
1028 " fRet -= 100.0 * fRate / fFreq * fA / fE;\n"
1029 " double fT1 = 100.0 * fRate / fFreq;\n"
1030 " double fT2 = 1.0 + fYield / fFreq;\n"
1031 " for( double fK = 0.0 ; fK < fN ; fK+=1.0 )\n"
1032 " fRet += fT1 / pow( fT2, fK + fDSC_E );\n"
1033 " return fRet;\n"
1034 "}\n";
1035 
1036 std::string getYield_Decl=
1037 "double getYield_( int nNullDate, int nSettle, int nMat, double fCoup,"
1038 "double fPrice,double fRedemp, int nFreq, int nBase);\n";
1039 
1040 std::string getYield_=
1041 "double getYield_( int nNullDate, int nSettle, int nMat, double fCoup,"
1042 "double fPrice,double fRedemp, int nFreq, int nBase )\n"
1043 "{\n"
1044 " double fRate = fCoup;\n"
1045 " double fPriceN = 0.0;\n"
1046 " double fYield1 = 0.0;\n"
1047 " double fYield2 = 1.0;\n"
1048 " double fPrice1 = getPrice_(nSettle, nMat, fRate, fYield1, fRedemp, "
1049 "nFreq, nBase );\n"
1050 " double fPrice2 = getPrice_(nSettle, nMat, fRate, fYield2, fRedemp, "
1051 "nFreq, nBase );\n"
1052 " double fYieldN = ( fYield2 - fYield1 ) * 0.5;\n"
1053 " for( unsigned int nIter = 0 ; nIter < 100 && fPriceN != fPrice ; nIter++ "
1054 ")\n"
1055 " {\n"
1056 " fPriceN = getPrice_(nSettle, nMat, fRate, fYieldN, fRedemp, nFreq, "
1057 "nBase );\n"
1058 " if( fPrice == fPrice1 )\n"
1059 " return fYield1;\n"
1060 " else if( fPrice == fPrice2 )\n"
1061 " return fYield2;\n"
1062 " else if( fPrice == fPriceN )\n"
1063 " return fYieldN;\n"
1064 " else if( fPrice < fPrice2 )\n"
1065 " {\n"
1066 " fYield2 *= 2.0;\n"
1067 " fPrice2 = getPrice_(nSettle, nMat, fRate, fYield2, fRedemp, nFreq"
1068 ", nBase );\n"
1069 " fYieldN = ( fYield2 - fYield1 ) * 0.5;\n"
1070 " }\n"
1071 " else\n"
1072 " {\n"
1073 " if( fPrice < fPriceN )\n"
1074 " {\n"
1075 " fYield1 = fYieldN;\n"
1076 " fPrice1 = fPriceN;\n"
1077 " }\n"
1078 " else\n"
1079 " {\n"
1080 " fYield2 = fYieldN;\n"
1081 " fPrice2 = fPriceN;\n"
1082 " }\n"
1083 " fYieldN = fYield2 - ( fYield2 - fYield1 ) * ( ( fPrice - fPrice2 "
1084 ") / ( fPrice1 - fPrice2 ) );\n"
1085 " }\n"
1086 " }\n"
1087 " return fYieldN;\n"
1088 "}\n";
1089 
1090 std::string GetYearFracDecl=
1091 "double GetYearFrac( int nNullDate, int nStartDate, int nEndDate,"
1092 "int nMode );\n";
1093 
1094 std::string GetYearFrac=
1095 "double GetYearFrac( int nNullDate, int nStartDate, int nEndDate,"
1096 "int nMode ) \n"
1097 "{\n"
1098 " if( nStartDate == nEndDate )\n"
1099 " return 0.0; \n"
1100 
1101  " if( nStartDate > nEndDate )\n"
1102  " {\n"
1103  " int n = nEndDate;\n"
1104  " nEndDate = nStartDate;\n"
1105  " nStartDate = n;\n"
1106  " }\n"
1107 
1108  " int nDate1 = nStartDate + nNullDate;\n"
1109  " int nDate2 = nEndDate + nNullDate;\n"
1110 
1111  " int nDay1, nDay2;\n"
1112  " int nMonth1, nMonth2;\n"
1113  " int nYear1, nYear2;\n"
1114 
1115  " DaysToDate( nDate1, &nDay1, &nMonth1, &nYear1 );\n"
1116  " DaysToDate( nDate2, &nDay2, &nMonth2, &nYear2 );\n"
1117 
1118  " int nDayDiff;\n"
1119  " switch( nMode )\n"
1120  " {\n"
1121  " case 0: \n"
1122  " if ( nDay1 == 31 )\n"
1123  " {\n"
1124  " nDay1--;\n"
1125  " }\n"
1126  " if ( nDay1 == 30 && nDay2 == 31 )\n"
1127  " {\n"
1128  " nDay2--;\n"
1129  " }\n"
1130  " else\n"
1131  " {\n"
1132  " if ( nMonth1 == 2 && nDay1 == "
1133  "( IsLeapYear( nYear1 ) ? 29 : 28 ) )\n"
1134  " {\n"
1135  " nDay1 = 30;\n"
1136  " if ( nMonth2 == 2 && nDay2 == "
1137  "( IsLeapYear( nYear2 ) ? 29 : 28 ) )\n"
1138  " {\n"
1139  " nDay2 = 30;\n"
1140  " }\n"
1141  " }\n"
1142  " }\n"
1143  " nDayDiff = ( nYear2 - nYear1 ) * 360 + "
1144  "( nMonth2 - nMonth1 ) * 30 + ( nDay2 - nDay1 );\n"
1145  " break;\n"
1146  " case 1: \n"
1147  " case 2: \n"
1148  " case 3: \n"
1149  " nDayDiff = nDate2 - nDate1;\n"
1150  " break;\n"
1151  " case 4: \n"
1152  " if ( nDay1 == 31 )\n"
1153  " {\n"
1154  " nDay1--;\n"
1155  " }\n"
1156  " if ( nDay2 == 31 )\n"
1157  " {\n"
1158  " nDay2--;\n"
1159  " }\n"
1160  " nDayDiff = ( nYear2 - nYear1 ) * 360 + "
1161  "( nMonth2 - nMonth1 ) * 30 + ( nDay2 - nDay1 );\n"
1162  " break;\n"
1163  " }\n"
1164 
1165  " double nDaysInYear;\n"
1166  " switch( nMode )\n"
1167  " {\n"
1168  " case 0: \n"
1169  " case 2: \n"
1170  " case 4: \n"
1171  " nDaysInYear = 360;\n"
1172  " break;\n"
1173  " case 1: \n"
1174  " {\n"
1175  " bool isYearDifferent = ( nYear1 != nYear2 );\n"
1176  " if ( isYearDifferent &&\n"
1177  " ( ( nYear2 != nYear1 + 1 ) ||\n"
1178  " ( nMonth1 < nMonth2 ) ||\n"
1179  " ( nMonth1 == nMonth2 && nDay1 < nDay2 ) ) )\n"
1180  " {\n"
1181 
1182  " int nDayCount = 0;\n"
1183  " for ( int i = nYear1; i <= nYear2; i++ )\n"
1184  " nDayCount += ( IsLeapYear( i ) ? 366 : 365 );\n"
1185 
1186  " nDaysInYear = ( double ) nDayCount / "
1187  "( double ) ( nYear2 - nYear1 + 1 );\n"
1188  " }\n"
1189  " else\n"
1190  " {\n"
1191  " if ( isYearDifferent && IsLeapYear( nYear1 ) )\n"
1192  " {\n"
1193  " nDaysInYear = 366;\n"
1194  " }\n"
1195  " else\n"
1196  " {\n"
1197 
1198  " if ( ( IsLeapYear( nYear1 ) && nMonth1 <= 2 "
1199  "&& nDay1 <= 29 ) ||\n"
1200  " ( IsLeapYear( nYear2 ) && ( nMonth2 > 3 || "
1201  "( nMonth2 == 2 && nDay1 == 29 ) ) ) )\n"
1202  " {\n"
1203  " nDaysInYear = 366;\n"
1204  " }\n"
1205  " else\n"
1206  " {\n"
1207  " nDaysInYear = 365;\n"
1208  " for ( int i = nYear1; i <= nYear2; i++ )\n"
1209  " {\n"
1210  " if ( IsLeapYear( i ) )\n"
1211  " {\n"
1212  " nDaysInYear = 366;\n"
1213  " break;\n"
1214  " }\n"
1215  " }\n"
1216  " }\n"
1217  " }\n"
1218  " }\n"
1219  " }\n"
1220  " break;\n"
1221  " case 3: \n"
1222  " nDaysInYear = 365;\n"
1223  " break;\n"
1224  " }\n"
1225  " return (double)( nDayDiff ) / (nDaysInYear);\n"
1226 "}\n";
1227 
1228 std::string GetYieldmatDecl=
1229  "double GetYieldmat( int nNullDate, int nSettle, int nMat, int nIssue,\n"
1230  "double fRate, double fPrice, int nBase );\n";
1231 
1232 std::string GetYieldmat=
1233  "double GetYieldmat( int nNullDate, int nSettle, int nMat, int nIssue,\n"
1234  "double fRate, double fPrice, int nBase )\n"
1235 "{\n"
1236 " double fIssMat = GetYearFrac_new( nNullDate, nIssue, nMat, nBase );\n"
1237 " double fIssSet = GetYearFrac_new( nNullDate, nIssue, nSettle, nBase );\n"
1238 " double fSetMat = GetYearFrac_new( nNullDate, nSettle, nMat, nBase );\n"
1239 " double y = 1.0 + fIssMat * fRate;\n"
1240 " y =y * pow( (fPrice / 100.0 + fIssSet * fRate),-1);\n"
1241 " y-=1.0;\n"
1242 " y = y * pow(fSetMat,-1);\n"
1243 " return y;\n"
1244 "}\n";
1245 
1246 std::string GetDiffDateDecl=
1247 "int GetDiffDate( int nNullDate, int nStartDate, int nEndDate, int nMode,"
1248 " int* pOptDaysIn1stYear );\n";
1249 
1250 std::string GetDiffDate=
1251 "int GetDiffDate( int nNullDate, int nStartDate, int nEndDate, int nMode,"
1252 " int* pOptDaysIn1stYear )\n"
1253 "{\n"
1254 " bool bNeg = nStartDate > nEndDate;\n"
1255 " if( bNeg )\n"
1256 " {\n"
1257 " int n = nEndDate;\n"
1258 " nEndDate = nStartDate;\n"
1259 " nStartDate = n;\n"
1260 " }\n"
1261 " int nRet;\n"
1262 " switch( nMode )\n"
1263 " {\n"
1264 " case 0: \n"
1265 " case 4: \n"
1266 " {\n"
1267 " int nD1, nM1, nY1, nD2, nM2, nY2;\n"
1268 " nStartDate += nNullDate;\n"
1269 " nEndDate += nNullDate;\n"
1270 " DaysToDate( nStartDate, &nD1, &nM1, &nY1 );\n"
1271 " DaysToDate( nEndDate, &nD2, &nM2, &nY2 );\n"
1272 " bool bLeap = IsLeapYear( nY1 );\n"
1273 " int nDays, nMonths;\n"
1274 " nMonths = nM2 - nM1;\n"
1275 " nDays = nD2 - nD1;\n"
1276 " nMonths += ( nY2 - nY1 ) * 12;\n"
1277 " nRet = nMonths * 30 + nDays;\n"
1278 " if( nMode == 0 && nM1 == 2 && nM2 != 2 && nY1 == nY2 )\n"
1279 " nRet -= bLeap? 1 : 2;\n"
1280 " if( pOptDaysIn1stYear )\n"
1281 " *pOptDaysIn1stYear = 360;\n"
1282 " }\n"
1283 " break;\n"
1284 " case 1: \n"
1285 " if( pOptDaysIn1stYear )\n"
1286 " {\n"
1287 " int nD, nM, nY;\n"
1288 " DaysToDate( nStartDate + nNullDate, &nD, &nM, &nY );\n"
1289 " *pOptDaysIn1stYear = IsLeapYear( nY )? 366 : 365;\n"
1290 " }\n"
1291 " nRet = nEndDate - nStartDate;\n"
1292 " break;\n"
1293 " case 2: \n"
1294 " nRet = nEndDate - nStartDate;\n"
1295 " if( pOptDaysIn1stYear )\n"
1296 " *pOptDaysIn1stYear = 360;\n"
1297 " break;\n"
1298 " case 3: \n"
1299 " nRet = nEndDate - nStartDate;\n"
1300 " if( pOptDaysIn1stYear )\n"
1301 " *pOptDaysIn1stYear = 365;\n"
1302 " break;\n"
1303 " }\n"
1304 " return bNeg? -nRet : nRet;\n"
1305 "}\n";
1306 
1307 std::string GetYearDiffDecl=
1308 "double GetYearDiff( int nNullDate, int nStartDate, int nEndDate,"
1309 "int nMode);\n";
1310 
1311 std::string GetYearDiff=
1312 "double GetYearDiff( int nNullDate, int nStartDate, int nEndDate,"
1313 "int nMode )\n"
1314 "{\n"
1315 " int nDays1stYear;\n"
1316 " int nTotalDays = GetDiffDate( nNullDate, nStartDate, nEndDate,"
1317 "nMode, &"
1318 "nDays1stYear );\n"
1319 " return (double)(nTotalDays)*pow((double)nDays1stYear,-1);\n"
1320 "}\n";
1321 
1323 "int GetDiffDate360_(\n"
1324 " int nDay1, int nMonth1, int nYear1, bool bLeapYear1,\n"
1325 " int nDay2, int nMonth2, int nYear2,\n"
1326 " bool bUSAMethod );\n";
1327 
1328 std::string GetDiffDate360_=
1329 "int GetDiffDate360_(\n"
1330 " int nDay1, int nMonth1, int nYear1, bool bLeapYear1,\n"
1331 " int nDay2, int nMonth2, int nYear2,\n"
1332 " bool bUSAMethod )\n"
1333 "{\n"
1334 " if( nDay1 == 31 )\n"
1335 " nDay1--;\n"
1336 " else if( bUSAMethod && ( nMonth1 == 2 && ( nDay1 == 29 || ( nDay1 == 28 "
1337 "&& !bLeapYear1 ) ) ) )\n"
1338 " nDay1 = 30;\n"
1339 " if( nDay2 == 31 )\n"
1340 " {\n"
1341 " if( bUSAMethod && nDay1 != 30 )\n"
1342 " {\n"
1343 " nDay2 = 1;\n"
1344 " if( nMonth2 == 12 )\n"
1345 " {\n"
1346 " nYear2++;\n"
1347 " nMonth2 = 1;\n"
1348 " }\n"
1349 " else\n"
1350 " nMonth2++;\n"
1351 " }\n"
1352 " else\n"
1353 " nDay2 = 30;\n"
1354 " }\n"
1355 " return nDay2 + nMonth2 * 30 + nYear2 * 360 - nDay1 - nMonth1 * 30 - "
1356 "nYear1 * 360;\n"
1357 "}\n";
1358 
1360 "int GetDiffDate360( int nNullDate, int nDate1, int nDate2,"
1361 "bool bUSAMethod);\n";
1362 
1363 std::string GetDiffDate360=
1364 "int GetDiffDate360( int nNullDate, int nDate1, int nDate2,"
1365 "bool bUSAMethod )\n"
1366 "{\n"
1367 " nDate1 += nNullDate;\n"
1368 " nDate2 += nNullDate;\n"
1369 " int nDay1, nMonth1, nYear1, nDay2, nMonth2, nYear2;\n"
1370 " DaysToDate( nDate1, &nDay1, &nMonth1, &nYear1 );\n"
1371 " DaysToDate( nDate2, &nDay2, &nMonth2, &nYear2 );\n"
1372 " return GetDiffDate360_( nDay1, nMonth1, nYear1, IsLeapYear( nYear1 ), "
1373 "nDay2, nMonth2, nYear2, bUSAMethod );\n"
1374 "}\n";
1375 
1376 std::string GetDurationDecl=
1377 "double GetDuration( \n"
1378 " int nNullDate, int nSettle, int nMat, double fCoup,\n"
1379 " double fYield, int nFreq, int nBase );\n";
1380 
1381 std::string GetDuration=
1382 "double GetDuration( \n"
1383 " int nNullDate, int nSettle, int nMat, double fCoup,\n"
1384 " double fYield, int nFreq, int nBase )\n"
1385 "{\n"
1386 " double fYearfrac = GetYearFrac( nNullDate, nSettle, nMat, nBase );\n"
1387 " double fNumOfCoups = lcl_Getcoupnum(nNullDate,nSettle,nMat,nFreq);\n"
1388 " double fDur = 0.0;\n"
1389 " double f100 = 100.0;\n"
1390 " fCoup *= f100 / nFreq;\n"
1391 " fYield /= nFreq;\n"
1392 " fYield += 1.0;\n"
1393 " double nDiff = fYearfrac * nFreq - fNumOfCoups;\n"
1394 " int t;\n"
1395 " for( t = 1 ; t < fNumOfCoups ; t++ )\n"
1396 " fDur += ( t + nDiff ) * ( fCoup ) / pow( fYield, t + nDiff );\n"
1397 " fDur += ( fNumOfCoups + nDiff ) * ( fCoup + f100 ) /"
1398 " pow( fYield, fNumOfCoups + nDiff );\n"
1399 " double p = 0.0;\n"
1400 " for( t = 1 ; t < fNumOfCoups ; t++ )\n"
1401 " p += fCoup / pow( fYield, t + nDiff );\n"
1402 " p += ( fCoup + f100 ) / pow( fYield, fNumOfCoups + nDiff );\n"
1403 " fDur /= p;\n"
1404 " fDur /= nFreq;\n"
1405 " return fDur;\n""}\n";
1406 
1408 "double GetDuration_new( \n"
1409 " int nNullDate, int nSettle, int nMat, double fCoup,\n"
1410 " double fYield, int nFreq, int nBase );\n";
1411 
1412 std::string GetDuration_new=
1413 "double GetDuration_new( \n"
1414 " int nNullDate, int nSettle, int nMat, double fCoup,\n"
1415 " double fYield, int nFreq, int nBase )\n"
1416 " {\n"
1417 " double fYearfrac = GetYearFrac(nNullDate,nSettle,nMat,nBase);\n"
1418 " double fNumOfCoups = lcl_Getcoupnum_new(nNullDate,nSettle,nMat,"
1419 "nFreq,nBase);\n"
1420 " double fDur = 0.0;\n"
1421 " fCoup = fCoup * 100.0 * pow(nFreq, -1.0);\n"
1422 " fYield = fYield * pow(nFreq, -1.0);\n"
1423 " fYield += 1.0;\n"
1424 " double nDiff = fYearfrac * nFreq - fNumOfCoups;\n"
1425 " int t;\n"
1426 " double tmp0 = 0, tmp1 = 0, tmp2 = 0;\n"
1427 " for( t = 1 ; t < fNumOfCoups ; t++ ){\n"
1428 " tmp0 = (t + nDiff) * ( fCoup ) ;\n"
1429 " tmp1 = pow( fYield, t + nDiff ) ;\n"
1430 " tmp2 = tmp0 * pow(tmp1, -1);\n"
1431 " fDur += tmp2;\n"
1432 " }\n"
1433 " fDur += (fNumOfCoups + nDiff) * (fCoup + 100.0) * pow(pow(fYield,"
1434 " fNumOfCoups + nDiff ),-1);\n"
1435 " double p = 0.0;\n"
1436 " for( t = 1 ; t < fNumOfCoups ; t++ ){\n"
1437 " tmp0 = pow( fYield, t + nDiff );\n"
1438 " p += fCoup * pow(tmp0, -1);}\n"
1439 " p += (fCoup + 100.0) * pow(pow(fYield, fNumOfCoups + nDiff), -1);\n"
1440 " fDur = fDur * pow(p, -1.0);\n"
1441 " fDur = fDur * pow(nFreq, -1.0);\n"
1442 " return fDur;\n"
1443 " }\n";
1444 
1445 std::string ScGetDDBDecl=
1446 "double ScGetDDB(double fCost, double fSalvage, double fLife, double fPeriod,"
1447 "double fFactor);\n";
1448 
1449 std::string ScGetDDB=
1450 "double ScGetDDB(double fCost, double fSalvage, double fLife, double fPeriod,"
1451 "double fFactor)\n"
1452 "{\n"
1453 " double fDdb, fRate, fOldValue, fNewValue;\n"
1454 " fRate = fFactor / fLife;\n"
1455 " if (fRate >= 1.0)\n"
1456 " {\n"
1457 " fRate = 1.0;\n"
1458 " if (fPeriod == 1.0)\n"
1459 " fOldValue = fCost;\n"
1460 " else\n"
1461 " fOldValue = 0.0;\n"
1462 " }\n"
1463 " else\n"
1464 " fOldValue = fCost * pow(1.0 - fRate, fPeriod - 1.0);\n"
1465 " fNewValue = fCost * pow(1.0 - fRate, fPeriod);\n"
1466 
1467 " if (fNewValue < fSalvage)\n"
1468 " fDdb = fOldValue - fSalvage;\n"
1469 " else\n"
1470 " fDdb = fOldValue - fNewValue;\n"
1471 " if (fDdb < 0.0)\n"
1472 " fDdb = 0.0;\n"
1473 " return fDdb;\n"
1474 "}\n";
1475 
1476 std::string DblMinDecl=
1477 "inline double DblMin( double a, double b );\n";
1478 
1479 std::string DblMin=
1480 "inline double DblMin( double a, double b )\n"
1481 "{\n"
1482 " return (a < b) ? a : b;\n"
1483 "}\n";
1484 
1485 std::string ScInterVDBDecl=
1486 "double ScInterVDB(double fCost, double fSalvage, double fLife, double fLife1,"
1487 "double fPeriod, double fFactor);\n";
1488 
1489 std::string ScInterVDB=
1490 "double ScInterVDB(double fCost, double fSalvage, double fLife, double fLife1,"
1491 "double fPeriod, double fFactor)\n"
1492 "{\n"
1493 " double fVdb=0;\n"
1494 " double fIntEnd = ceil(fPeriod);\n"
1495 " int nLoopEnd = fIntEnd;\n"
1496 
1497 " double fTerm, fSln;\n"
1498 " double fSalvageValue = fCost - fSalvage;\n"
1499 " int nNowSln = 0;\n"
1500 " double fDdb;\n"
1501 " int i;\n"
1502 " fSln=0;\n"
1503 " for ( i = 1; i <= nLoopEnd; i++)\n"
1504 " {\n"
1505 " if(!nNowSln)\n"
1506 " {\n"
1507 " fDdb = ScGetDDB(fCost, fSalvage, fLife, (double) i, fFactor);\n"
1508 " fSln = fSalvageValue/ (fLife1 - (double) (i-1));\n"
1509 " if (fSln > fDdb)\n"
1510 " {\n"
1511 " fTerm = fSln;\n"
1512 " nNowSln = 1;\n"
1513 " }\n"
1514 " else\n"
1515 " {\n"
1516 " fTerm = fDdb;\n"
1517 " fSalvageValue =fSalvageValue- fDdb;\n"
1518 " }\n"
1519 " }\n"
1520 " else\n"
1521 " {\n"
1522 " fTerm = fSln;\n"
1523 " }\n"
1524 
1525 " if ( i == nLoopEnd)\n"
1526 " fTerm *= ( fPeriod + 1.0 - fIntEnd );\n"
1527 
1528 " fVdb += fTerm;\n"
1529 " }\n"
1530 " return fVdb;\n"
1531 "}\n";
1532 
1533 std::string VDBImplementDecl=
1534 "double VDBImplement(double fCost, double fSalvage, double fLife, double fStart"
1535 ", double fEnd, double fFactor, bool bNoSwitch);\n";
1536 
1537 std::string VDBImplement=
1538 "double VDBImplement(double fCost, double fSalvage, double fLife, double fStart"
1539 ", double fEnd, double fFactor, bool bNoSwitch)\n"
1540 "{\n"
1541 " double result=0;\n"
1542 " double fIntStart = floor(fStart);\n"
1543 " double fIntEnd = ceil(fEnd);\n"
1544 " int nLoopStart = (int) fIntStart;\n"
1545 " int nLoopEnd = (int) fIntEnd;\n"
1546 " if (bNoSwitch)\n"
1547 " {\n"
1548 " for (int i = nLoopStart + 1; i <= nLoopEnd; i++)\n"
1549 " {\n"
1550 " double fTerm = ScGetDDB(fCost, fSalvage, fLife, (double) i, fFactor"
1551 ");\n"
1552 " if ( i == nLoopStart+1 )\n"
1553 " fTerm *= ( DblMin( fEnd, fIntStart + 1.0 ) - fStart );\n"
1554 " else if ( i == nLoopEnd )\n"
1555 " fTerm *= ( fEnd + 1.0 - fIntEnd );\n"
1556 " result += fTerm;\n"
1557 " }\n"
1558 " }\n"
1559 " else\n"
1560 " {\n"
1561 " double fLife1=fLife;\n"
1562 " if(!isequal(fStart,floor(fStart)))\n"
1563 " {\n"
1564 " if(fFactor>1)\n"
1565 " {\n"
1566 " if(fStart>fLife/2 || isequal(fStart,fLife/2))\n"
1567 " {\n"
1568 " double fPart=fStart-fLife/2;\n"
1569 " fStart=fLife/2;\n"
1570 " fEnd-=fPart;\n"
1571 " fLife1+=1;\n"
1572 " }\n"
1573 " }\n"
1574 " }\n"
1575 " fCost-=ScInterVDB(fCost, fSalvage, fLife, fLife1, fStart, fFactor);\n"
1576 " result=ScInterVDB(fCost, fSalvage, fLife, fLife-fStart, fEnd-fStart,"
1577 "fFactor);\n"
1578 " }\n"
1579 " return result;\n"
1580 "}\n";
1581 
1582 std::string GetOddlpriceDecl=
1583 "double GetOddlprice( int nNullDate, int nSettle, int nMat, int nLastCoup,\n"
1584 " double fRate, double fYield, double fRedemp, int nFreq, int nBase );\n";
1585 
1586 std::string GetOddlprice=
1587 "double GetOddlprice( int nNullDate, int nSettle, int nMat, int nLastCoup,\n"
1588 " double fRate, double fYield, double fRedemp, int nFreq, int nBase )\n"
1589 "{\n"
1590 " double fFreq = nFreq ;\n"
1591 " double fDCi = GetYearFrac( nNullDate, nLastCoup,"
1592 "nMat, nBase ) * fFreq;\n"
1593 " double fDSCi = GetYearFrac( nNullDate, nSettle,"
1594 "nMat, nBase ) * fFreq;\n"
1595 " double fAi = GetYearFrac( nNullDate, nLastCoup,"
1596 "nSettle, nBase ) * fFreq;\n"
1597 " double p = fRedemp + fDCi * 100.0 * fRate / fFreq;\n"
1598 " p /= fDSCi * fYield / fFreq + 1.0;\n"
1599 " p -= fAi * 100.0 * fRate / fFreq;\n"
1600 " return p;\n"
1601 "}\n";
1602 
1603 std::string GetOddlyieldDecl=
1604 "double GetOddlyield( int nNullDate, int nSettle, int nMat, int nLastCoup,\n"
1605 " double fRate, double fPrice, double fRedemp, int nFreq, int nBase );\n";
1606 
1607 std::string GetOddlyield=
1608 "double GetOddlyield( int nNullDate, int nSettle, int nMat, int nLastCoup,\n"
1609 " double fRate, double fPrice, double fRedemp, int nFreq, int nBase ) \n"
1610 "{\n"
1611 " double fFreq = nFreq ;\n"
1612 " double fDCi= GetYearFrac( nNullDate, nLastCoup, nMat, nBase ) * fFreq;\n"
1613 " double fDSCi= GetYearFrac( nNullDate, nSettle, nMat, nBase ) * fFreq;\n"
1614 " double fAi= GetYearFrac( nNullDate, nLastCoup, nSettle, nBase )*fFreq;\n"
1615 " double y = fRedemp + fDCi * 100.0 * fRate / fFreq;\n"
1616 " y /= fPrice + fAi * 100.0 * fRate / fFreq;\n"
1617 " y -= 1.0;\n"
1618 " y *= fFreq / fDSCi;\n"
1619 " return y;\n"
1620 "}\n";
1621 
1623 "double GetYearFrac_new( int nNullDate, int nStartDate, int nEndDate,"
1624 "int nMode );\n";
1625 
1626 std::string GetYearFrac_new=
1627 "double GetYearFrac_new( int nNullDate, int nStartDate, int nEndDate,"
1628 "int nMode ) \n"
1629 "{\n"
1630 " if( nStartDate == nEndDate )\n"
1631 " return 0.0; \n"
1632 " if( nStartDate > nEndDate )\n"
1633 " {\n"
1634 " int n = nEndDate;\n"
1635 " nEndDate = nStartDate;\n"
1636 " nStartDate = n;\n"
1637 " }\n"
1638 " int nDate1 = nStartDate + nNullDate;\n"
1639 " int nDate2 = nEndDate + nNullDate;\n"
1640 " int nDay1, nDay2;\n"
1641 " int nMonth1, nMonth2;\n"
1642 " int nYear1, nYear2;\n"
1643 " DaysToDate_new( nDate1, &nDay1, &nMonth1, &nYear1 );\n"
1644 " DaysToDate_new( nDate2, &nDay2, &nMonth2, &nYear2 );\n"
1645 " int nDayDiff;\n"
1646 " switch( nMode )\n"
1647 " {\n"
1648 " case 0: \n"
1649 " if ( nDay1 == 31 )\n"
1650 " {\n"
1651 " nDay1--;\n"
1652 " }\n"
1653 " if ( nDay1 == 30 && nDay2 == 31 )\n"
1654 " {\n"
1655 " nDay2--;\n"
1656 " }\n"
1657 " else\n"
1658 " {\n"
1659 " if ( nMonth1 == 2 && nDay1 == "
1660 "( IsLeapYear( nYear1 ) ? 29 : 28 ) )\n"
1661 " {\n"
1662 " nDay1 = 30;\n"
1663 " if ( nMonth2 == 2 && nDay2 == "
1664 "( IsLeapYear( nYear2 ) ? 29 : 28 ) )\n"
1665 " {\n"
1666 " nDay2 = 30;\n"
1667 " }\n"
1668 " }\n"
1669 " }\n"
1670 " nDayDiff = ( nYear2 - nYear1 ) * 360 + "
1671 "( nMonth2 - nMonth1 ) * 30 + ( nDay2 - nDay1 );\n"
1672 " break;\n"
1673 " case 1: \n"
1674 " case 2: \n"
1675 " case 3: \n"
1676 " nDayDiff = nDate2 - nDate1;\n"
1677 " break;\n"
1678 " case 4: \n"
1679 " if ( nDay1 == 31 )\n"
1680 " {\n"
1681 " nDay1--;\n"
1682 " }\n"
1683 " if ( nDay2 == 31 )\n"
1684 " {\n"
1685 " nDay2--;\n"
1686 " }\n"
1687 " nDayDiff = ( nYear2 - nYear1 ) * 360 + "
1688 "( nMonth2 - nMonth1 ) * 30 + ( nDay2 - nDay1 );\n"
1689 " break;\n"
1690 " }\n"
1691 " double nDaysInYear;\n"
1692 " switch( nMode )\n"
1693 " {\n"
1694 " case 0: \n"
1695 " case 2: \n"
1696 " case 4: \n"
1697 " nDaysInYear = 360;\n"
1698 " break;\n"
1699 " case 1: \n"
1700 " {\n"
1701 " bool isYearDifferent = ( nYear1 != nYear2 );\n"
1702 " if ( isYearDifferent &&\n"
1703 " ( ( nYear2 != nYear1 + 1 ) ||\n"
1704 " ( nMonth1 < nMonth2 ) ||\n"
1705 " ( nMonth1 == nMonth2 && nDay1 < nDay2 ) ) )\n"
1706 " {\n"
1707 " int nDayCount = 0;\n"
1708 " for ( int i = nYear1; i <= nYear2; i++ )\n"
1709 " nDayCount += ( IsLeapYear( i ) ? 366 : 365 );\n"
1710 " nDaysInYear = ( double ) nDayCount / "
1711 "( double ) ( nYear2 - nYear1 + 1 );\n"
1712 " }\n"
1713 " else\n"
1714 " {\n"
1715 " if ( isYearDifferent && IsLeapYear( nYear1 ) )\n"
1716 " {\n"
1717 " nDaysInYear = 366;\n"
1718 " }\n"
1719 " else\n"
1720 " {\n"
1721 " if ( ( IsLeapYear( nYear1 ) && nMonth1 <= 2 "
1722 "&& nDay1 <= 29 ) ||\n"
1723 " ( IsLeapYear( nYear2 ) && ( nMonth2 > 3 || "
1724 "( nMonth2 == 2 && nDay1 == 29 ) ) ) )\n"
1725 " {\n"
1726 " nDaysInYear = 366;\n"
1727 " }\n"
1728 " else\n"
1729 " {\n"
1730 " nDaysInYear = 365;\n"
1731 " for ( int i = nYear1; i <= nYear2; i++ )\n"
1732 " {\n"
1733 " if ( IsLeapYear( i ) )\n"
1734 " {\n"
1735 " nDaysInYear = 366;\n"
1736 " break;\n"
1737 " }\n"
1738 " }\n"
1739 " }\n"
1740 " }\n"
1741 " }\n"
1742 " }\n"
1743 " break;\n"
1744 " case 3: \n"
1745 " nDaysInYear = 365;\n"
1746 " break;\n"
1747 " }\n"
1748 " return (double)( nDayDiff ) / (nDaysInYear);\n"
1749 "}\n";
1750 
1751 std::string DaysToDate_newDecl =
1752 "void DaysToDate_new( int nDays, int *rDay, int* rMonth, int* rYear );\n";
1753 
1754 std::string DaysToDate_new =
1755 "void DaysToDate_new( int nDays, int *rDay, int* rMonth, int* rYear )\n"
1756 "{\n"
1757 " int nTempDays;\n"
1758 " int i = 0;\n"
1759 " bool bCalc;\n"
1760 " do\n"
1761 " {\n"
1762 " nTempDays = nDays;\n"
1763 " *rYear = (int)((nTempDays / 365) - i);\n"
1764 " nTempDays -= ((int) *rYear -1) * 365;\n"
1765 " nTempDays -= ((*rYear -1) / 4) - ((*rYear -1) / 100) + ((*rYear -1)"
1766 " / 400);\n"
1767 " bCalc = false;\n"
1768 " if ( nTempDays < 1 )\n"
1769 " {\n"
1770 " i++;\n"
1771 " bCalc = true;\n"
1772 " }\n"
1773 " else\n"
1774 " {\n"
1775 " if ( nTempDays > 365 )\n"
1776 " {\n"
1777 " if ( (nTempDays != 366) || !IsLeapYear( *rYear ) )\n"
1778 " {\n"
1779 " i--;\n"
1780 " bCalc = true;\n"
1781 " }\n"
1782 " }\n"
1783 " }\n"
1784 " }\n"
1785 " while ( bCalc );\n"
1786 " if(nTempDays != 0){\n"
1787 " for (*rMonth = 1; (int)nTempDays > DaysInMonth( *rMonth, *rYear );"
1788 " *rMonth += 1)\n"
1789 " {\n"
1790 " nTempDays -= DaysInMonth( *rMonth, *rYear ); \n"
1791 " }\n"
1792 " *rDay = (int)nTempDays;\n"
1793 " }\n"
1794 "}\n";
1795 
1797 "void DaysToDate( int nDays, int *rDay, int* rMonth, int* rYear );\n";
1798 
1800 "void DaysToDate( int nDays, int *rDay, int* rMonth, int* rYear )\n"
1801 "{\n"
1802 " int nTempDays;\n"
1803 " int i = 0;\n"
1804 " bool bCalc;\n"
1805 " do\n"
1806 " {\n"
1807 " nTempDays = nDays;\n"
1808 " *rYear = (int)((nTempDays / 365) - i);\n"
1809 " nTempDays -= ((int) *rYear -1) * 365;\n"
1810 " nTempDays -= ((*rYear -1) / 4) - ((*rYear -1) / 100) + ((*rYear -1)"
1811 " / 400);\n"
1812 " bCalc = false;\n"
1813 " if ( nTempDays < 1 )\n"
1814 " {\n"
1815 " i++;\n"
1816 " bCalc = true;\n"
1817 " }\n"
1818 " else\n"
1819 " {\n"
1820 " if ( nTempDays > 365 )\n"
1821 " {\n"
1822 " if ( (nTempDays != 366) || !IsLeapYear( *rYear ) )\n"
1823 " {\n"
1824 " i--;\n"
1825 " bCalc = true;\n"
1826 " }\n"
1827 " }\n"
1828 " }\n"
1829 " }\n"
1830 " while ( bCalc );\n"
1831 " barrier(CLK_LOCAL_MEM_FENCE);\n"
1832 " if(nTempDays != 0){\n"
1833 " for (*rMonth = 1; (int)nTempDays > DaysInMonth( *rMonth, *rYear );"
1834 " *rMonth += 1)\n"
1835 " {\n"
1836 " nTempDays -= DaysInMonth( *rMonth, *rYear ); \n"
1837 " }\n"
1838 " *rDay = (int)nTempDays;\n"
1839 " }\n"
1840 "}\n";
1841 
1843 "double GetYearDiff_new( int nNullDate, int nStartDate, int nEndDate,"
1844 "int nMode);\n";
1845 
1846 std::string GetYearDiff_new=
1847 "double GetYearDiff_new( int nNullDate, int nStartDate, int nEndDate,"
1848 "int nMode )\n"
1849 "{\n"
1850 " int nDays1stYear;\n"
1851 " int nTotalDays = GetDiffDate_new( nNullDate, nStartDate, nEndDate,"
1852 "nMode, &"
1853 "nDays1stYear );\n"
1854 " return (double)(nTotalDays)* pow((double)nDays1stYear,-1);\n"
1855 "}\n";
1856 
1858 "int GetDiffDate_new( int nNullDate, int nStartDate, int nEndDate, int nMode,"
1859 " int* pOptDaysIn1stYear );\n";
1860 
1861 std::string GetDiffDate_new=
1862 "int GetDiffDate_new( int nNullDate, int nStartDate, int nEndDate, int nMode,"
1863 " int* pOptDaysIn1stYear )\n"
1864 "{\n"
1865 " bool bNeg = nStartDate > nEndDate;\n"
1866 " if( bNeg )\n"
1867 " {\n"
1868 " int n = nEndDate;\n"
1869 " nEndDate = nStartDate;\n"
1870 " nStartDate = n;\n"
1871 " }\n"
1872 " int nRet;\n"
1873 " switch( nMode )\n"
1874 " {\n"
1875 " case 0: \n"
1876 " case 4: \n"
1877 " {\n"
1878 " int nD1, nM1, nY1, nD2, nM2, nY2;\n"
1879 " nStartDate += nNullDate;\n"
1880 " nEndDate += nNullDate;\n"
1881 " DaysToDate_new( nStartDate, &nD1, &nM1, &nY1 );\n"
1882 " DaysToDate_new( nEndDate, &nD2, &nM2, &nY2 );\n"
1883 " bool bLeap = IsLeapYear( nY1 );\n"
1884 " int nDays, nMonths;\n"
1885 " nMonths = nM2 - nM1;\n"
1886 " nDays = nD2 - nD1;\n"
1887 " nMonths += ( nY2 - nY1 ) * 12;\n"
1888 " nRet = nMonths * 30 + nDays;\n"
1889 " if( nMode == 0 && nM1 == 2 && nM2 != 2 && nY1 == nY2 )\n"
1890 " nRet -= bLeap? 1 : 2;\n"
1891 " if( pOptDaysIn1stYear )\n"
1892 " *pOptDaysIn1stYear = 360;\n"
1893 " }\n"
1894 " break;\n"
1895 " case 1: \n"
1896 " if( pOptDaysIn1stYear )\n"
1897 " {\n"
1898 " int nD, nM, nY;\n"
1899 " DaysToDate_new( nStartDate + nNullDate, &nD, &nM, &nY );\n"
1900 " *pOptDaysIn1stYear = IsLeapYear( nY )? 366 : 365;\n"
1901 " }\n"
1902 " nRet = nEndDate - nStartDate;\n"
1903 " break;\n"
1904 " case 2: \n"
1905 " nRet = nEndDate - nStartDate;\n"
1906 " if( pOptDaysIn1stYear )\n"
1907 " *pOptDaysIn1stYear = 360;\n"
1908 " break;\n"
1909 " case 3: \n"
1910 " nRet = nEndDate - nStartDate;\n"
1911 " if( pOptDaysIn1stYear )\n"
1912 " *pOptDaysIn1stYear = 365;\n"
1913 " break;\n"
1914 " }\n"
1915 " return bNeg? -nRet : nRet;\n"
1916 "}\n";
1917 
1918 #endif
1919 
1920 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::string ScInterVDBDecl
std::string DaysToDate_new
std::string GetOddlpriceDecl
std::string GetNullDate_new
std::string getPrice_new
std::string DateToDaysDecl
std::string coupdaybs
std::string GetYearFrac_newDecl
std::string coupdaysnc_new
std::string GetYearDiffDecl
std::string getDiffDecl
std::string GetFV_new
std::string GetDiffDate360_
std::string GetDaysInYear
std::string GetDiffDate360
std::string coupdaysnc
std::string ScaDateDecl
std::string DaysInMonth_new
std::string IsLeapYearDecl
std::string getPrice_new_Decl
std::string GetDiffDate360_Decl
std::string GetDiffDate360Decl
std::string RoundDecl
std::string GetYieldmatDecl
std::string GetNullDate
std::string GetYearDiff_newDecl
std::string Round
std::string SCdEpsilonDecl
std::string lcl_GetCouppcd
std::string lcl_Getcoupdaybs
std::string setDay
std::string lcl_GetCoupncdDecl
std::string coupdaysnc_newDecl
std::string ScaDate2
std::string DblMinDecl
std::string addMonths
std::string DaysToDate_LocalBarrierDecl
std::string IsLeapYear
std::string GetDaysInYearsDecl
std::string coupnum
std::string coupdays_new
std::string lcl_GetcoupdaybsDecl
std::string lcl_Getcoupnum_newDecl
std::string GetDiffDate
std::string coupnumDecl
std::string GetOddlyield
std::string DaysInMonth_newDecl
std::string DateToDays_newDecl
std::string lcl_GetcoupnumDecl
std::string lcl_GetCouppcdDecl
std::string ScaDate
std::string coupdaybs_new
std::string lcl_Getcoupnum
std::string lcl_Getcoupdaybs_newDecl
std::string GetDaysInYearDecl
std::string GetDaysInYears
std::string addMonthsDecl
std::string VDBImplement
std::string lcl_GetCoupncd
std::string DaysToDateDecl
std::string coupdaybs_newDecl
std::string GetPMT
std::string GetOddlprice
std::string getDaysInMonthRange
std::string getDiff
std::string DaysToDate_LocalBarrier
std::string lcl_Getcoupnum_new
std::string GetOddlyieldDecl
std::string coupdays
std::string VDBImplementDecl
std::string GetDuration_newDecl
std::string DblMin
std::string DaysInMonth
std::string GetDuration_new
std::string GetYearDiff
std::string lcl_Getcoupdays_newDecl
std::string DateToDays
std::string coupnum_newDecl
std::string GetFV
std::string getYield_
std::string coupdaysncDecl
std::string GetYearFrac
std::string ScGetDDBDecl
std::string DaysInMonthDecl
std::string GetPMT_newDecl
std::string coupdays_newDecl
std::string DaysToDate
std::string coupdaysDecl
std::string lcl_Getcoupdays_new
std::string GetYieldmat
std::string GetFV_newDecl
std::string getDaysInMonthRangeDecl
std::string getPrice_Decl
std::string GetNullDateDecl
std::string checklessthanDecl
std::string lcl_Getcoupdaybs_new
std::string ScGetDDB
std::string ScInterVDB
std::string getYield_Decl
std::string nCorrValDecl
std::string lcl_GetcoupdaysDecl
std::string coupdaybsDecl
std::string getPrice_
std::string checklessthan
std::string lcl_Getcoupdays
std::string GetDiffDateDecl
std::string GetDuration
std::string GetYearFrac_new
std::string DateToDays_new
std::string GetYearFracDecl
std::string GetPMT_new
std::string GetDiffDate_new
std::string GetPMTDecl
std::string GetFVDecl
std::string GetDurationDecl
std::string getDaysInYearRangeDecl
std::string ScaDate2Decl
std::string GetDiffDate_newDecl
std::string GetNullDate_newDecl
std::string getDaysInYearRange
std::string setDayDecl
std::string DaysToDate_newDecl
std::string GetYearDiff_new
std::string coupnum_new