LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/scaddins/source/analysis - financial.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 274 0.0 %
Date: 2013-07-09 Functions: 0 39 0.0 %
Legend: Lines: hit not hit

          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           0 : 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 )
      28             : {
      29           0 :     if( nDate > nFirstPer || fRate <= 0.0 || fRestVal > fCost )
      30           0 :         throw css::lang::IllegalArgumentException();
      31             : 
      32           0 :     double fRet = GetAmordegrc( GetNullDate( xOpt ), fCost, nDate, nFirstPer, fRestVal, fPer, fRate, getDateMode( xOpt, rOB ) );
      33           0 :     RETURN_FINITE( fRet );
      34             : }
      35             : 
      36             : 
      37           0 : 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 )
      40             : {
      41           0 :     if( nDate > nFirstPer || fRate <= 0.0 || fRestVal > fCost )
      42           0 :         throw css::lang::IllegalArgumentException();
      43             : 
      44           0 :     double fRet = GetAmorlinc( GetNullDate( xOpt ), fCost, nDate, nFirstPer, fRestVal, fPer, fRate, getDateMode( xOpt, rOB ) );
      45           0 :     RETURN_FINITE( fRet );
      46             : }
      47             : 
      48             : 
      49           0 : 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 )
      52             : {
      53           0 :     double      fVal = aAnyConv.getDouble( xOpt, rVal, 1000.0 );
      54             : 
      55           0 :     if( fRate <= 0.0 || fVal <= 0.0 || CHK_Freq || nIssue >= nSettle )
      56           0 :         throw css::lang::IllegalArgumentException();
      57             : 
      58           0 :     double fRet = fVal * fRate * GetYearDiff( GetNullDate( xOpt ), nIssue, nSettle, getDateMode( xOpt, rOB ) );
      59           0 :     RETURN_FINITE( fRet );
      60             : }
      61             : 
      62             : 
      63           0 : 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 )
      65             : {
      66           0 :     double      fVal = aAnyConv.getDouble( xOpt, rVal, 1000.0 );
      67             : 
      68           0 :     if( fRate <= 0.0 || fVal <= 0.0 || nIssue >= nSettle )
      69           0 :         throw css::lang::IllegalArgumentException();
      70             : 
      71           0 :     double fRet = fVal * fRate * GetYearDiff( GetNullDate( xOpt ), nIssue, nSettle, getDateMode( xOpt, rOB ) );
      72           0 :     RETURN_FINITE( fRet );
      73             : }
      74             : 
      75             : 
      76           0 : 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 )
      78             : {
      79           0 :     if( fInvest <= 0.0 || fDisc <= 0.0 )
      80           0 :         throw css::lang::IllegalArgumentException();
      81             : 
      82           0 :     double fRet = fInvest / ( 1.0 - ( fDisc * GetYearDiff( GetNullDate( xOpt ), nSettle, nMat, getDateMode( xOpt, rOB ) ) ) );
      83           0 :     RETURN_FINITE( fRet );
      84             : }
      85             : 
      86             : 
      87           0 : 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 )
      89             : {
      90           0 :     if( fPrice <= 0.0 || fRedemp <= 0.0 || nSettle >= nMat )
      91           0 :         throw css::lang::IllegalArgumentException();
      92           0 :     double fRet = ( 1.0 - fPrice / fRedemp ) / GetYearFrac( xOpt, nSettle, nMat, getDateMode( xOpt, rOB ) );
      93           0 :     RETURN_FINITE( fRet );
      94             : }
      95             : 
      96             : 
      97           0 : 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 )
     100             : {
     101           0 :     if( fCoup < 0.0 || fYield < 0.0 || CHK_Freq || nSettle >= nMat )
     102           0 :         throw css::lang::IllegalArgumentException();
     103             : 
     104           0 :     double fRet = GetDuration( GetNullDate( xOpt ),  nSettle, nMat, fCoup, fYield, nFreq, getDateMode( xOpt, rOB ) );
     105           0 :     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 )
     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 )
     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 )
     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           0 : 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 )
     200             : {
     201           0 :     if( fYield < 0.0 || fRate < 0.0 || fRedemp <= 0.0 || CHK_Freq || nSettle >= nMat )
     202           0 :         throw css::lang::IllegalArgumentException();
     203             : 
     204           0 :     double fRet = getPrice_( GetNullDate( xOpt ), nSettle, nMat, fRate, fYield, fRedemp, nFreq, getDateMode( xOpt, rOB ) );
     205           0 :     RETURN_FINITE( fRet );
     206             : }
     207             : 
     208             : 
     209           0 : 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 )
     211             : {
     212           0 :     if( fDisc <= 0.0 || fRedemp <= 0.0 || nSettle >= nMat )
     213           0 :         throw css::lang::IllegalArgumentException();
     214             : 
     215           0 :     double fRet = fRedemp * ( 1.0 - fDisc * GetYearDiff( GetNullDate( xOpt ), nSettle, nMat, getDateMode( xOpt, rOB ) ) );
     216           0 :     RETURN_FINITE( fRet );
     217             : }
     218             : 
     219             : 
     220           0 : 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 )
     223             : {
     224           0 :     if( fRate < 0.0 || fYield < 0.0 || nSettle >= nMat )
     225           0 :         throw css::lang::IllegalArgumentException();
     226             : 
     227           0 :     sal_Int32   nNullDate = GetNullDate( xOpt );
     228           0 :     sal_Int32   nBase = getDateMode( xOpt, rOB );
     229             : 
     230           0 :     double      fIssMat = GetYearFrac( nNullDate, nIssue, nMat, nBase );
     231           0 :     double      fIssSet = GetYearFrac( nNullDate, nIssue, nSettle, nBase );
     232           0 :     double      fSetMat = GetYearFrac( nNullDate, nSettle, nMat, nBase );
     233             : 
     234           0 :     double      fRet = 1.0 + fIssMat * fRate;
     235           0 :     fRet /= 1.0 + fSetMat * fYield;
     236           0 :     fRet -= fIssSet * fRate;
     237           0 :     fRet *= 100.0;
     238             : 
     239           0 :     RETURN_FINITE( fRet );
     240             : }
     241             : 
     242             : 
     243           0 : 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 )
     246             : {
     247           0 :     if( fCoup < 0.0 || fYield < 0.0 || CHK_Freq )
     248           0 :         throw css::lang::IllegalArgumentException();
     249             : 
     250           0 :     double      fRet = GetDuration( GetNullDate( xOpt ),  nSettle, nMat, fCoup, fYield, nFreq, getDateMode( xOpt, rOB ) );
     251           0 :     fRet /= 1.0 + ( fYield / double( nFreq ) );
     252           0 :     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 )
     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           0 : double SAL_CALL AnalysisAddIn::getDollarfr( double fDollarDec, sal_Int32 nFrac ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
     268             : {
     269           0 :     if( nFrac <= 0 )
     270           0 :         throw css::lang::IllegalArgumentException();
     271             : 
     272             :     double  fInt;
     273           0 :     double  fFrac = nFrac;
     274             : 
     275           0 :     double  fRet = modf( fDollarDec, &fInt );
     276             : 
     277           0 :     fRet *= fFrac;
     278             : 
     279           0 :     fRet *= pow( 10.0, -ceil( log10( fFrac ) ) );
     280             : 
     281           0 :     fRet += fInt;
     282             : 
     283           0 :     RETURN_FINITE( fRet );
     284             : }
     285             : 
     286             : 
     287           0 : double SAL_CALL AnalysisAddIn::getDollarde( double fDollarFrac, sal_Int32 nFrac ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
     288             : {
     289           0 :     if( nFrac <= 0 )
     290           0 :         throw css::lang::IllegalArgumentException();
     291             : 
     292             :     double  fInt;
     293           0 :     double  fFrac = nFrac;
     294             : 
     295           0 :     double  fRet = modf( fDollarFrac, &fInt );
     296             : 
     297           0 :     fRet /= fFrac;
     298             : 
     299           0 :     fRet *= pow( 10.0, ceil( log10( fFrac ) ) );
     300             : 
     301           0 :     fRet += fInt;
     302             : 
     303           0 :     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 )
     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 )
     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 )
     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           0 : 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 )
     348             : {
     349           0 :     nMat++;
     350             : 
     351           0 :     sal_Int32   nDiff = GetDiffDate360( xOpt, nSettle, nMat, sal_True );
     352             : 
     353           0 :     if( fDisc <= 0.0 || nSettle >= nMat || nDiff > 360 )
     354           0 :         throw css::lang::IllegalArgumentException();
     355             : 
     356           0 :     double fRet = ( 365 * fDisc ) / ( 360 - ( fDisc * double( nDiff ) ) );
     357           0 :     RETURN_FINITE( fRet );
     358             : }
     359             : 
     360             : 
     361           0 : 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 )
     363             : {
     364           0 :     if( fDisc <= 0.0 || nSettle > nMat )
     365           0 :         throw css::lang::IllegalArgumentException();
     366             : 
     367           0 :     nMat++;
     368             : 
     369           0 :     double  fFraction = GetYearFrac( xOpt, nSettle, nMat, 0 );  // method: USA 30/360
     370             : 
     371             :     double  fDummy;
     372           0 :     if( modf( fFraction, &fDummy ) == 0.0 )
     373           0 :         throw css::lang::IllegalArgumentException();
     374             : 
     375           0 :     double fRet = 100.0 * ( 1.0 - fDisc * fFraction );
     376           0 :     RETURN_FINITE( fRet );
     377             : }
     378             : 
     379             : 
     380           0 : 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 )
     382             : {
     383           0 :     sal_Int32   nDiff = GetDiffDate360( xOpt, nSettle, nMat, sal_True );
     384           0 :     nDiff++;
     385             : 
     386           0 :     if( fPrice <= 0.0 || nSettle >= nMat || nDiff > 360 )
     387           0 :         throw css::lang::IllegalArgumentException();
     388             : 
     389           0 :     double      fRet = 100.0;
     390           0 :     fRet /= fPrice;
     391           0 :     fRet--;
     392           0 :     fRet /= double( nDiff );
     393           0 :     fRet *= 360.0;
     394             : 
     395           0 :     RETURN_FINITE( fRet );
     396             : }
     397             : 
     398             : 
     399           0 : double SAL_CALL AnalysisAddIn::getOddfprice( const css::uno::Reference< css::beans::XPropertySet >& xOpt,
     400             :     sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue, sal_Int32 nFirstCoup,
     401             :     double fRate, double fYield, double fRedemp, sal_Int32 nFreq, const css::uno::Any& rOB ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
     402             : {
     403           0 :     if( fRate < 0.0 || fYield < 0.0 || CHK_Freq || nMat <= nFirstCoup || nFirstCoup <= nSettle || nSettle <= nIssue )
     404           0 :         throw css::lang::IllegalArgumentException();
     405             : 
     406           0 :     double fRet = GetOddfprice( GetNullDate( xOpt ), nSettle, nMat, nIssue, nFirstCoup, fRate, fYield, fRedemp, nFreq, getDateMode( xOpt, rOB ) );
     407           0 :     RETURN_FINITE( fRet );
     408             : }
     409             : 
     410             : 
     411           0 : double SAL_CALL AnalysisAddIn::getOddfyield( const css::uno::Reference< css::beans::XPropertySet >& xOpt,
     412             :     sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue, sal_Int32 nFirstCoup,
     413             :     double fRate, double fPrice, double fRedemp, sal_Int32 nFreq, const css::uno::Any& rOB ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
     414             : {
     415           0 :     if( fRate < 0.0 || fPrice <= 0.0 || CHK_Freq || nMat <= nFirstCoup || nFirstCoup <= nSettle || nSettle <= nIssue )
     416           0 :         throw css::lang::IllegalArgumentException();
     417             : 
     418             :     double fRet = GetOddfyield( GetNullDate( xOpt ), nSettle, nMat, nIssue, nFirstCoup, fRate, fPrice, fRedemp, nFreq,
     419           0 :                         getDateMode( xOpt, rOB ) );
     420           0 :     RETURN_FINITE( fRet );
     421             : }
     422             : 
     423             : 
     424           0 : double SAL_CALL AnalysisAddIn::getOddlprice( const css::uno::Reference< css::beans::XPropertySet >& xOpt,
     425             :     sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nLastInterest,
     426             :     double fRate, double fYield, double fRedemp, sal_Int32 nFreq, const css::uno::Any& rOB ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
     427             : {
     428           0 :     if( fRate < 0.0 || fYield < 0.0 || CHK_Freq || nMat <= nSettle || nSettle <= nLastInterest )
     429           0 :         throw css::lang::IllegalArgumentException();
     430             : 
     431             :     double fRet = GetOddlprice( GetNullDate( xOpt ), nSettle, nMat, nLastInterest, fRate, fYield, fRedemp, nFreq,
     432           0 :                         getDateMode( xOpt, rOB ) );
     433           0 :     RETURN_FINITE( fRet );
     434             : }
     435             : 
     436             : 
     437           0 : double SAL_CALL AnalysisAddIn::getOddlyield( const css::uno::Reference< css::beans::XPropertySet >& xOpt,
     438             :     sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nLastInterest,
     439             :     double fRate, double fPrice, double fRedemp, sal_Int32 nFreq, const css::uno::Any& rOB ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
     440             : {
     441           0 :     if( fRate < 0.0 || fPrice <= 0.0 || CHK_Freq || nMat <= nSettle || nSettle <= nLastInterest )
     442           0 :         throw css::lang::IllegalArgumentException();
     443             : 
     444             :     double fRet = GetOddlyield( GetNullDate( xOpt ), nSettle, nMat, nLastInterest, fRate, fPrice, fRedemp, nFreq,
     445           0 :                         getDateMode( xOpt, rOB ) );
     446           0 :     RETURN_FINITE( fRet );
     447             : }
     448             : 
     449             : 
     450             : // ============================================================================
     451             : // XIRR helper functions
     452             : 
     453             : #define V_(i) (*rValues.Get(i))
     454             : #define D_(i) (*rDates.Get(i))
     455             : 
     456             : /** Calculates the resulting amount for the passed interest rate and the given XIRR parameters. */
     457           0 : static double lcl_sca_XirrResult( const ScaDoubleList& rValues, const ScaDoubleList& rDates, double fRate )
     458             : {
     459             :     /*  V_0 ... V_n = input values.
     460             :         D_0 ... D_n = input dates.
     461             :         R           = input interest rate.
     462             : 
     463             :         r   := R+1
     464             :         E_i := (D_i-D_0) / 365
     465             : 
     466             :                     n    V_i                n    V_i
     467             :         f(R)  =  SUM   -------  =  V_0 + SUM   ------- .
     468             :                    i=0  r^E_i              i=1  r^E_i
     469             :     */
     470           0 :     double D_0 = D_(0);
     471           0 :     double r = fRate + 1.0;
     472           0 :     double fResult = V_(0);
     473           0 :     for( sal_uInt32 i = 1, nCount = rValues.Count(); i < nCount; ++i )
     474           0 :         fResult += V_(i) / pow( r, (D_(i) - D_0) / 365.0 );
     475           0 :     return fResult;
     476             : }
     477             : 
     478             : /** Calculates the first derivation of lcl_sca_XirrResult(). */
     479           0 : static double lcl_sca_XirrResult_Deriv1( const ScaDoubleList& rValues, const ScaDoubleList& rDates, double fRate )
     480             : {
     481             :     /*  V_0 ... V_n = input values.
     482             :         D_0 ... D_n = input dates.
     483             :         R           = input interest rate.
     484             : 
     485             :         r   := R+1
     486             :         E_i := (D_i-D_0) / 365
     487             : 
     488             :                              n    V_i
     489             :         f'(R)  =  [ V_0 + SUM   ------- ]'
     490             :                             i=1  r^E_i
     491             : 
     492             :                          n           V_i                 n    E_i V_i
     493             :                =  0 + SUM   -E_i ----------- r'  =  - SUM   ----------- .
     494             :                         i=1       r^(E_i+1)             i=1  r^(E_i+1)
     495             :     */
     496           0 :     double D_0 = D_(0);
     497           0 :     double r = fRate + 1.0;
     498           0 :     double fResult = 0.0;
     499           0 :     for( sal_uInt32 i = 1, nCount = rValues.Count(); i < nCount; ++i )
     500             :     {
     501           0 :         double E_i = (D_(i) - D_0) / 365.0;
     502           0 :         fResult -= E_i * V_(i) / pow( r, E_i + 1.0 );
     503             :     }
     504           0 :     return fResult;
     505             : }
     506             : 
     507             : #undef V_
     508             : #undef D_
     509             : 
     510             : 
     511             : // ----------------------------------------------------------------------------
     512             : // XIRR calculation
     513             : 
     514           0 : double SAL_CALL AnalysisAddIn::getXirr(
     515             :     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 )
     516             : {
     517           0 :     ScaDoubleList aValues, aDates;
     518           0 :     aValues.Append( rValues );
     519           0 :     aDates.Append( rDates );
     520             : 
     521           0 :     if( (aValues.Count() < 2) || (aValues.Count() != aDates.Count()) )
     522           0 :         throw css::lang::IllegalArgumentException();
     523             : 
     524             :     // result interest rate, initialized with passed guessed rate, or 10%
     525           0 :     double fResultRate = aAnyConv.getDouble( xOpt, rGuessRate, 0.1 );
     526           0 :     if( fResultRate <= -1 )
     527           0 :         throw css::lang::IllegalArgumentException();
     528             : 
     529             :     // maximum epsilon for end of iteration
     530             :     static const double fMaxEps = 1e-10;
     531             :     // maximum number of iterations
     532             :     static const sal_Int32 nMaxIter = 50;
     533             : 
     534             :     // Newton's method - try to find a fResultRate, so that lcl_sca_XirrResult() returns 0.
     535             :     double fNewRate, fRateEps, fResultValue;
     536           0 :     sal_Int32 nIter = 0;
     537             :     bool bContLoop;
     538           0 :     do
     539             :     {
     540           0 :         fResultValue = lcl_sca_XirrResult( aValues, aDates, fResultRate );
     541           0 :         fNewRate = fResultRate - fResultValue / lcl_sca_XirrResult_Deriv1( aValues, aDates, fResultRate );
     542           0 :         fRateEps = fabs( fNewRate - fResultRate );
     543           0 :         fResultRate = fNewRate;
     544           0 :         bContLoop = (fRateEps > fMaxEps) && (fabs( fResultValue ) > fMaxEps);
     545             :     }
     546           0 :     while( bContLoop && (++nIter < nMaxIter) );
     547             : 
     548           0 :     if( bContLoop )
     549           0 :         throw css::lang::IllegalArgumentException();
     550           0 :     RETURN_FINITE( fResultRate );
     551             : }
     552             : 
     553             : 
     554             : // ============================================================================
     555             : 
     556           0 : double SAL_CALL AnalysisAddIn::getXnpv(
     557             :     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 )
     558             : {
     559           0 :     ScaDoubleList aValList;
     560           0 :     ScaDoubleList aDateList;
     561             : 
     562           0 :     aValList.Append( rValues );
     563           0 :     aDateList.Append( rDates );
     564             : 
     565           0 :     sal_Int32           nNum = aValList.Count();
     566             : 
     567           0 :     if( nNum != sal_Int32( aDateList.Count() ) || nNum < 2 )
     568           0 :         throw css::lang::IllegalArgumentException();
     569             : 
     570           0 :     double              fRet = 0.0;
     571           0 :     double              fNull = *aDateList.Get( 0 );
     572           0 :     fRate++;
     573             : 
     574           0 :     for( sal_Int32 i = 0 ; i < nNum ; i++ )
     575           0 :         fRet += *aValList.Get( i ) / ( pow( fRate, ( *aDateList.Get( i ) - fNull ) / 365.0 ) );
     576             : 
     577           0 :     RETURN_FINITE( fRet );
     578             : }
     579             : 
     580             : 
     581           0 : double SAL_CALL AnalysisAddIn::getIntrate( const css::uno::Reference< css::beans::XPropertySet >& xOpt,
     582             :     sal_Int32 nSettle, sal_Int32 nMat, double fInvest, double fRedemp, const css::uno::Any& rOB ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
     583             : {
     584           0 :     if( fInvest <= 0.0 || fRedemp <= 0.0 || nSettle >= nMat )
     585           0 :         throw css::lang::IllegalArgumentException();
     586             : 
     587           0 :     double fRet = ( ( fRedemp / fInvest ) - 1.0 ) / GetYearDiff( GetNullDate( xOpt ), nSettle, nMat, getDateMode( xOpt, rOB ) );
     588           0 :     RETURN_FINITE( fRet );
     589             : }
     590             : 
     591             : 
     592           0 : double SAL_CALL AnalysisAddIn::getCoupncd( const css::uno::Reference< css::beans::XPropertySet >& xOpt,
     593             :     sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, const css::uno::Any& rOB ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
     594             : {
     595           0 :     double fRet = GetCoupncd( GetNullDate( xOpt ), nSettle, nMat, nFreq, getDateMode( xOpt, rOB ) );
     596           0 :     RETURN_FINITE( fRet );
     597             : }
     598             : 
     599             : 
     600           0 : double SAL_CALL AnalysisAddIn::getCoupdays( const css::uno::Reference< css::beans::XPropertySet >& xOpt,
     601             :     sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, const css::uno::Any& rOB ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
     602             : {
     603           0 :     double fRet = GetCoupdays( GetNullDate( xOpt ), nSettle, nMat, nFreq, getDateMode( xOpt, rOB ) );
     604           0 :     RETURN_FINITE( fRet );
     605             : }
     606             : 
     607             : 
     608           0 : double SAL_CALL AnalysisAddIn::getCoupdaysnc( const css::uno::Reference< css::beans::XPropertySet >& xOpt,
     609             :     sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, const css::uno::Any& rOB ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
     610             : {
     611           0 :     double fRet = GetCoupdaysnc( GetNullDate( xOpt ), nSettle, nMat, nFreq, getDateMode( xOpt, rOB ) );
     612           0 :     RETURN_FINITE( fRet );
     613             : }
     614             : 
     615             : 
     616           0 : double SAL_CALL AnalysisAddIn::getCoupdaybs( const css::uno::Reference< css::beans::XPropertySet >& xOpt,
     617             :     sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, const css::uno::Any& rOB ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
     618             : {
     619           0 :     double fRet = GetCoupdaybs( GetNullDate( xOpt ), nSettle, nMat, nFreq, getDateMode( xOpt, rOB ) );
     620           0 :     RETURN_FINITE( fRet );
     621             : }
     622             : 
     623             : 
     624           0 : double SAL_CALL AnalysisAddIn::getCouppcd( const css::uno::Reference< css::beans::XPropertySet >& xOpt,
     625             :     sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, const css::uno::Any& rOB ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
     626             : {
     627           0 :     double fRet = GetCouppcd( GetNullDate( xOpt ), nSettle, nMat, nFreq, getDateMode( xOpt, rOB ) );
     628           0 :     RETURN_FINITE( fRet );
     629             : }
     630             : 
     631             : 
     632           0 : double SAL_CALL AnalysisAddIn::getCoupnum( const css::uno::Reference< css::beans::XPropertySet >& xOpt,
     633             :     sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq, const css::uno::Any& rOB ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
     634             : {
     635           0 :     double fRet = GetCoupnum( GetNullDate( xOpt ), nSettle, nMat, nFreq, getDateMode( xOpt, rOB ) );
     636           0 :     RETURN_FINITE( fRet );
     637             : }
     638             : 
     639             : 
     640           0 : double SAL_CALL AnalysisAddIn::getFvschedule( double fPrinc, const css::uno::Sequence< css::uno::Sequence< double > >& rSchedule ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
     641             : {
     642           0 :     ScaDoubleList aSchedList;
     643             : 
     644           0 :     aSchedList.Append( rSchedule );
     645             : 
     646           0 :     for( const double* p = aSchedList.First() ; p ; p = aSchedList.Next() )
     647           0 :         fPrinc *= 1.0 + *p;
     648             : 
     649           0 :     RETURN_FINITE( fPrinc );
     650             : }
     651             : 
     652             : 
     653             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10