summaryrefslogtreecommitdiff
path: root/scaddins/source/analysis/analysis.cxx
blob: 34414abd0f50b220aa9e3dfeb60b99dc4472dcfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "analysis.hxx"
#include <strings.hrc>
#include "bessel.hxx"
#include <cppuhelper/factory.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/random.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <o3tl/any.hxx>
#include <rtl/ustrbuf.hxx>
#include <rtl/math.hxx>
#include <sal/macros.h>
#include <string.h>
#include <unotools/resmgr.hxx>
#include <algorithm>
#include <cmath>

#define ADDIN_SERVICE               "com.sun.star.sheet.AddIn"
#define MY_SERVICE                  "com.sun.star.sheet.addin.Analysis"
#define MY_IMPLNAME                 "com.sun.star.sheet.addin.AnalysisImpl"

using namespace                 ::com::sun::star;
using namespace sca::analysis;
using namespace std;

extern "C" SAL_DLLPUBLIC_EXPORT void* analysis_component_getFactory(
    const sal_Char* pImplName, void* pServiceManager, void* /*pRegistryKey*/ )
{
    void* pRet = nullptr;

    if( pServiceManager && OUString::createFromAscii( pImplName ) == AnalysisAddIn::getImplementationName_Static() )
    {
        uno::Reference< lang::XSingleServiceFactory >  xFactory( cppu::createOneInstanceFactory(
                static_cast< lang::XMultiServiceFactory* >( pServiceManager ),
                AnalysisAddIn::getImplementationName_Static(),
                AnalysisAddIn_CreateInstance,
                AnalysisAddIn::getSupportedServiceNames_Static() ) );

        if( xFactory.is() )
        {
            xFactory->acquire();
            pRet = xFactory.get();
        }
    }

    return pRet;
}

OUString AnalysisAddIn::GetFuncDescrStr(const char** pResId, sal_uInt16 nStrIndex)
{
    return AnalysisResId(pResId[nStrIndex - 1]);
}

void AnalysisAddIn::InitData()
{
    aResLocale = Translate::Create("sca", LanguageTag(aFuncLoc));

    pFD.reset(new FuncDataList);
    InitFuncDataList(*pFD);

    pDefLocales.reset();
}

AnalysisAddIn::AnalysisAddIn( const uno::Reference< uno::XComponentContext >& xContext ) :
    pDefLocales( nullptr ),
    pFD( nullptr ),
    pFactDoubles( nullptr ),
    pCDL( nullptr ),
    aAnyConv( xContext )
{
}

AnalysisAddIn::~AnalysisAddIn()
{
}

sal_Int32 AnalysisAddIn::getDateMode(
        const uno::Reference< beans::XPropertySet >& xPropSet,
        const uno::Any& rAny )
{
    sal_Int32 nMode = aAnyConv.getInt32( xPropSet, rAny, 0 );
    if( (nMode < 0) || (nMode > 4) )
        throw lang::IllegalArgumentException();
    return nMode;
}

#define MAXFACTDOUBLE   300

double AnalysisAddIn::FactDouble( sal_Int32 nNum )
{
    if( nNum < 0 || nNum > MAXFACTDOUBLE )
        throw lang::IllegalArgumentException();

    if( !pFactDoubles )
    {
        pFactDoubles.reset( new double[ MAXFACTDOUBLE + 1 ] );

        pFactDoubles[ 0 ] = 1.0;    // by default

        double      fOdd = 1.0;
        double      fEven = 2.0;

        pFactDoubles[ 1 ] = fOdd;
        pFactDoubles[ 2 ] = fEven;

        bool    bOdd = true;

        for( sal_uInt16 nCnt = 3 ; nCnt <= MAXFACTDOUBLE ; nCnt++ )
        {
            if( bOdd )
            {
                fOdd *= nCnt;
                pFactDoubles[ nCnt ] = fOdd;
            }
            else
            {
                fEven *= nCnt;
                pFactDoubles[ nCnt ] = fEven;
            }

            bOdd = !bOdd;

        }
    }

    return pFactDoubles[ nNum ];
}

OUString AnalysisAddIn::getImplementationName_Static()
{
    return OUString( MY_IMPLNAME );
}

uno::Sequence< OUString > AnalysisAddIn::getSupportedServiceNames_Static()
{
    uno::Sequence< OUString >   aRet(2);
    OUString*         pArray = aRet.getArray();
    pArray[0] = ADDIN_SERVICE;
    pArray[1] = MY_SERVICE;
    return aRet;
}

