bump product version to 4.1.6.2
[LibreOffice.git] / scaddins / source / analysis / analysis.cxx
blob2362dba807a2fed0870a5e2a8c325b1505141bdd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "analysis.hxx"
22 #include <comphelper/processfactory.hxx>
23 #include <cppuhelper/factory.hxx>
24 #include <osl/diagnose.h>
25 #include <rtl/ustrbuf.hxx>
26 #include <rtl/math.hxx>
27 #include <sal/macros.h>
28 #include <string.h>
30 #include <tools/resmgr.hxx>
31 #include <tools/rcid.h>
32 #include "analysis.hrc"
33 #include "bessel.hxx"
35 #define ADDIN_SERVICE "com.sun.star.sheet.AddIn"
36 #define MY_SERVICE "com.sun.star.sheet.addin.Analysis"
37 #define MY_IMPLNAME "com.sun.star.sheet.addin.AnalysisImpl"
39 using namespace ::com::sun::star;
42 extern "C" SAL_DLLPUBLIC_EXPORT void* SAL_CALL analysis_component_getFactory(
43 const sal_Char* pImplName, void* pServiceManager, void* /*pRegistryKey*/ )
45 void* pRet = 0;
47 if( pServiceManager && OUString::createFromAscii( pImplName ) == AnalysisAddIn::getImplementationName_Static() )
49 uno::Reference< lang::XSingleServiceFactory > xFactory( cppu::createOneInstanceFactory(
50 reinterpret_cast< lang::XMultiServiceFactory* >( pServiceManager ),
51 AnalysisAddIn::getImplementationName_Static(),
52 AnalysisAddIn_CreateInstance,
53 AnalysisAddIn::getSupportedServiceNames_Static() ) );
55 if( xFactory.is() )
57 xFactory->acquire();
58 pRet = xFactory.get();
62 return pRet;
66 //------------------------------------------------------------------------
68 // "normal" service implementation
70 //------------------------------------------------------------------------
73 ResMgr& AnalysisAddIn::GetResMgr( void ) throw( uno::RuntimeException )
75 if( !pResMgr )
77 InitData(); // try to get resource manager
79 if( !pResMgr )
80 throw uno::RuntimeException();
83 return *pResMgr;
87 OUString AnalysisAddIn::GetDisplFuncStr( sal_uInt16 nFuncNum ) throw( uno::RuntimeException )
89 return String( AnalysisRscStrLoader( RID_ANALYSIS_FUNCTION_NAMES, nFuncNum, GetResMgr() ).GetString() );
93 class AnalysisResourcePublisher : public Resource
95 public:
96 AnalysisResourcePublisher( const AnalysisResId& rId ) : Resource( rId ) {}
97 sal_Bool IsAvailableRes( const ResId& rId ) const { return Resource::IsAvailableRes( rId ); }
98 void FreeResource() { Resource::FreeResource(); }
102 class AnalysisFuncRes : public Resource
104 public:
105 AnalysisFuncRes( ResId& rRes, ResMgr& rResMgr, sal_uInt16 nInd, OUString& rRet );
109 AnalysisFuncRes::AnalysisFuncRes( ResId& rRes, ResMgr& rResMgr, sal_uInt16 nInd, OUString& rRet ) : Resource( rRes )
111 rRet = String( AnalysisResId( nInd, rResMgr ) );
113 FreeResource();
117 OUString AnalysisAddIn::GetFuncDescrStr( sal_uInt16 nResId, sal_uInt16 nStrIndex ) throw( uno::RuntimeException )
119 OUString aRet;
120 AnalysisResourcePublisher aResPubl( AnalysisResId( RID_ANALYSIS_FUNCTION_DESCRIPTIONS, GetResMgr() ) );
121 AnalysisResId aRes( nResId, GetResMgr() );
122 aRes.SetRT( RSC_RESOURCE );
123 if( aResPubl.IsAvailableRes( aRes ) )
125 AnalysisFuncRes aSubRes( aRes, GetResMgr(), nStrIndex, aRet );
128 aResPubl.FreeResource();
130 return aRet;
134 void AnalysisAddIn::InitData( void )
136 if( pResMgr )
137 delete pResMgr;
139 OString aModName( "analysis" );
140 pResMgr = ResMgr::CreateResMgr( aModName.getStr(), LanguageTag( aFuncLoc) );
142 if( pFD )
143 delete pFD;
145 if( pResMgr )
146 pFD = new FuncDataList( *pResMgr );
147 else
148 pFD = NULL;
150 if( pDefLocales )
152 delete pDefLocales;
153 pDefLocales = NULL;
158 AnalysisAddIn::AnalysisAddIn( const uno::Reference< uno::XComponentContext >& xContext ) :
159 pDefLocales( NULL ),
160 pFD( NULL ),
161 pFactDoubles( NULL ),
162 pCDL( NULL ),
163 pResMgr( NULL ),
164 aAnyConv( xContext )
169 AnalysisAddIn::~AnalysisAddIn()
171 if( pFD )
172 delete pFD;
174 if( pFactDoubles )
175 delete[] pFactDoubles;
177 if( pCDL )
178 delete pCDL;
180 if( pDefLocales )
181 delete[] pDefLocales;
185 sal_Int32 AnalysisAddIn::getDateMode(
186 const uno::Reference< beans::XPropertySet >& xPropSet,
187 const uno::Any& rAny ) throw( uno::RuntimeException, lang::IllegalArgumentException )
189 sal_Int32 nMode = aAnyConv.getInt32( xPropSet, rAny, 0 );
190 if( (nMode < 0) || (nMode > 4) )
191 throw lang::IllegalArgumentException();
192 return nMode;
197 //-----------------------------------------------------------------------------
200 #define MAXFACTDOUBLE 300
202 double AnalysisAddIn::FactDouble( sal_Int32 nNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
204 if( nNum < 0 || nNum > MAXFACTDOUBLE )
205 throw lang::IllegalArgumentException();
207 if( !pFactDoubles )
209 pFactDoubles = new double[ MAXFACTDOUBLE + 1 ];
211 pFactDoubles[ 0 ] = 1.0; // by default
213 double fOdd = 1.0;
214 double fEven = 2.0;
216 pFactDoubles[ 1 ] = fOdd;
217 pFactDoubles[ 2 ] = fEven;
219 sal_Bool bOdd = sal_True;
221 for( sal_uInt16 nCnt = 3 ; nCnt <= MAXFACTDOUBLE ; nCnt++ )
223 if( bOdd )
225 fOdd *= nCnt;
226 pFactDoubles[ nCnt ] = fOdd;
228 else
230 fEven *= nCnt;
231 pFactDoubles[ nCnt ] = fEven;
234 bOdd = !bOdd;
239 return pFactDoubles[ nNum ];
243 OUString AnalysisAddIn::getImplementationName_Static()
245 return OUString( MY_IMPLNAME );
249 uno::Sequence< OUString > AnalysisAddIn::getSupportedServiceNames_Static()
251 uno::Sequence< OUString > aRet(2);
252 OUString* pArray = aRet.getArray();
253 pArray[0] = OUString( ADDIN_SERVICE );
254 pArray[1] = OUString( MY_SERVICE );
255 return aRet;
259 uno::Reference< uno::XInterface > SAL_CALL AnalysisAddIn_CreateInstance(
260 const uno::Reference< lang::XMultiServiceFactory >& xServiceFact )
262 static uno::Reference< uno::XInterface > xInst = (cppu::OWeakObject*) new AnalysisAddIn( comphelper::getComponentContext(xServiceFact) );
263 return xInst;
267 // XServiceName
269 OUString SAL_CALL AnalysisAddIn::getServiceName() throw( uno::RuntimeException )
271 // name of specific AddIn service
272 return OUString( MY_SERVICE );
276 // XServiceInfo
278 OUString SAL_CALL AnalysisAddIn::getImplementationName() throw( uno::RuntimeException )
280 return getImplementationName_Static();
284 sal_Bool SAL_CALL AnalysisAddIn::supportsService( const OUString& aName ) throw( uno::RuntimeException )
286 return aName.compareToAscii( ADDIN_SERVICE ) == 0 || aName.compareToAscii( MY_SERVICE ) == 0;
290 uno::Sequence< OUString > SAL_CALL AnalysisAddIn::getSupportedServiceNames() throw( uno::RuntimeException )
292 return getSupportedServiceNames_Static();
296 // XLocalizable
298 void SAL_CALL AnalysisAddIn::setLocale( const lang::Locale& eLocale ) throw( uno::RuntimeException )
300 aFuncLoc = eLocale;
302 InitData(); // change of locale invalidates resources!
305 lang::Locale SAL_CALL AnalysisAddIn::getLocale() throw( uno::RuntimeException )
307 return aFuncLoc;
311 // XAddIn
313 OUString SAL_CALL AnalysisAddIn::getProgrammaticFuntionName( const OUString& ) throw( uno::RuntimeException )
315 // not used by calc
316 // (but should be implemented for other uses of the AddIn service)
318 return OUString();
322 OUString SAL_CALL AnalysisAddIn::getDisplayFunctionName( const OUString& aProgrammaticName ) throw( uno::RuntimeException )
324 OUString aRet;
326 const FuncData* p = pFD->Get( aProgrammaticName );
327 if( p )
329 aRet = GetDisplFuncStr( p->GetUINameID() );
330 if( p->IsDouble() )
331 aRet += "_ADD";
333 else
335 aRet = "UNKNOWNFUNC_" + aProgrammaticName;
338 return aRet;
342 OUString SAL_CALL AnalysisAddIn::getFunctionDescription( const OUString& aProgrammaticName ) throw( uno::RuntimeException )
344 OUString aRet;
346 const FuncData* p = pFD->Get( aProgrammaticName );
347 if( p )
348 aRet = GetFuncDescrStr( p->GetDescrID(), 1 );
350 return aRet;
354 OUString SAL_CALL AnalysisAddIn::getDisplayArgumentName( const OUString& aName, sal_Int32 nArg ) throw( uno::RuntimeException )
356 OUString aRet;
358 const FuncData* p = pFD->Get( aName );
359 if( p && nArg <= 0xFFFF )
361 sal_uInt16 nStr = p->GetStrIndex( sal_uInt16( nArg ) );
362 if( nStr )
363 aRet = GetFuncDescrStr( p->GetDescrID(), nStr );
364 else
365 aRet = "internal";
368 return aRet;
372 OUString SAL_CALL AnalysisAddIn::getArgumentDescription( const OUString& aName, sal_Int32 nArg ) throw( uno::RuntimeException )
374 OUString aRet;
376 const FuncData* p = pFD->Get( aName );
377 if( p && nArg <= 0xFFFF )
379 sal_uInt16 nStr = p->GetStrIndex( sal_uInt16( nArg ) );
380 if( nStr )
381 aRet = GetFuncDescrStr( p->GetDescrID(), nStr + 1 );
382 else
383 aRet = "for internal use only";
386 return aRet;
390 static const OUString pDefCatName("Add-In");
393 OUString SAL_CALL AnalysisAddIn::getProgrammaticCategoryName( const OUString& aName ) throw( uno::RuntimeException )
395 // return non-translated strings
396 // return OUString( "Add-In" );
397 const FuncData* p = pFD->Get( aName );
398 OUString aRet;
399 if( p )
401 switch( p->GetCategory() )
403 case FDCat_DateTime: aRet = "Date&Time"; break;
404 case FDCat_Finance: aRet = "Financial"; break;
405 case FDCat_Inf: aRet = "Information"; break;
406 case FDCat_Math: aRet = "Mathematical"; break;
407 case FDCat_Tech: aRet = "Technical"; break;
408 default:
409 aRet = pDefCatName; break;
412 else
413 aRet = pDefCatName;
415 return aRet;
419 OUString SAL_CALL AnalysisAddIn::getDisplayCategoryName( const OUString& aProgrammaticFunctionName ) throw( uno::RuntimeException )
421 // return translated strings, not used for predefined categories
422 // return OUString( "Add-In" );
423 const FuncData* p = pFD->Get( aProgrammaticFunctionName );
424 OUString aRet;
425 if( p )
427 switch( p->GetCategory() )
429 case FDCat_DateTime: aRet = "Date&Time"; break;
430 case FDCat_Finance: aRet = "Financial"; break;
431 case FDCat_Inf: aRet = "Information"; break;
432 case FDCat_Math: aRet = "Mathematical"; break;
433 case FDCat_Tech: aRet = "Technical"; break;
434 default:
435 aRet = pDefCatName; break;
438 else
439 aRet = pDefCatName;
441 return aRet;
445 static const sal_Char* pLang[] = { "de", "en" };
446 static const sal_Char* pCoun[] = { "DE", "US" };
447 static const sal_uInt32 nNumOfLoc = SAL_N_ELEMENTS(pLang);
450 void AnalysisAddIn::InitDefLocales( void )
452 pDefLocales = new lang::Locale[ nNumOfLoc ];
454 for( sal_uInt32 n = 0 ; n < nNumOfLoc ; n++ )
456 pDefLocales[ n ].Language = OUString::createFromAscii( pLang[ n ] );
457 pDefLocales[ n ].Country = OUString::createFromAscii( pCoun[ n ] );
462 inline const lang::Locale& AnalysisAddIn::GetLocale( sal_uInt32 nInd )
464 if( !pDefLocales )
465 InitDefLocales();
467 if( nInd < sizeof( pLang ) )
468 return pDefLocales[ nInd ];
469 else
470 return aFuncLoc;
474 uno::Sequence< sheet::LocalizedName > SAL_CALL AnalysisAddIn::getCompatibilityNames( const OUString& aProgrammaticName ) throw( uno::RuntimeException )
476 const FuncData* p = pFD->Get( aProgrammaticName );
478 if( !p )
479 return uno::Sequence< sheet::LocalizedName >( 0 );
481 const StringList& r = p->GetCompNameList();
482 sal_uInt32 nCount = r.Count();
484 uno::Sequence< sheet::LocalizedName > aRet( nCount );
486 sheet::LocalizedName* pArray = aRet.getArray();
488 for( sal_uInt32 n = 0 ; n < nCount ; n++ )
490 pArray[ n ] = sheet::LocalizedName( GetLocale( n ), *r.Get( n ) );
493 return aRet;
497 // XAnalysis
500 * Workday
503 sal_Int32 SAL_CALL AnalysisAddIn::getWorkday( const uno::Reference< beans::XPropertySet >& xOptions,
504 sal_Int32 nDate, sal_Int32 nDays, const uno::Any& aHDay ) throw( uno::RuntimeException, lang::IllegalArgumentException )
506 if( !nDays )
507 return nDate;
509 sal_Int32 nNullDate = GetNullDate( xOptions );
511 SortedIndividualInt32List aSrtLst;
513 aSrtLst.InsertHolidayList( aAnyConv, xOptions, aHDay, nNullDate, sal_False );
515 sal_Int32 nActDate = nDate + nNullDate;
517 if( nDays > 0 )
519 if( GetDayOfWeek( nActDate ) == 5 )
520 // when starting on Saturday, assuming we're starting on Sunday to get the jump over the weekend
521 nActDate++;
523 while( nDays )
525 nActDate++;
527 if( GetDayOfWeek( nActDate ) < 5 )
529 if( !aSrtLst.Find( nActDate ) )
530 nDays--;
532 else
533 nActDate++; // jump over weekend
536 else
538 if( GetDayOfWeek( nActDate ) == 6 )
539 // when starting on Sunday, assuming we're starting on Saturday to get the jump over the weekend
540 nActDate--;
542 while( nDays )
544 nActDate--;
546 if( GetDayOfWeek( nActDate ) < 5 )
548 if( !aSrtLst.Find( nActDate ) )
549 nDays++;
551 else
552 nActDate--; // jump over weekend
556 return nActDate - nNullDate;
561 * Yearfrac
564 double SAL_CALL AnalysisAddIn::getYearfrac( const uno::Reference< beans::XPropertySet >& xOpt,
565 sal_Int32 nStartDate, sal_Int32 nEndDate, const uno::Any& rMode ) throw( uno::RuntimeException, lang::IllegalArgumentException )
567 double fRet = GetYearFrac( xOpt, nStartDate, nEndDate, getDateMode( xOpt, rMode ) );
568 RETURN_FINITE( fRet );
572 sal_Int32 SAL_CALL AnalysisAddIn::getEdate( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nStartDate, sal_Int32 nMonths ) throw( uno::RuntimeException, lang::IllegalArgumentException )
574 sal_Int32 nNullDate = GetNullDate( xOpt );
575 ScaDate aDate( nNullDate, nStartDate, 5 );
576 aDate.addMonths( nMonths );
577 return aDate.getDate( nNullDate );
581 sal_Int32 SAL_CALL AnalysisAddIn::getWeeknum( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nDate, sal_Int32 nMode ) throw( uno::RuntimeException, lang::IllegalArgumentException )
583 nDate += GetNullDate( xOpt );
585 sal_uInt16 nDay, nMonth, nYear;
586 DaysToDate( nDate, nDay, nMonth, nYear );
588 sal_Int32 nFirstInYear = DateToDays( 1, 1, nYear );
589 sal_uInt16 nFirstDayInYear = GetDayOfWeek( nFirstInYear );
591 return ( nDate - nFirstInYear + ( ( nMode == 1 )? ( nFirstDayInYear + 1 ) % 7 : nFirstDayInYear ) ) / 7 + 1;
595 sal_Int32 SAL_CALL AnalysisAddIn::getEomonth( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nDate, sal_Int32 nMonths ) throw( uno::RuntimeException, lang::IllegalArgumentException )
597 sal_Int32 nNullDate = GetNullDate( xOpt );
598 nDate += nNullDate;
599 sal_uInt16 nDay, nMonth, nYear;
600 DaysToDate( nDate, nDay, nMonth, nYear );
602 sal_Int32 nNewMonth = nMonth + nMonths;
604 if( nNewMonth > 12 )
606 nYear = sal::static_int_cast<sal_uInt16>( nYear + ( nNewMonth / 12 ) );
607 nNewMonth %= 12;
609 else if( nNewMonth < 1 )
611 nNewMonth = -nNewMonth;
612 nYear = sal::static_int_cast<sal_uInt16>( nYear - ( nNewMonth / 12 ) );
613 nYear--;
614 nNewMonth %= 12;
615 nNewMonth = 12 - nNewMonth;
618 return DateToDays( DaysInMonth( sal_uInt16( nNewMonth ), nYear ), sal_uInt16( nNewMonth ), nYear ) - nNullDate;
622 sal_Int32 SAL_CALL AnalysisAddIn::getNetworkdays( const uno::Reference< beans::XPropertySet >& xOpt,
623 sal_Int32 nStartDate, sal_Int32 nEndDate, const uno::Any& aHDay ) throw( uno::RuntimeException, lang::IllegalArgumentException )
625 sal_Int32 nNullDate = GetNullDate( xOpt );
627 SortedIndividualInt32List aSrtLst;
629 aSrtLst.InsertHolidayList( aAnyConv, xOpt, aHDay, nNullDate, sal_False );
631 sal_Int32 nActDate = nStartDate + nNullDate;
632 sal_Int32 nStopDate = nEndDate + nNullDate;
633 sal_Int32 nCnt = 0;
635 if( nActDate <= nStopDate )
637 while( nActDate <= nStopDate )
639 if( GetDayOfWeek( nActDate ) < 5 && !aSrtLst.Find( nActDate ) )
640 nCnt++;
642 nActDate++;
645 else
647 while( nActDate >= nStopDate )
649 if( GetDayOfWeek( nActDate ) < 5 && !aSrtLst.Find( nActDate ) )
650 nCnt--;
652 nActDate--;
656 return nCnt;
660 sal_Int32 SAL_CALL AnalysisAddIn::getIseven( sal_Int32 nVal ) throw( uno::RuntimeException, lang::IllegalArgumentException )
662 return ( nVal & 0x00000001 )? 0 : 1;
666 sal_Int32 SAL_CALL AnalysisAddIn::getIsodd( sal_Int32 nVal ) throw( uno::RuntimeException, lang::IllegalArgumentException )
668 return ( nVal & 0x00000001 )? 1 : 0;
671 double SAL_CALL
672 AnalysisAddIn::getMultinomial( const uno::Reference< beans::XPropertySet >& xOpt, const uno::Sequence< uno::Sequence< sal_Int32 > >& aVLst,
673 const uno::Sequence< uno::Any >& aOptVLst ) throw( uno::RuntimeException, lang::IllegalArgumentException )
675 ScaDoubleListGE0 aValList;
677 aValList.Append( aVLst );
678 aValList.Append( aAnyConv, xOpt, aOptVLst );
680 if( aValList.Count() == 0 )
681 return 0.0;
683 double nZ = 0;
684 double fRet = 1.0;
686 for( const double *p = aValList.First(); p; p = aValList.Next() )
688 double n = (*p >= 0.0) ? rtl::math::approxFloor( *p ) : rtl::math::approxCeil( *p );
689 if ( n < 0.0 )
690 throw lang::IllegalArgumentException();
692 if( n > 0.0 )
694 nZ += n;
695 fRet *= BinomialCoefficient(nZ, n);
698 RETURN_FINITE( fRet );
702 double SAL_CALL AnalysisAddIn::getSeriessum( double fX, double fN, double fM, const uno::Sequence< uno::Sequence< double > >& aCoeffList ) throw( uno::RuntimeException, lang::IllegalArgumentException )
704 double fRet = 0.0;
706 // #i32269# 0^0 is undefined, Excel returns #NUM! error
707 if( fX == 0.0 && fN == 0 )
708 throw uno::RuntimeException();
710 if( fX != 0.0 )
712 sal_Int32 n1, n2;
713 sal_Int32 nE1 = aCoeffList.getLength();
714 sal_Int32 nE2;
716 for( n1 = 0 ; n1 < nE1 ; n1++ )
718 const uno::Sequence< double >& rList = aCoeffList[ n1 ];
719 nE2 = rList.getLength();
720 const double* pList = rList.getConstArray();
722 for( n2 = 0 ; n2 < nE2 ; n2++ )
724 fRet += pList[ n2 ] * pow( fX, fN );
726 fN += fM;
731 RETURN_FINITE( fRet );
735 double SAL_CALL AnalysisAddIn::getQuotient( double fNum, double fDenom ) throw( uno::RuntimeException, lang::IllegalArgumentException )
737 double fRet;
738 if( (fNum < 0) != (fDenom < 0) )
739 fRet = ::rtl::math::approxCeil( fNum / fDenom );
740 else
741 fRet = ::rtl::math::approxFloor( fNum / fDenom );
742 RETURN_FINITE( fRet );
746 double SAL_CALL AnalysisAddIn::getMround( double fNum, double fMult ) throw( uno::RuntimeException, lang::IllegalArgumentException )
748 if( fMult == 0.0 )
749 return fMult;
751 double fRet = fMult * ::rtl::math::round( fNum / fMult );
752 RETURN_FINITE( fRet );
756 double SAL_CALL AnalysisAddIn::getSqrtpi( double fNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
758 double fRet = sqrt( fNum * PI );
759 RETURN_FINITE( fRet );
763 double SAL_CALL AnalysisAddIn::getRandbetween( double fMin, double fMax ) throw( uno::RuntimeException, lang::IllegalArgumentException )
765 fMin = ::rtl::math::round( fMin, 0, rtl_math_RoundingMode_Up );
766 fMax = ::rtl::math::round( fMax, 0, rtl_math_RoundingMode_Up );
767 if( fMin > fMax )
768 throw lang::IllegalArgumentException();
770 // fMax -> range
771 double fRet = fMax - fMin + 1.0;
772 fRet *= rand();
773 fRet /= (RAND_MAX + 1.0);
774 fRet += fMin;
775 fRet = floor( fRet ); // simple floor is sufficient here
776 RETURN_FINITE( fRet );
780 double SAL_CALL AnalysisAddIn::getGcd( const uno::Reference< beans::XPropertySet >& xOpt, const uno::Sequence< uno::Sequence< double > >& aVLst, const uno::Sequence< uno::Any >& aOptVLst ) throw( uno::RuntimeException, lang::IllegalArgumentException )
782 ScaDoubleListGT0 aValList;
784 aValList.Append( aVLst );
785 aValList.Append( aAnyConv, xOpt, aOptVLst );
787 if( aValList.Count() == 0 )
788 return 0.0;
790 const double* p = aValList.First();
791 double f = *p;
793 p = aValList.Next();
795 while( p )
797 f = GetGcd( *p, f );
798 p = aValList.Next();
801 RETURN_FINITE( f );
805 double SAL_CALL AnalysisAddIn::getLcm( const uno::Reference< beans::XPropertySet >& xOpt, const uno::Sequence< uno::Sequence< double > >& aVLst, const uno::Sequence< uno::Any >& aOptVLst ) throw( uno::RuntimeException, lang::IllegalArgumentException )
807 ScaDoubleListGE0 aValList;
809 aValList.Append( aVLst );
810 aValList.Append( aAnyConv, xOpt, aOptVLst );
812 if( aValList.Count() == 0 )
813 return 0.0;
815 const double* p = aValList.First();
816 double f = *p;
818 if( f == 0.0 )
819 return f;
821 p = aValList.Next();
823 while( p )
825 double fTmp = *p;
826 if( f == 0.0 )
827 return f;
828 else
829 f = fTmp * f / GetGcd( fTmp, f );
830 p = aValList.Next();
833 RETURN_FINITE( f );
837 double SAL_CALL AnalysisAddIn::getBesseli( double fNum, sal_Int32 nOrder ) throw( uno::RuntimeException, lang::IllegalArgumentException, sheet::NoConvergenceException )
839 double fRet = sca::analysis::BesselI( fNum, nOrder );
840 RETURN_FINITE( fRet );
844 double SAL_CALL AnalysisAddIn::getBesselj( double fNum, sal_Int32 nOrder ) throw( uno::RuntimeException, lang::IllegalArgumentException, sheet::NoConvergenceException )
846 double fRet = sca::analysis::BesselJ( fNum, nOrder );
847 RETURN_FINITE( fRet );
851 double SAL_CALL AnalysisAddIn::getBesselk( double fNum, sal_Int32 nOrder ) throw( uno::RuntimeException, lang::IllegalArgumentException, sheet::NoConvergenceException )
853 if( nOrder < 0 || fNum <= 0.0 )
854 throw lang::IllegalArgumentException();
856 double fRet = sca::analysis::BesselK( fNum, nOrder );
857 RETURN_FINITE( fRet );
861 double SAL_CALL AnalysisAddIn::getBessely( double fNum, sal_Int32 nOrder ) throw( uno::RuntimeException, lang::IllegalArgumentException, sheet::NoConvergenceException )
863 if( nOrder < 0 || fNum <= 0.0 )
864 throw lang::IllegalArgumentException();
866 double fRet = sca::analysis::BesselY( fNum, nOrder );
867 RETURN_FINITE( fRet );
871 const double SCA_MAX2 = 511.0; // min. val for binary numbers (9 bits + sign)
872 const double SCA_MIN2 = -SCA_MAX2-1.0; // min. val for binary numbers (9 bits + sign)
873 const double SCA_MAX8 = 536870911.0; // max. val for octal numbers (29 bits + sign)
874 const double SCA_MIN8 = -SCA_MAX8-1.0; // min. val for octal numbers (29 bits + sign)
875 const double SCA_MAX16 = 549755813888.0; // max. val for hexadecimal numbers (39 bits + sign)
876 const double SCA_MIN16 = -SCA_MAX16-1.0; // min. val for hexadecimal numbers (39 bits + sign)
877 const sal_Int32 SCA_MAXPLACES = 10; // max. number of places
880 OUString SAL_CALL AnalysisAddIn::getBin2Oct( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces ) throw( uno::RuntimeException, lang::IllegalArgumentException )
882 double fVal = ConvertToDec( aNum, 2, SCA_MAXPLACES );
883 sal_Int32 nPlaces = 0;
884 sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
885 return ConvertFromDec( fVal, SCA_MIN8, SCA_MAX8, 8, nPlaces, SCA_MAXPLACES, bUsePlaces );
889 double SAL_CALL AnalysisAddIn::getBin2Dec( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
891 double fRet = ConvertToDec( aNum, 2, SCA_MAXPLACES );
892 RETURN_FINITE( fRet );
896 OUString SAL_CALL AnalysisAddIn::getBin2Hex( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces ) throw( uno::RuntimeException, lang::IllegalArgumentException )
898 double fVal = ConvertToDec( aNum, 2, SCA_MAXPLACES );
899 sal_Int32 nPlaces = 0;
900 sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
901 return ConvertFromDec( fVal, SCA_MIN16, SCA_MAX16, 16, nPlaces, SCA_MAXPLACES, bUsePlaces );
905 OUString SAL_CALL AnalysisAddIn::getOct2Bin( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces ) throw( uno::RuntimeException, lang::IllegalArgumentException )
907 double fVal = ConvertToDec( aNum, 8, SCA_MAXPLACES );
908 sal_Int32 nPlaces = 0;
909 sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
910 return ConvertFromDec( fVal, SCA_MIN2, SCA_MAX2, 2, nPlaces, SCA_MAXPLACES, bUsePlaces );
914 double SAL_CALL AnalysisAddIn::getOct2Dec( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
916 double fRet = ConvertToDec( aNum, 8, SCA_MAXPLACES );
917 RETURN_FINITE( fRet );
921 OUString SAL_CALL AnalysisAddIn::getOct2Hex( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces ) throw( uno::RuntimeException, lang::IllegalArgumentException )
923 double fVal = ConvertToDec( aNum, 8, SCA_MAXPLACES );
924 sal_Int32 nPlaces = 0;
925 sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
926 return ConvertFromDec( fVal, SCA_MIN16, SCA_MAX16, 16, nPlaces, SCA_MAXPLACES, bUsePlaces );
930 OUString SAL_CALL AnalysisAddIn::getDec2Bin( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nNum, const uno::Any& rPlaces ) throw( uno::RuntimeException, lang::IllegalArgumentException )
932 sal_Int32 nPlaces = 0;
933 sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
934 return ConvertFromDec( nNum, SCA_MIN2, SCA_MAX2, 2, nPlaces, SCA_MAXPLACES, bUsePlaces );
938 OUString SAL_CALL AnalysisAddIn::getDec2Oct( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nNum, const uno::Any& rPlaces ) throw( uno::RuntimeException, lang::IllegalArgumentException )
940 sal_Int32 nPlaces = 0;
941 sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
942 return ConvertFromDec( nNum, SCA_MIN8, SCA_MAX8, 8, nPlaces, SCA_MAXPLACES, bUsePlaces );
946 OUString SAL_CALL AnalysisAddIn::getDec2Hex( const uno::Reference< beans::XPropertySet >& xOpt, double fNum, const uno::Any& rPlaces ) throw( uno::RuntimeException, lang::IllegalArgumentException )
948 sal_Int32 nPlaces = 0;
949 sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
950 return ConvertFromDec( fNum, SCA_MIN16, SCA_MAX16, 16, nPlaces, SCA_MAXPLACES, bUsePlaces );
954 OUString SAL_CALL AnalysisAddIn::getHex2Bin( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces ) throw( uno::RuntimeException, lang::IllegalArgumentException )
956 double fVal = ConvertToDec( aNum, 16, SCA_MAXPLACES );
957 sal_Int32 nPlaces = 0;
958 sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
959 return ConvertFromDec( fVal, SCA_MIN2, SCA_MAX2, 2, nPlaces, SCA_MAXPLACES, bUsePlaces );
963 double SAL_CALL AnalysisAddIn::getHex2Dec( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
965 double fRet = ConvertToDec( aNum, 16, SCA_MAXPLACES );
966 RETURN_FINITE( fRet );
970 OUString SAL_CALL AnalysisAddIn::getHex2Oct( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces ) throw( uno::RuntimeException, lang::IllegalArgumentException )
972 double fVal = ConvertToDec( aNum, 16, SCA_MAXPLACES );
973 sal_Int32 nPlaces = 0;
974 sal_Bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
975 return ConvertFromDec( fVal, SCA_MIN8, SCA_MAX8, 8, nPlaces, SCA_MAXPLACES, bUsePlaces );
979 sal_Int32 SAL_CALL AnalysisAddIn::getDelta( const uno::Reference< beans::XPropertySet >& xOpt, double fNum1, const uno::Any& rNum2 ) throw( uno::RuntimeException, lang::IllegalArgumentException )
981 return fNum1 == aAnyConv.getDouble( xOpt, rNum2, 0.0 );
985 double SAL_CALL AnalysisAddIn::getErf( const uno::Reference< beans::XPropertySet >& xOpt, double fLL, const uno::Any& rUL ) throw( uno::RuntimeException, lang::IllegalArgumentException )
987 double fUL, fRet;
988 sal_Bool bContainsValue = aAnyConv.getDouble( fUL, xOpt, rUL );
990 fRet = bContainsValue ? (Erf( fUL ) - Erf( fLL )) : Erf( fLL );
991 RETURN_FINITE( fRet );
995 double SAL_CALL AnalysisAddIn::getErfc( double f ) throw( uno::RuntimeException, lang::IllegalArgumentException )
997 double fRet = Erfc( f );
998 RETURN_FINITE( fRet );
1002 sal_Int32 SAL_CALL AnalysisAddIn::getGestep( const uno::Reference< beans::XPropertySet >& xOpt, double fNum, const uno::Any& rStep ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1004 return fNum >= aAnyConv.getDouble( xOpt, rStep, 0.0 );
1008 double SAL_CALL AnalysisAddIn::getFactdouble( sal_Int32 nNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1010 double fRet = FactDouble( nNum );
1011 RETURN_FINITE( fRet );
1015 double SAL_CALL AnalysisAddIn::getImabs( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1017 double fRet = Complex( aNum ).Abs();
1018 RETURN_FINITE( fRet );
1022 double SAL_CALL AnalysisAddIn::getImaginary( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1024 double fRet = Complex( aNum ).Imag();
1025 RETURN_FINITE( fRet );
1029 OUString SAL_CALL AnalysisAddIn::getImpower( const OUString& aNum, double f ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1031 Complex z( aNum );
1033 z.Power( f );
1035 return z.GetString();
1039 double SAL_CALL AnalysisAddIn::getImargument( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1041 double fRet = Complex( aNum ).Arg();
1042 RETURN_FINITE( fRet );
1046 OUString SAL_CALL AnalysisAddIn::getImcos( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1048 Complex z( aNum );
1050 z.Cos();
1052 return z.GetString();
1056 OUString SAL_CALL AnalysisAddIn::getImdiv( const OUString& aDivid, const OUString& aDivis ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1058 Complex z( aDivid );
1060 z.Div( Complex( aDivis ) );
1062 return z.GetString();
1066 OUString SAL_CALL AnalysisAddIn::getImexp( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1068 Complex z( aNum );
1070 z.Exp();
1072 return z.GetString();
1076 OUString SAL_CALL AnalysisAddIn::getImconjugate( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1078 Complex z( aNum );
1080 z.Conjugate();
1082 return z.GetString();
1086 OUString SAL_CALL AnalysisAddIn::getImln( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1088 Complex z( aNum );
1090 z.Ln();
1092 return z.GetString();
1096 OUString SAL_CALL AnalysisAddIn::getImlog10( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1098 Complex z( aNum );
1100 z.Log10();
1102 return z.GetString();
1106 OUString SAL_CALL AnalysisAddIn::getImlog2( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1108 Complex z( aNum );
1110 z.Log2();
1112 return z.GetString();
1116 OUString SAL_CALL AnalysisAddIn::getImproduct( const uno::Reference< beans::XPropertySet >&, const uno::Sequence< uno::Sequence< OUString > >& aNum1, const uno::Sequence< uno::Any >& aNL ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1118 ComplexList z_list;
1120 z_list.Append( aNum1, AH_IgnoreEmpty );
1121 z_list.Append( aNL, AH_IgnoreEmpty );
1123 const Complex* p = z_list.First();
1125 if( !p )
1126 return Complex( 0 ).GetString();
1128 Complex z( *p );
1130 for( p = z_list.Next() ; p ; p = z_list.Next() )
1131 z.Mult( *p );
1133 return z.GetString();
1137 double SAL_CALL AnalysisAddIn::getImreal( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1139 double fRet = Complex( aNum ).Real();
1140 RETURN_FINITE( fRet );
1144 OUString SAL_CALL AnalysisAddIn::getImsin( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1146 Complex z( aNum );
1148 z.Sin();
1150 return z.GetString();
1154 OUString SAL_CALL AnalysisAddIn::getImsub( const OUString& aNum1, const OUString& aNum2 ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1156 Complex z( aNum1 );
1158 z.Sub( Complex( aNum2 ) );
1160 return z.GetString();
1164 OUString SAL_CALL AnalysisAddIn::getImsum( const uno::Reference< beans::XPropertySet >&, const uno::Sequence< uno::Sequence< OUString > >& aNum1, const uno::Sequence< uno::Any >& aFollowingPars ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1166 ComplexList z_list;
1168 z_list.Append( aNum1, AH_IgnoreEmpty );
1169 z_list.Append( aFollowingPars, AH_IgnoreEmpty );
1171 const Complex* p = z_list.First();
1173 if( !p )
1174 return Complex( 0 ).GetString();
1176 Complex z( *p );
1178 for( p = z_list.Next() ; p ; p = z_list.Next() )
1179 z.Add( *p );
1181 return z.GetString();
1185 OUString SAL_CALL AnalysisAddIn::getImsqrt( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1187 Complex z( aNum );
1189 z.Sqrt();
1191 return z.GetString();
1195 OUString SAL_CALL AnalysisAddIn::getImtan( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1197 Complex z( aNum );
1199 z.Tan();
1201 return z.GetString();
1205 OUString SAL_CALL AnalysisAddIn::getImsec( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1207 Complex z( aNum );
1209 z.Sec();
1211 return z.GetString();
1215 OUString SAL_CALL AnalysisAddIn::getImcsc( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1217 Complex z( aNum );
1219 z.Csc();
1221 return z.GetString();
1225 OUString SAL_CALL AnalysisAddIn::getImcot( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1227 Complex z( aNum );
1229 z.Cot();
1231 return z.GetString();
1235 OUString SAL_CALL AnalysisAddIn::getImsinh( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1237 Complex z( aNum );
1239 z.Sinh();
1241 return z.GetString();
1245 OUString SAL_CALL AnalysisAddIn::getImcosh( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1247 Complex z( aNum );
1249 z.Cosh();
1251 return z.GetString();
1255 OUString SAL_CALL AnalysisAddIn::getImsech( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1257 Complex z( aNum );
1259 z.Sech();
1261 return z.GetString();
1265 OUString SAL_CALL AnalysisAddIn::getImcsch( const OUString& aNum ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1267 Complex z( aNum );
1269 z.Csch();
1271 return z.GetString();
1275 OUString SAL_CALL AnalysisAddIn::getComplex( double fR, double fI, const uno::Any& rSuff ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1277 sal_Bool bi;
1279 switch( rSuff.getValueTypeClass() )
1281 case uno::TypeClass_VOID:
1282 bi = sal_True;
1283 break;
1284 case uno::TypeClass_STRING:
1286 const OUString* pSuff = ( const OUString* ) rSuff.getValue();
1287 bi = pSuff->compareToAscii( "i" ) == 0 || pSuff->isEmpty();
1288 if( !bi && pSuff->compareToAscii( "j" ) != 0 )
1289 throw lang::IllegalArgumentException();
1291 break;
1292 default:
1293 throw lang::IllegalArgumentException();
1296 return Complex( fR, fI, bi ? 'i' : 'j' ).GetString();
1300 double SAL_CALL AnalysisAddIn::getConvert( double f, const OUString& aFU, const OUString& aTU ) throw( uno::RuntimeException, lang::IllegalArgumentException )
1302 if( !pCDL )
1303 pCDL = new ConvertDataList();
1305 double fRet = pCDL->Convert( f, aFU, aTU );
1306 RETURN_FINITE( fRet );
1310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */