1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "analysis.hxx"
25 #include "analysishelper.hxx"
26 #include <rtl/math.hxx>
27 
28 
29 
30 double SAL_CALL AnalysisAddIn::getAmordegrc( constREFXPS& xOpt,
31 	double fCost, sal_Int32 nDate, sal_Int32 nFirstPer, double fRestVal,
32 	double fPer, double fRate, const ANY& rOB ) THROWDEF_RTE_IAE
33 {
34 	if( nDate > nFirstPer || fRate <= 0.0 || fRestVal > fCost )
35 		THROW_IAE;
36 
37     double fRet = GetAmordegrc( GetNullDate( xOpt ), fCost, nDate, nFirstPer, fRestVal, fPer, fRate, getDateMode( xOpt, rOB ) );
38     RETURN_FINITE( fRet );
39 }
40 
41 
42 double SAL_CALL AnalysisAddIn::getAmorlinc( constREFXPS& xOpt,
43 	double fCost, sal_Int32 nDate, sal_Int32 nFirstPer, double fRestVal,
44 	double fPer, double fRate, const ANY& rOB ) THROWDEF_RTE_IAE
45 {
46 	if( nDate > nFirstPer || fRate <= 0.0 || fRestVal > fCost )
47 		THROW_IAE;
48 
49     double fRet = GetAmorlinc( GetNullDate( xOpt ), fCost, nDate, nFirstPer, fRestVal, fPer, fRate, getDateMode( xOpt, rOB ) );
50     RETURN_FINITE( fRet );
51 }
52 
53 
54 double SAL_CALL AnalysisAddIn::getAccrint( constREFXPS& xOpt,
55 	sal_Int32 nIssue, sal_Int32 /*nFirstInter*/, sal_Int32 nSettle, double fRate,
56 	const ANY &rVal, sal_Int32 nFreq, const ANY& rOB ) THROWDEF_RTE_IAE
57 {
58     double      fVal = aAnyConv.getDouble( xOpt, rVal, 1000.0 );
59 
60 	if( fRate <= 0.0 || fVal <= 0.0 || CHK_Freq || nIssue >= nSettle )
61 		THROW_IAE;
62 
63     double fRet = fVal * fRate * GetYearDiff( GetNullDate( xOpt ), nIssue, nSettle, getDateMode( xOpt, rOB ) );
64     RETURN_FINITE( fRet );
65 }
66 
67 
68 double SAL_CALL AnalysisAddIn::getAccrintm( constREFXPS& xOpt,
69 	sal_Int32 nIssue, sal_Int32 nSettle, double fRate, const ANY& rVal, const ANY& rOB ) THROWDEF_RTE_IAE
70 {
71     double      fVal = aAnyConv.getDouble( xOpt, rVal, 1000.0 );
72 
73 	if( fRate <= 0.0 || fVal <= 0.0 || nIssue >= nSettle )
74 		THROW_IAE;
75 
76     double fRet = fVal * fRate * GetYearDiff( GetNullDate( xOpt ), nIssue, nSettle, getDateMode( xOpt, rOB ) );
77     RETURN_FINITE( fRet );
78 }
79 
80 
81 double SAL_CALL AnalysisAddIn::getReceived( constREFXPS& xOpt,
82 	sal_Int32 nSettle, sal_Int32 nMat, double fInvest, double fDisc, const ANY& rOB ) THROWDEF_RTE_IAE
83 {
84 	if( fInvest <= 0.0 || fDisc <= 0.0 )
85 		THROW_IAE;
86 
87     double fRet = fInvest / ( 1.0 - ( fDisc * GetYearDiff( GetNullDate( xOpt ), nSettle, nMat, getDateMode( xOpt, rOB ) ) ) );
88     RETURN_FINITE( fRet );
89 }
90 
91 
92 double SAL_CALL AnalysisAddIn::getDisc( constREFXPS& xOpt,
93 	sal_Int32 nSettle, sal_Int32 nMat, double fPrice, double fRedemp, const ANY& rOB ) THROWDEF_RTE_IAE
94 {
95 	if( fPrice <= 0.0 || fRedemp <= 0.0 || nSettle >= nMat )
96 		THROW_IAE;
97     double fRet = ( 1.0 - fPrice / fRedemp ) / GetYearFrac( xOpt, nSettle, nMat, getDateMode( xOpt, rOB ) );
98     RETURN_FINITE( fRet );
99 }
100 
101 
102 double SAL_CALL AnalysisAddIn::getDuration( constREFXPS& xOpt,
103 	sal_Int32 nSettle, sal_Int32 nMat, double fCoup, double fYield, sal_Int32 nFreq, const ANY& rOB )
104 	THROWDEF_RTE_IAE
105 {
106 	if( fCoup < 0.0 || fYield < 0.0 || CHK_Freq || nSettle >= nMat )
107 		THROW_IAE;
108 
109     double fRet = GetDuration( GetNullDate( xOpt ),  nSettle, nMat, fCoup, fYield, nFreq, getDateMode( xOpt, rOB ) );
110     RETURN_FINITE( fRet );
111 }
112 
113 
114 double SAL_CALL AnalysisAddIn::getEffect( double fNominal, sal_Int32 nPeriods ) THROWDEF_RTE_IAE
115 {
116 	if( nPeriods < 1 || fNominal <= 0.0 )
117 		THROW_IAE;
118 
119 	double	fPeriods = nPeriods;
120 
121     double fRet = pow( 1.0 + fNominal / fPeriods, fPeriods ) - 1.0;
122     RETURN_FINITE( fRet );
123 }
124 
125 
126 double SAL_CALL AnalysisAddIn::getCumprinc( double fRate, sal_Int32 nNumPeriods, double fVal,
127 	sal_Int32 nStartPer, sal_Int32 nEndPer, sal_Int32 nPayType ) THROWDEF_RTE_IAE
128 {
129 	double fRmz, fKapZ;
130 
131 	if( nStartPer < 1 || nEndPer < nStartPer || fRate <= 0.0 || nEndPer > nNumPeriods  || nNumPeriods <= 0 ||
132 		fVal <= 0.0 || ( nPayType != 0 && nPayType != 1 ) )
133 		THROW_IAE;
134 
135 	fRmz = GetRmz( fRate, nNumPeriods, fVal, 0.0, nPayType );
136 
137 	fKapZ = 0.0;
138 
139     sal_uInt32  nStart = sal_uInt32( nStartPer );
140     sal_uInt32  nEnd = sal_uInt32( nEndPer );
141 
142     if( nStart == 1 )
143 	{
144 		if( nPayType <= 0 )
145 			fKapZ = fRmz + fVal * fRate;
146 		else
147 			fKapZ = fRmz;
148 
149         nStart++;
150 	}
151 
152     for( sal_uInt32 i = nStart ; i <= nEnd ; i++ )
153 	{
154 		if( nPayType > 0 )
155 			fKapZ += fRmz - ( GetZw( fRate, double( i - 2 ), fRmz, fVal, 1 ) - fRmz ) * fRate;
156 		else
157 			fKapZ += fRmz - GetZw( fRate, double( i - 1 ), fRmz, fVal, 0 ) * fRate;
158 	}
159 
160     RETURN_FINITE( fKapZ );
161 }
162 
163 
164 double SAL_CALL AnalysisAddIn::getCumipmt( double fRate, sal_Int32 nNumPeriods, double fVal,
165 	sal_Int32 nStartPer, sal_Int32 nEndPer, sal_Int32 nPayType ) THROWDEF_RTE_IAE
166 {
167 	double fRmz, fZinsZ;
168 
169 	if( nStartPer < 1 || nEndPer < nStartPer || fRate <= 0.0 || nEndPer > nNumPeriods  || nNumPeriods <= 0 ||
170 		fVal <= 0.0 || ( nPayType != 0 && nPayType != 1 ) )
171 		THROW_IAE;
172 
173 	fRmz = GetRmz( fRate, nNumPeriods, fVal, 0.0, nPayType );
174 
175 	fZinsZ = 0.0;
176 
177     sal_uInt32  nStart = sal_uInt32( nStartPer );
178     sal_uInt32  nEnd = sal_uInt32( nEndPer );
179 
180     if( nStart == 1 )
181 	{
182 		if( nPayType <= 0 )
183 			fZinsZ = -fVal;
184 
185         nStart++;
186 	}
187 
188     for( sal_uInt32 i = nStart ; i <= nEnd ; i++ )
189 	{
190 		if( nPayType > 0 )
191 			fZinsZ += GetZw( fRate, double( i - 2 ), fRmz, fVal, 1 ) - fRmz;
192 		else
193 			fZinsZ += GetZw( fRate, double( i - 1 ), fRmz, fVal, 0 );
194 	}
195 
196 	fZinsZ *= fRate;
197 
198     RETURN_FINITE( fZinsZ );
199 }
200 
201 
202 double SAL_CALL AnalysisAddIn::getPrice( constREFXPS& xOpt,
203 	sal_Int32 nSettle, sal_Int32 nMat, double fRate, double fYield, double fRedemp, sal_Int32 nFreq,
204 	const ANY& rOB ) THROWDEF_RTE_IAE
205 {
206 	if( fYield < 0.0 || fRate < 0.0 || fRedemp <= 0 || CHK_Freq || nSettle >= nMat )
207 		THROW_IAE;
208 
209     double fRet = getPrice_( GetNullDate( xOpt ), nSettle, nMat, fRate, fYield, fRedemp, nFreq, getDateMode( xOpt, rOB ) );
210     RETURN_FINITE( fRet );
211 }
212 
213 
214 double SAL_CALL AnalysisAddIn::getPricedisc( constREFXPS& xOpt,
215 	sal_Int32 nSettle, sal_Int32 nMat, double fDisc, double fRedemp, const ANY& rOB ) THROWDEF_RTE_IAE
216 {
217 	if( fDisc <= 0.0 || fRedemp <= 0 || nSettle >= nMat )
218 		THROW_IAE;
219 
220     double fRet = fRedemp * ( 1.0 - fDisc * GetYearDiff( GetNullDate( xOpt ), nSettle, nMat, getDateMode( xOpt, rOB ) ) );
221     RETURN_FINITE( fRet );
222 }
223 
224 
225 double SAL_CALL AnalysisAddIn::getPricemat( constREFXPS& xOpt,
226 	sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue, double fRate, double fYield, const ANY& rOB )
227 	THROWDEF_RTE_IAE
228 {
229 	if( fRate < 0.0 || fYield < 0.0 || nSettle >= nMat )
230 		THROW_IAE;
231 
232 	sal_Int32	nNullDate = GetNullDate( xOpt );
233     sal_Int32   nBase = getDateMode( xOpt, rOB );
234 
235 	double		fIssMat = GetYearFrac( nNullDate, nIssue, nMat, nBase );
236 	double		fIssSet = GetYearFrac( nNullDate, nIssue, nSettle, nBase );
237 	double		fSetMat = GetYearFrac( nNullDate, nSettle, nMat, nBase );
238 
239 	double		fRet = 1.0 + fIssMat * fRate;
240 	fRet /= 1.0 + fSetMat * fYield;
241 	fRet -= fIssSet * fRate;
242 	fRet *= 100.0;
243 
244     RETURN_FINITE( fRet );
245 }
246 
247 
248 double SAL_CALL AnalysisAddIn::getMduration( constREFXPS& xOpt,
249 	sal_Int32 nSettle, sal_Int32 nMat, double fCoup, double fYield, sal_Int32 nFreq, const ANY& rOB )
250 	THROWDEF_RTE_IAE
251 {
252 	if( fCoup < 0.0 || fYield < 0.0 || CHK_Freq )
253 		THROW_IAE;
254 
255     double      fRet = GetDuration( GetNullDate( xOpt ),  nSettle, nMat, fCoup, fYield, nFreq, getDateMode( xOpt, rOB ) );
256 	fRet /= 1.0 + ( fYield / double( nFreq ) );
257     RETURN_FINITE( fRet );
258 }
259 
260 
261 double SAL_CALL AnalysisAddIn::getNominal( double fRate, sal_Int32 nPeriods ) THROWDEF_RTE_IAE
262 {
263 	if( fRate <= 0.0 || nPeriods < 0 )
264 		THROW_IAE;
265 
266 	double	fPeriods = nPeriods;
267     double fRet = ( pow( fRate + 1.0, 1.0 / fPeriods ) - 1.0 ) * fPeriods;
268     RETURN_FINITE( fRet );
269 }
270 
271 
272 double SAL_CALL AnalysisAddIn::getDollarfr( double fDollarDec, sal_Int32 nFrac ) THROWDEF_RTE_IAE
273 {
274 	if( nFrac <= 0 )
275 		THROW_IAE;
276 
277 	double	fInt;
278 	double	fFrac = nFrac;
279 
280 	double	fRet = modf( fDollarDec, &fInt );
281 
282 	fRet *= fFrac;
283 
284 	fRet *= pow( 10.0, -ceil( log10( fFrac ) ) );
285 
286 	fRet += fInt;
287 
288     RETURN_FINITE( fRet );
289 }
290 
291 
292 double SAL_CALL AnalysisAddIn::getDollarde( double fDollarFrac, sal_Int32 nFrac ) THROWDEF_RTE_IAE
293 {
294 	if( nFrac <= 0 )
295 		THROW_IAE;
296 
297 	double	fInt;
298 	double	fFrac = nFrac;
299 
300 	double	fRet = modf( fDollarFrac, &fInt );
301 
302 	fRet /= fFrac;
303 
304 	fRet *= pow( 10.0, ceil( log10( fFrac ) ) );
305 
306 	fRet += fInt;
307 
308     RETURN_FINITE( fRet );
309 }
310 
311 
312 double SAL_CALL AnalysisAddIn::getYield( constREFXPS& xOpt,
313 	sal_Int32 nSettle, sal_Int32 nMat, double fCoup, double fPrice, double fRedemp, sal_Int32 nFreq, const ANY& rOB )
314 	THROWDEF_RTE_IAE
315 {
316 	if( fCoup < 0.0 || fPrice <= 0.0 || fRedemp <= 0.0 || CHK_Freq || nSettle >= nMat )
317 		THROW_IAE;
318 
319     double fRet = getYield_( GetNullDate( xOpt ), nSettle, nMat, fCoup, fPrice, fRedemp, nFreq, getDateMode( xOpt, rOB ) );
320     RETURN_FINITE( fRet );
321 }
322 
323 
324 double SAL_CALL AnalysisAddIn::getYielddisc( constREFXPS& xOpt,
325 	sal_Int32 nSettle, sal_Int32 nMat, double fPrice, double fRedemp, const ANY& rOB ) THROWDEF_RTE_IAE
326 {
327 	if( fPrice <= 0.0 || fRedemp <= 0.0 || nSettle >= nMat )
328 		THROW_IAE;
329 
330 	sal_Int32	nNullDate = GetNullDate( xOpt );
331 
332 #if 0
333 	double		fRet = 1.0 - fPrice / fRedemp;
334     fRet /= GetYearFrac( nNullDate, nSettle, nMat, getDateMode( xOpt, rOB ) );
335     fRet /= 0.99795;  // don't know what this constant means in original
336 #endif
337 
338     double fRet = ( fRedemp / fPrice ) - 1.0;
339     fRet /= GetYearFrac( nNullDate, nSettle, nMat, getDateMode( xOpt, rOB ) );
340 
341     RETURN_FINITE( fRet );
342 }
343 
344 
345 double SAL_CALL AnalysisAddIn::getYieldmat( constREFXPS& xOpt,
346 	sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue, double fRate, double fPrice, const ANY& rOB )
347 	THROWDEF_RTE_IAE
348 {
349 	if( fRate < 0.0 || fRate <= 0.0 || nSettle >= nMat )
350 		THROW_IAE;
351 
352     double fRet = GetYieldmat( GetNullDate( xOpt ),  nSettle, nMat, nIssue, fRate, fPrice, getDateMode( xOpt, rOB ) );
353     RETURN_FINITE( fRet );
354 }
355 
356 
357 double SAL_CALL AnalysisAddIn::getTbilleq( constREFXPS& xOpt,
358 	sal_Int32 nSettle, sal_Int32 nMat, double fDisc ) THROWDEF_RTE_IAE
359 {
360 	nMat++;
361 
362 	sal_Int32	nDiff = GetDiffDate360( xOpt, nSettle, nMat, sal_True );
363 
364 	if( fDisc <= 0.0 || nSettle >= nMat || nDiff > 360 )
365 		THROW_IAE;
366 
367     double fRet = ( 365 * fDisc ) / ( 360 - ( fDisc * double( nDiff ) ) );
368     RETURN_FINITE( fRet );
369 }
370 
371 
372 double SAL_CALL AnalysisAddIn::getTbillprice( constREFXPS& xOpt,
373 	sal_Int32 nSettle, sal_Int32 nMat, double fDisc ) THROWDEF_RTE_IAE
374 {
375 	if( fDisc <= 0.0 || nSettle > nMat )
376 		THROW_IAE;
377 
378 	nMat++;
379 
380 	double	fFraction = GetYearFrac( xOpt, nSettle, nMat, 0 );	// method: USA 30/360
381 
382 	double	fDummy;
383 	if( modf( fFraction, &fDummy ) == 0.0 )
384 		THROW_IAE;
385 
386     double fRet = 100.0 * ( 1.0 - fDisc * fFraction );
387     RETURN_FINITE( fRet );
388 }
389 
390 
391 double SAL_CALL AnalysisAddIn::getTbillyield( constREFXPS& xOpt, sal_Int32 nSettle, sal_Int32 nMat, double fPrice )
392 	THROWDEF_RTE_IAE
393 {
394 	sal_Int32	nDiff = GetDiffDate360( xOpt, nSettle, nMat, sal_True );
395 	nDiff++;
396 
397 	if( fPrice <= 0.0 || nSettle >= nMat || nDiff > 360 )
398 		THROW_IAE;
399 
400 	double		fRet = 100.0;
401 	fRet /= fPrice;
402 	fRet--;
403 	fRet /= double( nDiff );
404 	fRet *= 360.0;
405 
406     RETURN_FINITE( fRet );
407 }
408 
409 
410 double SAL_CALL AnalysisAddIn::getOddfprice( constREFXPS& xOpt,
411 	sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue, sal_Int32 nFirstCoup,
412 	double fRate, double fYield, double fRedemp, sal_Int32 nFreq, const ANY& rOB ) THROWDEF_RTE_IAE
413 {
414 	if( fRate < 0 || fYield < 0 || CHK_Freq || nMat <= nFirstCoup || nFirstCoup <= nSettle || nSettle <= nIssue )
415 		THROW_IAE;
416 
417     double fRet = GetOddfprice( GetNullDate( xOpt ), nSettle, nMat, nIssue, nFirstCoup, fRate, fYield, fRedemp, nFreq, getDateMode( xOpt, rOB ) );
418     RETURN_FINITE( fRet );
419 }
420 
421 
422 double SAL_CALL AnalysisAddIn::getOddfyield( constREFXPS& xOpt,
423 	sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue, sal_Int32 nFirstCoup,
424 	double fRate, double fPrice, double fRedemp, sal_Int32 nFreq, const ANY& rOB ) THROWDEF_RTE_IAE
425 {
426 	if( fRate < 0 || fPrice <= 0 || CHK_Freq || nMat <= nFirstCoup || nFirstCoup <= nSettle || nSettle <= nIssue )
427 		THROW_IAE;
428 
429     double fRet = GetOddfyield( GetNullDate( xOpt ), nSettle, nMat, nIssue, nFirstCoup, fRate, fPrice, fRedemp, nFreq,
430                         getDateMode( xOpt, rOB ) );
431     RETURN_FINITE( fRet );
432 }
433 
434 
435 double SAL_CALL AnalysisAddIn::getOddlprice( constREFXPS& xOpt,
436 	sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nLastInterest,
437 	double fRate, double fYield, double fRedemp, sal_Int32 nFreq, const ANY& rOB ) THROWDEF_RTE_IAE
438 {
439 	if( fRate < 0 || fYield < 0 || CHK_Freq || nMat <= nSettle || nSettle <= nLastInterest )
440 		THROW_IAE;
441 
442     double fRet = GetOddlprice( GetNullDate( xOpt ), nSettle, nMat, nLastInterest, fRate, fYield, fRedemp, nFreq,
443                         getDateMode( xOpt, rOB ) );
444     RETURN_FINITE( fRet );
445 }
446 
447 
448 double SAL_CALL AnalysisAddIn::getOddlyield( constREFXPS& xOpt,
449 	sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nLastInterest,
450 	double fRate, double fPrice, double fRedemp, sal_Int32 nFreq, const ANY& rOB ) THROWDEF_RTE_IAE
451 {
452 	if( fRate < 0 || fPrice <= 0 || CHK_Freq || nMat <= nSettle || nSettle <= nLastInterest )
453 		THROW_IAE;
454 
455     double fRet = GetOddlyield( GetNullDate( xOpt ), nSettle, nMat, nLastInterest, fRate, fPrice, fRedemp, nFreq,
456                         getDateMode( xOpt, rOB ) );
457     RETURN_FINITE( fRet );
458 }
459 
460 
461 // ============================================================================
462 // XIRR helper functions
463 
464 #define V_(i) (*rValues.Get(i))
465 #define D_(i) (*rDates.Get(i))
466 
467 /** Calculates the resulting amount for the passed interest rate and the given XIRR parameters. */
468 double lcl_sca_XirrResult( const ScaDoubleList& rValues, const ScaDoubleList& rDates, double fRate )
469 {
470     /*  V_0 ... V_n = input values.
471         D_0 ... D_n = input dates.
472         R           = input interest rate.
473 
474         r   := R+1
475         E_i := (D_i-D_0) / 365
476 
477                     n    V_i                n    V_i
478         f(R)  =  SUM   -------  =  V_0 + SUM   ------- .
479                    i=0  r^E_i              i=1  r^E_i
480     */
481     double D_0 = D_(0);
482     double r = fRate + 1.0;
483     double fResult = V_(0);
484     for( sal_uInt32 i = 1, nCount = rValues.Count(); i < nCount; ++i )
485         fResult += V_(i) / pow( r, (D_(i) - D_0) / 365.0 );
486     return fResult;
487 }
488 
489 /** Calculates the first derivation of lcl_sca_XirrResult(). */
490 double lcl_sca_XirrResult_Deriv1( const ScaDoubleList& rValues, const ScaDoubleList& rDates, double fRate )
491 {
492     /*  V_0 ... V_n = input values.
493         D_0 ... D_n = input dates.
494         R           = input interest rate.
495 
496         r   := R+1
497         E_i := (D_i-D_0) / 365
498 
499                              n    V_i
500         f'(R)  =  [ V_0 + SUM   ------- ]'
501                             i=1  r^E_i
502 
503                          n           V_i                 n    E_i V_i
504                =  0 + SUM   -E_i ----------- r'  =  - SUM   ----------- .
505                         i=1       r^(E_i+1)             i=1  r^(E_i+1)
506     */
507     double D_0 = D_(0);
508     double r = fRate + 1.0;
509     double fResult = 0.0;
510     for( sal_uInt32 i = 1, nCount = rValues.Count(); i < nCount; ++i )
511     {
512         double E_i = (D_(i) - D_0) / 365.0;
513         fResult -= E_i * V_(i) / pow( r, E_i + 1.0 );
514     }
515     return fResult;
516 }
517 
518 #undef V_
519 #undef D_
520 
521 
522 // ----------------------------------------------------------------------------
523 // XIRR calculation
524 
525 double SAL_CALL AnalysisAddIn::getXirr(
526     constREFXPS& xOpt, const SEQSEQ( double )& rValues, const SEQSEQ( sal_Int32 )& rDates, const ANY& rGuessRate ) THROWDEF_RTE_IAE
527 {
528     ScaDoubleList aValues, aDates;
529     aValues.Append( rValues );
530     aDates.Append( rDates );
531 
532     if( (aValues.Count() < 2) || (aValues.Count() != aDates.Count()) )
533         THROW_IAE;
534 
535     // result interest rate, initialized with passed guessed rate, or 10%
536     double fResultRate = aAnyConv.getDouble( xOpt, rGuessRate, 0.1 );
537     if( fResultRate <= -1 )
538         THROW_IAE;
539 
540     // maximum epsilon for end of iteration
541     static const double fMaxEps = 1e-10;
542     // maximum number of iterations
543     static const sal_Int32 nMaxIter = 50;
544 
545     // Newton's method - try to find a fResultRate, so that lcl_sca_XirrResult() returns 0.
546     double fNewRate, fRateEps, fResultValue;
547     sal_Int32 nIter = 0;
548     bool bContLoop;
549     do
550     {
551         fResultValue = lcl_sca_XirrResult( aValues, aDates, fResultRate );
552         fNewRate = fResultRate - fResultValue / lcl_sca_XirrResult_Deriv1( aValues, aDates, fResultRate );
553         fRateEps = fabs( fNewRate - fResultRate );
554         fResultRate = fNewRate;
555         bContLoop = (fRateEps > fMaxEps) && (fabs( fResultValue ) > fMaxEps);
556     }
557     while( bContLoop && (++nIter < nMaxIter) );
558 
559     if( bContLoop )
560         THROW_IAE;
561     RETURN_FINITE( fResultRate );
562 }
563 
564 
565 // ============================================================================
566 
567 double SAL_CALL AnalysisAddIn::getXnpv(
568 	double fRate, const SEQSEQ( double )& rValues, const SEQSEQ( sal_Int32 )& rDates ) THROWDEF_RTE_IAE
569 {
570     ScaDoubleList aValList;
571     ScaDoubleList aDateList;
572 
573 	aValList.Append( rValues );
574 	aDateList.Append( rDates );
575 
576 	sal_Int32			nNum = aValList.Count();
577 
578 	if( nNum != sal_Int32( aDateList.Count() ) || nNum < 2 )
579 		THROW_IAE;
580 
581 	double				fRet = 0.0;
582 	double				fNull = *aDateList.Get( 0 );
583 	fRate++;
584 
585 	for( sal_Int32 i = 0 ; i < nNum ; i++ )
586 		fRet += *aValList.Get( i ) / ( pow( fRate, ( *aDateList.Get( i ) - fNull ) / 365.0 ) );
587 
588     RETURN_FINITE( fRet );
589 }
590 
591 
592 double SAL_CALL AnalysisAddIn::getIntrate( constREFXPS& xOpt,
593 	sal_Int32 nSettle, sal_Int32 nMat, double fInvest, double fRedemp, const ANY& rOB ) THROWDEF_RTE_IAE
594 {
595 	if( fInvest <= 0.0 || fRedemp <= 0.0 || nSettle >= nMat )
596 		THROW_IAE;
597 
598     double fRet = ( ( fRedemp / fInvest ) - 1.0 ) / GetYearDiff( GetNullDate( xOpt ), nSettle, nMat, getDateMode( xOpt, rOB ) );
599     RETURN_FINITE( fRet );
600 }
601 
602 
603 double SAL_CALL AnalysisAddIn::getCoupncd( constREFXPS& xOpt,
604 	sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, const ANY& rOB ) THROWDEF_RTE_IAE
605 {
606     double fRet = GetCoupncd( GetNullDate( xOpt ), nSettle, nMat, nFreq, getDateMode( xOpt, rOB ) );
607     RETURN_FINITE( fRet );
608 }
609 
610 
611 double SAL_CALL AnalysisAddIn::getCoupdays( constREFXPS& xOpt,
612 	sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, const ANY& rOB ) THROWDEF_RTE_IAE
613 {
614     double fRet = GetCoupdays( GetNullDate( xOpt ), nSettle, nMat, nFreq, getDateMode( xOpt, rOB ) );
615     RETURN_FINITE( fRet );
616 }
617 
618 
619 double SAL_CALL AnalysisAddIn::getCoupdaysnc( constREFXPS& xOpt,
620 	sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, const ANY& rOB ) THROWDEF_RTE_IAE
621 {
622     double fRet = GetCoupdaysnc( GetNullDate( xOpt ), nSettle, nMat, nFreq, getDateMode( xOpt, rOB ) );
623     RETURN_FINITE( fRet );
624 }
625 
626 
627 double SAL_CALL AnalysisAddIn::getCoupdaybs( constREFXPS& xOpt,
628 	sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, const ANY& rOB ) THROWDEF_RTE_IAE
629 {
630     double fRet = GetCoupdaybs( GetNullDate( xOpt ), nSettle, nMat, nFreq, getDateMode( xOpt, rOB ) );
631     RETURN_FINITE( fRet );
632 }
633 
634 
635 double SAL_CALL AnalysisAddIn::getCouppcd( constREFXPS& xOpt,
636 	sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, const ANY& rOB ) THROWDEF_RTE_IAE
637 {
638     double fRet = GetCouppcd( GetNullDate( xOpt ), nSettle, nMat, nFreq, getDateMode( xOpt, rOB ) );
639     RETURN_FINITE( fRet );
640 }
641 
642 
643 double SAL_CALL AnalysisAddIn::getCoupnum( constREFXPS& xOpt,
644 	sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, const ANY& rOB ) THROWDEF_RTE_IAE
645 {
646     double fRet = GetCoupnum( GetNullDate( xOpt ), nSettle, nMat, nFreq, getDateMode( xOpt, rOB ) );
647     RETURN_FINITE( fRet );
648 }
649 
650 
651 double SAL_CALL AnalysisAddIn::getFvschedule( double fPrinc, const SEQSEQ( double )& rSchedule ) THROWDEF_RTE_IAE
652 {
653     ScaDoubleList aSchedList;
654 
655 	aSchedList.Append( rSchedule );
656 
657 	for( const double* p = aSchedList.First() ; p ; p = aSchedList.Next() )
658 		fPrinc *= 1.0 + *p;
659 
660     RETURN_FINITE( fPrinc );
661 }
662 
663 
664