uno::Reference< uno::XInterface > AnalysisAddIn_CreateInstance(
        const uno::Reference< lang::XMultiServiceFactory >& xServiceFact )
{
    return static_cast<cppu::OWeakObject*>(new AnalysisAddIn( comphelper::getComponentContext(xServiceFact) ));
}

// XServiceName
OUString SAL_CALL AnalysisAddIn::getServiceName()
{
    // name of specific AddIn service
    return OUString( MY_SERVICE );
}

// XServiceInfo
OUString SAL_CALL AnalysisAddIn::getImplementationName()
{
    return getImplementationName_Static();
}

sal_Bool SAL_CALL AnalysisAddIn::supportsService( const OUString& aName )
{
    return cppu::supportsService(this, aName);
}

uno::Sequence< OUString > SAL_CALL AnalysisAddIn::getSupportedServiceNames()
{
    return getSupportedServiceNames_Static();
}

// XLocalizable
void SAL_CALL AnalysisAddIn::setLocale( const lang::Locale& eLocale )
{
    aFuncLoc = eLocale;

    InitData();     // change of locale invalidates resources!
}

lang::Locale SAL_CALL AnalysisAddIn::getLocale()
{
    return aFuncLoc;
}

// XAddIn
OUString SAL_CALL AnalysisAddIn::getProgrammaticFuntionName( const OUString& )
{
    //  not used by calc
    //  (but should be implemented for other uses of the AddIn service)

    return OUString();
}

OUString SAL_CALL AnalysisAddIn::getDisplayFunctionName( const OUString& aProgrammaticName )
{
    OUString          aRet;

    auto it = std::find_if(pFD->begin(), pFD->end(), FindFuncData( aProgrammaticName ) );
    if( it != pFD->end() )
    {
        aRet = AnalysisResId(it->GetUINameID());
        if( it->IsDouble() )
        {
            const OUString& rSuffix = it->GetSuffix();
            if (!rSuffix.isEmpty())
                aRet += rSuffix;
            else
                aRet += "_ADD";
        }
    }
    else
    {
        aRet = "UNKNOWNFUNC_" + aProgrammaticName;
    }

    return aRet;
}

OUString SAL_CALL AnalysisAddIn::getFunctionDescription( const OUString& aProgrammaticName )
{
    OUString          aRet;

    auto it = std::find_if(pFD->begin(), pFD->end(), FindFuncData( aProgrammaticName ) );
    if( it != pFD->end() )
        aRet = GetFuncDescrStr( it->GetDescrID(), 1 );

    return aRet;
}

OUString SAL_CALL AnalysisAddIn::getDisplayArgumentName( const OUString& aName, sal_Int32 nArg )
{
    OUString          aRet;

    auto it = std::find_if(pFD->begin(), pFD->end(), FindFuncData( aName ) );
    if( it != pFD->end() && nArg <= 0xFFFF )
    {
        sal_uInt16  nStr = it->GetStrIndex( sal_uInt16( nArg ) );
        if( nStr )
            aRet = GetFuncDescrStr( it->GetDescrID(), nStr );
        else
            aRet = "internal";
    }

    return aRet;
}

OUString SAL_CALL AnalysisAddIn::getArgumentDescription( const OUString& aName, sal_Int32 nArg )
{
    OUString          aRet;

    auto it = std::find_if(pFD->begin(), pFD->end(), FindFuncData( aName ) );
    if( it != pFD->end() && nArg <= 0xFFFF )
    {
        sal_uInt16  nStr = it->GetStrIndex( sal_uInt16( nArg ) );
        if( nStr )
            aRet = GetFuncDescrStr( it->GetDescrID(), nStr + 1 );
        else
            aRet = "for internal use only";
    }

    return aRet;
}

static const char pDefCatName[] = "Add-In";

OUString SAL_CALL AnalysisAddIn::getProgrammaticCategoryName( const OUString& aName )
{
    //  return non-translated strings
    //  return OUString( "Add-In" );
    auto it = std::find_if(pFD->begin(), pFD->end(), FindFuncData( aName ) );
    OUString              aRet;
    if( it != pFD->end() )
    {
        switch( it->GetCategory() )
        {
            case FDCategory::DateTime:    aRet = "Date&Time";         break;
            case FDCategory::Finance:     aRet = "Financial";         break;
            case FDCategory::Inf:         aRet = "Information";       break;
            case FDCategory::Math:        aRet = "Mathematical";      break;
            case FDCategory::Tech:        aRet = "Technical";         break;
        }
    }
    else
        aRet = pDefCatName;

    return aRet;
}

