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