OUString SAL_CALL AnalysisAddIn::getDisplayCategoryName( const OUString& aProgrammaticFunctionName )
{
    //  return translated strings, not used for predefined categories
    auto it = std::find_if(pFD->begin(), pFD->end(), FindFuncData( aProgrammaticFunctionName ) );
    OUString              aRet;
    if( it != pFD->end() )
    {
        switch( it->GetCategory() )
        {
            case FDCategory::DateTime:    aRet = "Date&Time";         break;
            case FDCategory::Finance:     aRet = "Financial";         break;
            case FDCategory::Inf:         aRet = "Information";       break;
            case FDCategory::Math:        aRet = "Mathematical";      break;
            case FDCategory::Tech:        aRet = "Technical";         break;
        }
    }
    else
        aRet = pDefCatName;

    return aRet;
}

static const sal_Char*      pLang[] = { "de", "en" };
static const sal_Char*      pCoun[] = { "DE", "US" };
static const sal_uInt32     nNumOfLoc = SAL_N_ELEMENTS(pLang);

void AnalysisAddIn::InitDefLocales()
{
    pDefLocales.reset( new lang::Locale[ nNumOfLoc ] );

    for( sal_uInt32 n = 0 ; n < nNumOfLoc ; n++ )
    {
        pDefLocales[ n ].Language = OUString::createFromAscii( pLang[ n ] );
        pDefLocales[ n ].Country = OUString::createFromAscii( pCoun[ n ] );
    }
}

inline const lang::Locale& AnalysisAddIn::GetLocale( sal_uInt32 nInd )
{
    if( !pDefLocales )
        InitDefLocales();

    if( nInd < sizeof( pLang ) )
        return pDefLocales[ nInd ];
    else
        return aFuncLoc;
}

uno::Sequence< sheet::LocalizedName > SAL_CALL AnalysisAddIn::getCompatibilityNames( const OUString& aProgrammaticName )
{
    auto it = std::find_if(pFD->begin(), pFD->end(), FindFuncData( aProgrammaticName ) );
    if( it == pFD->end() )
        return uno::Sequence< sheet::LocalizedName >( 0 );

    const std::vector<OUString>& r = it->GetCompNameList();
    sal_uInt32                   nCount = r.size();

    uno::Sequence< sheet::LocalizedName >                aRet( nCount );

    sheet::LocalizedName*  pArray = aRet.getArray();

    for( sal_uInt32 n = 0 ; n < nCount ; n++ )
    {
        pArray[ n ] = sheet::LocalizedName( GetLocale( n ), r[n] );
    }

    return aRet;
}

// XAnalysis
/** Workday */
sal_Int32 SAL_CALL AnalysisAddIn::getWorkday( const uno::Reference< beans::XPropertySet >& xOptions,
    sal_Int32 nDate, sal_Int32 nDays, const uno::Any& aHDay )
{
    if( !nDays )
        return nDate;

    sal_Int32                   nNullDate = GetNullDate( xOptions );

    SortedIndividualInt32List   aSrtLst;

    aSrtLst.InsertHolidayList( aAnyConv, xOptions, aHDay, nNullDate );

    sal_Int32                   nActDate = nDate + nNullDate;

    if( nDays > 0 )
    {
        if( GetDayOfWeek( nActDate ) == 5 )
            // when starting on Saturday, assuming we're starting on Sunday to get the jump over the weekend
            nActDate++;

        while( nDays )
        {
            nActDate++;

            if( GetDayOfWeek( nActDate ) < 5 )
            {
                if( !aSrtLst.Find( nActDate ) )
                    nDays--;
            }
            else
                nActDate++;     // jump over weekend
        }
    }
    else
    {
        if( GetDayOfWeek( nActDate ) == 6 )
            // when starting on Sunday, assuming we're starting on Saturday to get the jump over the weekend
            nActDate--;

        while( nDays )
        {
            nActDate--;

            if( GetDayOfWeek( nActDate ) < 5 )
            {
                if( !aSrtLst.Find( nActDate ) )
                    nDays++;
            }
            else
                nActDate--;     // jump over weekend
        }
    }

    return nActDate - nNullDate;
}

/** Yearfrac */
double SAL_CALL AnalysisAddIn::getYearfrac( const uno::Reference< beans::XPropertySet >& xOpt,
    sal_Int32 nStartDate, sal_Int32 nEndDate, const uno::Any& rMode )
{
    double fRet = GetYearFrac( xOpt, nStartDate, nEndDate, getDateMode( xOpt, rMode ) );
    RETURN_FINITE( fRet );
}

sal_Int32 SAL_CALL AnalysisAddIn::getEdate( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nStartDate, sal_Int32 nMonths )
{
    sal_Int32 nNullDate = GetNullDate( xOpt );
    ScaDate aDate( nNullDate, nStartDate, 5 );
    aDate.addMonths( nMonths );
    return aDate.getDate( nNullDate );
}

sal_Int32 SAL_CALL AnalysisAddIn::getWeeknum( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nDate, sal_Int32 nMode )
{
    nDate += GetNullDate( xOpt );

    sal_uInt16  nDay, nMonth, nYear;
    DaysToDate( nDate, nDay, nMonth, nYear );

    sal_Int32   nFirstInYear = DateToDays( 1, 1, nYear );
    sal_uInt16  nFirstDayInYear = GetDayOfWeek( nFirstInYear );

    return ( nDate - nFirstInYear + ( ( nMode == 1 )? ( nFirstDayInYear + 1 ) % 7 : nFirstDayInYear ) ) / 7 + 1;
}

sal_Int32 SAL_CALL AnalysisAddIn::getEomonth( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nDate, sal_Int32 nMonths )
{
    sal_Int32   nNullDate = GetNullDate( xOpt );
    nDate += nNullDate;
    sal_uInt16  nDay, nMonth, nYear;
    DaysToDate( nDate, nDay, nMonth, nYear );

    sal_Int32   nNewMonth = nMonth + nMonths;

    if( nNewMonth > 12 )
    {
        nYear = sal::static_int_cast<sal_uInt16>( nYear + ( nNewMonth / 12 ) );
        nNewMonth %= 12;
    }
    else if( nNewMonth < 1 )
    {
        nNewMonth = -nNewMonth;
        nYear = sal::static_int_cast<sal_uInt16>( nYear - ( nNewMonth / 12 ) );
        nYear--;
        nNewMonth %= 12;
        nNewMonth = 12 - nNewMonth;
    }

    return DateToDays( DaysInMonth( sal_uInt16( nNewMonth ), nYear ), sal_uInt16( nNewMonth ), nYear ) - nNullDate;
}

sal_Int32 SAL_CALL AnalysisAddIn::getNetworkdays( const uno::Reference< beans::XPropertySet >& xOpt,
        sal_Int32 nStartDate, sal_Int32 nEndDate, const uno::Any& aHDay )
{
    sal_Int32                   nNullDate = GetNullDate( xOpt );

    SortedIndividualInt32List   aSrtLst;

    aSrtLst.InsertHolidayList( aAnyConv, xOpt, aHDay, nNullDate );

    sal_Int32                   nActDate = nStartDate + nNullDate;
    sal_Int32                   nStopDate = nEndDate + nNullDate;
    sal_Int32                   nCnt = 0;

    if( nActDate <= nStopDate )
    {
        while( nActDate <= nStopDate )
        {
            if( GetDayOfWeek( nActDate ) < 5 && !aSrtLst.Find( nActDate ) )
                nCnt++;

            nActDate++;
        }
    }
    else
    {
        while( nActDate >= nStopDate )
        {
            if( GetDayOfWeek( nActDate ) < 5 && !aSrtLst.Find( nActDate ) )
                nCnt--;

            nActDate--;
        }
    }

    return nCnt;
}

sal_Int32 SAL_CALL AnalysisAddIn::getIseven( sal_Int32 nVal )
{
    return ( nVal & 0x00000001 )? 0 : 1;
}

sal_Int32 SAL_CALL AnalysisAddIn::getIsodd( sal_Int32 nVal )
{
    return ( nVal & 0x00000001 )? 1 : 0;
}

double SAL_CALL
AnalysisAddIn::getMultinomial( const uno::Reference< beans::XPropertySet >& xOpt, const uno::Sequence< uno::Sequence< sal_Int32 > >& aVLst,
                               const uno::Sequence< uno::Any >& aOptVLst )
{
    ScaDoubleListGE0 aValList;

    aValList.Append( aVLst );
    aValList.Append( aAnyConv, xOpt, aOptVLst );

    if( aValList.Count() == 0 )
        return 0.0;

    double nZ = 0;
    double fRet = 1.0;

    for( sal_uInt32 i = 0; i < aValList.Count(); ++i )
    {
        const double d = aValList.Get(i);
        double n = (d >= 0.0) ? rtl::math::approxFloor( d ) : rtl::math::approxCeil( d );
        if ( n < 0.0 )
            throw lang::IllegalArgumentException();

        if( n > 0.0 )
        {
            nZ += n;
            fRet *= BinomialCoefficient(nZ, n);
        }
    }
    RETURN_FINITE( fRet );
}

double SAL_CALL AnalysisAddIn::getSeriessum( double fX, double fN, double fM, const uno::Sequence< uno::Sequence< double > >& aCoeffList )
{
    double                          fRet = 0.0;

    // #i32269# 0^0 is undefined, Excel returns #NUM! error
    if( fX == 0.0 && fN == 0 )
        throw uno::RuntimeException();

    if( fX != 0.0 )
    {
        sal_Int32       n1, n2;
        sal_Int32       nE1 = aCoeffList.getLength();
        sal_Int32       nE2;

        for( n1 = 0 ; n1 < nE1 ; n1++ )
        {
            const uno::Sequence< double >&    rList = aCoeffList[ n1 ];
            nE2 = rList.getLength();
            const double*           pList = rList.getConstArray();

            for( n2 = 0 ; n2 < nE2 ; n2++ )
            {
                fRet += pList[ n2 ] * pow( fX, fN );

                fN += fM;
            }
        }
    }

    RETURN_FINITE( fRet );
}

double SAL_CALL AnalysisAddIn::getQuotient( double fNum, double fDenom )
{
    double fRet;
    if( (fNum < 0) != (fDenom < 0) )
        fRet = ::rtl::math::approxCeil( fNum / fDenom );
    else
        fRet = ::rtl::math::approxFloor( fNum / fDenom );
    RETURN_FINITE( fRet );
}

double SAL_CALL AnalysisAddIn::getMround( double fNum, double fMult )
{
    if( fMult == 0.0 )
        return fMult;

    double fRet = fMult * ::rtl::math::round( fNum / fMult );
    RETURN_FINITE( fRet );
}

double SAL_CALL AnalysisAddIn::getSqrtpi( double fNum )
{
    double fRet = sqrt( fNum * PI );
    RETURN_FINITE( fRet );
}

double SAL_CALL AnalysisAddIn::getRandbetween( double fMin, double fMax )
{
    fMin = ::rtl::math::round( fMin, 0, rtl_math_RoundingMode_Up );
    fMax = ::rtl::math::round( fMax, 0, rtl_math_RoundingMode_Up );
    if( fMin > fMax )
        throw lang::IllegalArgumentException();

    double fRet = floor(comphelper::rng::uniform_real_distribution(fMin, nextafter(fMax+1, -DBL_MAX)));
    RETURN_FINITE( fRet );
}

double SAL_CALL AnalysisAddIn::getGcd( const uno::Reference< beans::XPropertySet >& xOpt, const uno::Sequence< uno::Sequence< double > >& aVLst, const uno::Sequence< uno::Any >& aOptVLst )
{
    ScaDoubleListGT0 aValList;

    aValList.Append( aVLst );
    aValList.Append( aAnyConv, xOpt, aOptVLst );

    if( aValList.Count() == 0 )
        return 0.0;

    double          f = aValList.Get(0);
    for( sal_uInt32 i = 1; i < aValList.Count(); ++i )
    {
        f = GetGcd( aValList.Get(i), f );
    }

    RETURN_FINITE( f );
}

double SAL_CALL AnalysisAddIn::getLcm( const uno::Reference< beans::XPropertySet >& xOpt, const uno::Sequence< uno::Sequence< double > >& aVLst, const uno::Sequence< uno::Any >& aOptVLst )
{
    ScaDoubleListGE0 aValList;

    aValList.Append( aVLst );
    aValList.Append( aAnyConv, xOpt, aOptVLst );

    if( aValList.Count() == 0 )
        return 0.0;

    double f = rtl::math::approxFloor( aValList.Get(0) );
    if( f < 0.0 )
        throw lang::IllegalArgumentException();

    if( f == 0.0 )
        return f;

    for( sal_uInt32 i = 1; i < aValList.Count(); ++i )
    {
        double fTmp = rtl::math::approxFloor( aValList.Get(i) );
        if( fTmp < 0.0 )
            throw lang::IllegalArgumentException();

        f = fTmp * f / GetGcd( fTmp, f );
        if( f == 0.0 )
            return f;
    }

    RETURN_FINITE( f );
}

double SAL_CALL AnalysisAddIn::getBesseli( double fNum, sal_Int32 nOrder )
{
    double fRet = sca::analysis::BesselI( fNum, nOrder );
    RETURN_FINITE( fRet );
}

double SAL_CALL AnalysisAddIn::getBesselj( double fNum, sal_Int32 nOrder )
{
    double fRet = sca::analysis::BesselJ( fNum, nOrder );
    RETURN_FINITE( fRet );
}

double SAL_CALL AnalysisAddIn::getBesselk( double fNum, sal_Int32 nOrder )
{
    if( nOrder < 0 || fNum <= 0.0 )
        throw lang::IllegalArgumentException();

    double fRet = sca::analysis::BesselK( fNum, nOrder );
    RETURN_FINITE( fRet );
}

double SAL_CALL AnalysisAddIn::getBessely( double fNum, sal_Int32 nOrder )
{
    if( nOrder < 0 || fNum <= 0.0 )
        throw lang::IllegalArgumentException();

    double fRet = sca::analysis::BesselY( fNum, nOrder );
    RETURN_FINITE( fRet );
}

const double    SCA_MAX2        = 511.0;            // min. val for binary numbers (9 bits + sign)
const double    SCA_MIN2        = -SCA_MAX2-1.0;    // min. val for binary numbers (9 bits + sign)
const double    SCA_MAX8        = 536870911.0;      // max. val for octal numbers (29 bits + sign)
const double    SCA_MIN8        = -SCA_MAX8-1.0;    // min. val for octal numbers (29 bits + sign)
const double    SCA_MAX16       = 549755813888.0;   // max. val for hexadecimal numbers (39 bits + sign)
const double    SCA_MIN16       = -SCA_MAX16-1.0;   // min. val for hexadecimal numbers (39 bits + sign)
const sal_Int32 SCA_MAXPLACES   = 10;               // max. number of places

OUString SAL_CALL AnalysisAddIn::getBin2Oct( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces )
{
    double fVal = ConvertToDec( aNum, 2, SCA_MAXPLACES );
    sal_Int32 nPlaces = 0;
    bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( fVal, SCA_MIN8, SCA_MAX8, 8, nPlaces, SCA_MAXPLACES, bUsePlaces );
}

double SAL_CALL AnalysisAddIn::getBin2Dec( const OUString& aNum )
{
    double fRet = ConvertToDec( aNum, 2, SCA_MAXPLACES );
    RETURN_FINITE( fRet );
}

OUString SAL_CALL AnalysisAddIn::getBin2Hex( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces )
{
    double fVal = ConvertToDec( aNum, 2, SCA_MAXPLACES );
    sal_Int32 nPlaces = 0;
    bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( fVal, SCA_MIN16, SCA_MAX16, 16, nPlaces, SCA_MAXPLACES, bUsePlaces );
}

OUString SAL_CALL AnalysisAddIn::getOct2Bin( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces )
{
    double fVal = ConvertToDec( aNum, 8, SCA_MAXPLACES );
    sal_Int32 nPlaces = 0;
    bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( fVal, SCA_MIN2, SCA_MAX2, 2, nPlaces, SCA_MAXPLACES, bUsePlaces );
}

double SAL_CALL AnalysisAddIn::getOct2Dec( const OUString& aNum )
{
    double fRet = ConvertToDec( aNum, 8, SCA_MAXPLACES );
    RETURN_FINITE( fRet );
}

OUString SAL_CALL AnalysisAddIn::getOct2Hex( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces )
{
    double fVal = ConvertToDec( aNum, 8, SCA_MAXPLACES );
    sal_Int32 nPlaces = 0;
    bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( fVal, SCA_MIN16, SCA_MAX16, 16, nPlaces, SCA_MAXPLACES, bUsePlaces );
}

OUString SAL_CALL AnalysisAddIn::getDec2Bin( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nNum, const uno::Any& rPlaces )
{
    sal_Int32 nPlaces = 0;
    bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( nNum, SCA_MIN2, SCA_MAX2, 2, nPlaces, SCA_MAXPLACES, bUsePlaces );
}

OUString SAL_CALL AnalysisAddIn::getDec2Oct( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nNum, const uno::Any& rPlaces )
{
    sal_Int32 nPlaces = 0;
    bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( nNum, SCA_MIN8, SCA_MAX8, 8, nPlaces, SCA_MAXPLACES, bUsePlaces );
}

OUString SAL_CALL AnalysisAddIn::getDec2Hex( const uno::Reference< beans::XPropertySet >& xOpt, double fNum, const uno::Any& rPlaces )
{
    sal_Int32 nPlaces = 0;
    bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( fNum, SCA_MIN16, SCA_MAX16, 16, nPlaces, SCA_MAXPLACES, bUsePlaces );
}

OUString SAL_CALL AnalysisAddIn::getHex2Bin( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces )
{
    double fVal = ConvertToDec( aNum, 16, SCA_MAXPLACES );
    sal_Int32 nPlaces = 0;
    bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( fVal, SCA_MIN2, SCA_MAX2, 2, nPlaces, SCA_MAXPLACES, bUsePlaces );
}

double SAL_CALL AnalysisAddIn::getHex2Dec( const OUString& aNum )
{
    double fRet = ConvertToDec( aNum, 16, SCA_MAXPLACES );
    RETURN_FINITE( fRet );
}

OUString SAL_CALL AnalysisAddIn::getHex2Oct( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces )
{
    double fVal = ConvertToDec( aNum, 16, SCA_MAXPLACES );
    sal_Int32 nPlaces = 0;
    bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
    return ConvertFromDec( fVal, SCA_MIN8, SCA_MAX8, 8, nPlaces, SCA_MAXPLACES, bUsePlaces );
}

sal_Int32 SAL_CALL AnalysisAddIn::getDelta( const uno::Reference< beans::XPropertySet >& xOpt, double fNum1, const uno::Any& rNum2 )
{
    return sal_Int32(fNum1 == aAnyConv.getDouble( xOpt, rNum2, 0.0 ));
}

double SAL_CALL AnalysisAddIn::getErf( const uno::Reference< beans::XPropertySet >& xOpt, double fLL, const uno::Any& rUL )
{
    double fUL, fRet;
    bool bContainsValue = aAnyConv.getDouble( fUL, xOpt, rUL );

    fRet = bContainsValue ? (Erf( fUL ) - Erf( fLL )) : Erf( fLL );
    RETURN_FINITE( fRet );
}

double SAL_CALL AnalysisAddIn::getErfc( double f )
{
    double fRet = Erfc( f );
    RETURN_FINITE( fRet );
}

sal_Int32 SAL_CALL AnalysisAddIn::getGestep( const uno::Reference< beans::XPropertySet >& xOpt, double fNum, const uno::Any& rStep )
{
    return sal_Int32(fNum >= aAnyConv.getDouble( xOpt, rStep, 0.0 ));
}

double SAL_CALL AnalysisAddIn::getFactdouble( sal_Int32 nNum )
{
    double fRet = FactDouble( nNum );
    RETURN_FINITE( fRet );
}

double SAL_CALL AnalysisAddIn::getImabs( const OUString& aNum )
{
    double fRet = Complex( aNum ).Abs();
    RETURN_FINITE( fRet );
}

double SAL_CALL AnalysisAddIn::getImaginary( const OUString& aNum )
{
    double fRet = Complex( aNum ).Imag();
    RETURN_FINITE( fRet );
}

OUString SAL_CALL AnalysisAddIn::getImpower( const OUString& aNum, double f )
{
    Complex     z( aNum );

    z.Power( f );

    return z.GetString();
}

double SAL_CALL AnalysisAddIn::getImargument( const OUString& aNum )
{
    double fRet = Complex( aNum ).Arg();
    RETURN_FINITE( fRet );
}

OUString SAL_CALL AnalysisAddIn::getImcos( const OUString& aNum )
{
    Complex     z( aNum );

    z.Cos();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImdiv( const OUString& aDivid, const OUString& aDivis )
{
    Complex     z( aDivid );

    z.Div( Complex( aDivis ) );

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImexp( const OUString& aNum )
{
    Complex     z( aNum );

    z.Exp();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImconjugate( const OUString& aNum )
{
    Complex     z( aNum );

    z.Conjugate();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImln( const OUString& aNum )
{
    Complex     z( aNum );

    z.Ln();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImlog10( const OUString& aNum )
{
    Complex     z( aNum );

    z.Log10();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImlog2( const OUString& aNum )
{
    Complex     z( aNum );

    z.Log2();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImproduct( const uno::Reference< beans::XPropertySet >&, const uno::Sequence< uno::Sequence< OUString > >& aNum1, const uno::Sequence< uno::Any >& aNL )
{
    ComplexList     z_list;

    z_list.Append( aNum1, AH_IgnoreEmpty );
    z_list.Append( aNL, AH_IgnoreEmpty );

    if( z_list.empty() )
        return Complex( 0 ).GetString();

    Complex         z = z_list.Get(0);
    for( sal_uInt32 i = 1; i < z_list.Count(); ++i )
        z.Mult( z_list.Get(i) );

    return z.GetString();
}

double SAL_CALL AnalysisAddIn::getImreal( const OUString& aNum )
{
    double fRet = Complex( aNum ).Real();
    RETURN_FINITE( fRet );
}

OUString SAL_CALL AnalysisAddIn::getImsin( const OUString& aNum )
{
    Complex     z( aNum );

    z.Sin();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImsub( const OUString& aNum1, const OUString& aNum2 )
{
    Complex     z( aNum1 );

    z.Sub( Complex( aNum2 ) );

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImsum( const uno::Reference< beans::XPropertySet >&, const uno::Sequence< uno::Sequence< OUString > >& aNum1, const uno::Sequence< uno::Any >& aFollowingPars )
{
    ComplexList     z_list;

    z_list.Append( aNum1, AH_IgnoreEmpty );
    z_list.Append( aFollowingPars, AH_IgnoreEmpty );

    if( z_list.empty() )
        return Complex( 0 ).GetString();

    Complex         z( z_list.Get(0) );
    for( sal_uInt32 i = 1; i < z_list.Count(); ++i )
        z.Add( z_list.Get(i) );

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImsqrt( const OUString& aNum )
{
    Complex     z( aNum );

    z.Sqrt();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImtan( const OUString& aNum )
{
    Complex     z( aNum );

    z.Tan();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImsec( const OUString& aNum )
{
    Complex     z( aNum );

    z.Sec();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImcsc( const OUString& aNum )
{
    Complex     z( aNum );

    z.Csc();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImcot( const OUString& aNum )
{
    Complex     z( aNum );

    z.Cot();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImsinh( const OUString& aNum )
{
    Complex     z( aNum );

    z.Sinh();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImcosh( const OUString& aNum )
{
    Complex     z( aNum );

    z.Cosh();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImsech( const OUString& aNum )
{
    Complex     z( aNum );

    z.Sech();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getImcsch( const OUString& aNum )
{
    Complex     z( aNum );

    z.Csch();

    return z.GetString();
}

OUString SAL_CALL AnalysisAddIn::getComplex( double fR, double fI, const uno::Any& rSuff )
{
    bool    bi;

    switch( rSuff.getValueTypeClass() )
    {
        case uno::TypeClass_VOID:
            bi = true;
            break;
        case uno::TypeClass_STRING:
            {
            auto   pSuff = o3tl::forceAccess<OUString>(rSuff);
            bi = *pSuff == "i" || pSuff->isEmpty();
            if( !bi && *pSuff != "j" )
                throw lang::IllegalArgumentException();
            }
            break;
        default:
            throw lang::IllegalArgumentException();
    }

    return Complex( fR, fI, bi ? 'i' : 'j' ).GetString();
}

double SAL_CALL AnalysisAddIn::getConvert( double f, const OUString& aFU, const OUString& aTU )
{
    if( !pCDL )
        pCDL.reset(new ConvertDataList());

    double fRet = pCDL->Convert( f, aFU, aTU );
    RETURN_FINITE( fRet );
}

OUString AnalysisAddIn::AnalysisResId(const char* pResId)
{
    return Translate::get(pResId, aResLocale);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */