-
Notifications
You must be signed in to change notification settings - Fork 365
Expand file tree
/
Copy pathinterpr2.cxx
More file actions
3114 lines (2872 loc) · 104 KB
/
interpr2.cxx
File metadata and controls
3114 lines (2872 loc) · 104 KB
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
/**************************************************************
*
* 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
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sc.hxx"
// INCLUDE ---------------------------------------------------------------
#include <sfx2/linkmgr.hxx>
#include <sfx2/dispatch.hxx>
#include <sfx2/objsh.hxx>
#include <svl/stritem.hxx>
#include <svl/zforlist.hxx>
#include <rtl/logfile.hxx>
#include "interpre.hxx"
#include "attrib.hxx"
#include "sc.hrc"
#include "ddelink.hxx"
#include "scmatrix.hxx"
#include "compiler.hxx"
#include "cell.hxx"
#include "document.hxx"
#include "dociter.hxx"
#include "docoptio.hxx"
#include "unitconv.hxx"
#include "globstr.hrc"
#include "hints.hxx"
#include "dpobject.hxx"
#include "postit.hxx"
#include <string.h>
#include <math.h>
#include <boost/math/special_functions/expm1.hpp>
#include <boost/math/special_functions/log1p.hpp>
using namespace formula;
// STATIC DATA -----------------------------------------------------------
#define D_TIMEFACTOR 86400.0
#define SCdEpsilon 1.0E-7
//-----------------------------------------------------------------------------
// Datum und Zeit
//-----------------------------------------------------------------------------
double ScInterpreter::GetDateSerial( sal_Int16 nYear, sal_Int16 nMonth, sal_Int16 nDay, bool bStrict )
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::GetDateSerial" );
if ( nYear < 100 && !bStrict )
nYear = pFormatter->ExpandTwoDigitYear( nYear );
// Do not use a default Date ctor here because it asks system time with a
// performance penalty.
sal_Int16 nY, nM, nD;
if (bStrict)
nY = nYear, nM = nMonth, nD = nDay;
else
{
if (nMonth > 0)
{
nY = nYear + (nMonth-1) / 12;
nM = ((nMonth-1) % 12) + 1;
}
else
{
nY = nYear + (nMonth-12) / 12;
nM = 12 - (-nMonth) % 12;
}
nD = 1;
}
Date aDate( nD, nM, nY);
if (!bStrict)
aDate += nDay - 1;
if (aDate.IsValid())
return (double) (aDate - *(pFormatter->GetNullDate()));
else
{
SetError(errNoValue);
return 0;
}
}
//-----------------------------------------------------------------------------
// Funktionen
//-----------------------------------------------------------------------------
void ScInterpreter::ScGetActDate()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetActDate" );
nFuncFmtType = NUMBERFORMAT_DATE;
Date aActDate;
long nDiff = aActDate - *(pFormatter->GetNullDate());
PushDouble((double) nDiff);
}
void ScInterpreter::ScGetActTime()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetActTime" );
nFuncFmtType = NUMBERFORMAT_DATETIME;
Date aActDate;
long nDiff = aActDate - *(pFormatter->GetNullDate());
Time aActTime;
double nTime = ((double)aActTime.Get100Sec() / 100 +
(double)(aActTime.GetSec() +
(aActTime.GetMin() * 60) +
(aActTime.GetHour() * 3600))) / D_TIMEFACTOR;
PushDouble( (double) nDiff + nTime );
}
void ScInterpreter::ScGetYear()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetYear" );
Date aDate = *(pFormatter->GetNullDate());
aDate += (long) ::rtl::math::approxFloor(GetDouble());
PushDouble( (double) aDate.GetYear() );
}
void ScInterpreter::ScGetMonth()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetMonth" );
Date aDate = *(pFormatter->GetNullDate());
aDate += (long) ::rtl::math::approxFloor(GetDouble());
PushDouble( (double) aDate.GetMonth() );
}
void ScInterpreter::ScGetDay()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetDay" );
Date aDate = *(pFormatter->GetNullDate());
aDate += (long)::rtl::math::approxFloor(GetDouble());
PushDouble((double) aDate.GetDay());
}
void ScInterpreter::ScGetMin()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetMin" );
double fTime = GetDouble();
fTime -= ::rtl::math::approxFloor(fTime); // Datumsanteil weg
long nVal = (long)::rtl::math::approxFloor(fTime*D_TIMEFACTOR+0.5) % 3600;
PushDouble( (double) (nVal/60) );
}
void ScInterpreter::ScGetSec()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetSec" );
double fTime = GetDouble();
fTime -= ::rtl::math::approxFloor(fTime); // Datumsanteil weg
long nVal = (long)::rtl::math::approxFloor(fTime*D_TIMEFACTOR+0.5) % 60;
PushDouble( (double) nVal );
}
void ScInterpreter::ScGetHour()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetHour" );
double fTime = GetDouble();
fTime -= ::rtl::math::approxFloor(fTime); // Datumsanteil weg
long nVal = (long)::rtl::math::approxFloor(fTime*D_TIMEFACTOR+0.5) / 3600;
PushDouble((double) nVal);
}
void ScInterpreter::ScGetDateValue()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetDateValue" );
String aInputString = GetString();
sal_uInt32 nFIndex = 0; // damit default Land/Spr.
double fVal;
if (pFormatter->IsNumberFormat(aInputString, nFIndex, fVal))
{
short eType = pFormatter->GetType(nFIndex);
if (eType == NUMBERFORMAT_DATE || eType == NUMBERFORMAT_DATETIME)
PushDouble(::rtl::math::approxFloor(fVal));
else
PushIllegalArgument();
}
else
PushIllegalArgument();
}
void ScInterpreter::ScGetDayOfWeek()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetDayOfWeek" );
sal_uInt8 nParamCount = GetByte();
if ( MustHaveParamCount( nParamCount, 1, 2 ) )
{
short nFlag;
if (nParamCount == 2)
nFlag = (short) ::rtl::math::approxFloor(GetDouble());
else
nFlag = 1;
Date aDate = *(pFormatter->GetNullDate());
aDate += (long)::rtl::math::approxFloor(GetDouble());
int nVal = (int) aDate.GetDayOfWeek();
if (nFlag == 1)
{
if (nVal == 6)
nVal = 1;
else
nVal += 2;
}
else if (nFlag == 2)
nVal += 1;
PushInt( nVal );
}
}
void ScInterpreter::ScGetWeekOfYear()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetWeekOfYear" );
if ( MustHaveParamCount( GetByte(), 2 ) )
{
short nFlag = (short) ::rtl::math::approxFloor(GetDouble());
Date aDate = *(pFormatter->GetNullDate());
aDate += (long)::rtl::math::approxFloor(GetDouble());
PushInt( (int) aDate.GetWeekOfYear( nFlag == 1 ? SUNDAY : MONDAY ));
}
}
void ScInterpreter::ScEasterSunday()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScEasterSunday" );
nFuncFmtType = NUMBERFORMAT_DATE;
if ( MustHaveParamCount( GetByte(), 1 ) )
{
sal_Int16 nDay, nMonth, nYear;
nYear = (sal_Int16) ::rtl::math::approxFloor( GetDouble() );
if ( nYear < 100 )
nYear = pFormatter->ExpandTwoDigitYear( nYear );
// don't worry, be happy :)
int B,C,D,E,F,G,H,I,K,L,M,N,O;
N = nYear % 19;
B = int(nYear / 100);
C = nYear % 100;
D = int(B / 4);
E = B % 4;
F = int((B + 8) / 25);
G = int((B - F + 1) / 3);
H = (19 * N + B - D - G + 15) % 30;
I = int(C / 4);
K = C % 4;
L = (32 + 2 * E + 2 * I - H - K) % 7;
M = int((N + 11 * H + 22 * L) / 451);
O = H + L - 7 * M + 114;
nDay = sal::static_int_cast<sal_Int16>( O % 31 + 1 );
nMonth = sal::static_int_cast<sal_Int16>( int(O / 31) );
PushDouble( GetDateSerial( nYear, nMonth, nDay, true ) );
}
}
void ScInterpreter::ScGetDate()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetDate" );
nFuncFmtType = NUMBERFORMAT_DATE;
if ( MustHaveParamCount( GetByte(), 3 ) )
{
sal_Int16 nDay = (sal_Int16) ::rtl::math::approxFloor(GetDouble());
sal_Int16 nMonth = (sal_Int16) ::rtl::math::approxFloor(GetDouble());
sal_Int16 nYear = (sal_Int16) ::rtl::math::approxFloor(GetDouble());
if (nYear < 0)
PushIllegalArgument();
else
{
PushDouble(GetDateSerial(nYear, nMonth, nDay, false));
}
}
}
void ScInterpreter::ScGetTime()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetTime" );
nFuncFmtType = NUMBERFORMAT_TIME;
if ( MustHaveParamCount( GetByte(), 3 ) )
{
double nSec = GetDouble();
double nMin = GetDouble();
double nHour = GetDouble();
double fTime = fmod( (nHour * 3600) + (nMin * 60) + nSec, D_TIMEFACTOR) / D_TIMEFACTOR;
if (fTime < 0)
PushIllegalArgument();
else
PushDouble( fTime);
}
}
void ScInterpreter::ScGetDiffDate()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetDiffDate" );
if ( MustHaveParamCount( GetByte(), 2 ) )
{
double nDate2 = GetDouble();
double nDate1 = GetDouble();
PushDouble(nDate1 - nDate2);
}
}
void ScInterpreter::ScGetDiffDate360()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetDiffDate360" );
/* Implementation follows
* http://www.bondmarkets.com/eCommerce/SMD_Fields_030802.pdf
* Appendix B: Day-Count Bases, there are 7 different ways to calculate the
* 30-days count. That document also claims that Excel implements the "PSA
* 30" or "NASD 30" method (funny enough they also state that Excel is the
* only tool that does so).
*
* Note that the definition given in
* http://msdn.microsoft.com/library/en-us/office97/html/SEB7C.asp
* is _not_ the way how it is actually calculated by Excel (that would not
* even match any of the 7 methods mentioned above) and would result in the
* following test cases producing wrong results according to that appendix B:
*
* 28-Feb-95 31-Aug-95 181 instead of 180
* 29-Feb-96 31-Aug-96 181 instead of 180
* 30-Jan-96 31-Mar-96 61 instead of 60
* 31-Jan-96 31-Mar-96 61 instead of 60
*
* Still, there is a difference between OOoCalc and Excel:
* In Excel:
* 02-Feb-99 31-Mar-00 results in 419
* 31-Mar-00 02-Feb-99 results in -418
* In Calc the result is 419 respectively -419. I consider the -418 a bug in Excel.
*/
sal_uInt8 nParamCount = GetByte();
if ( MustHaveParamCount( nParamCount, 2, 3 ) )
{
sal_Bool bFlag;
if (nParamCount == 3)
bFlag = GetBool();
else
bFlag = sal_False;
double nDate2 = GetDouble();
double nDate1 = GetDouble();
double fSign;
if (nGlobalError)
PushError( nGlobalError);
else
{
// #i84934# only for non-US European algorithm swap dates. Else
// follow Excel's meaningless extrapolation for "interoperability".
if (bFlag && (nDate2 < nDate1))
{
fSign = nDate1;
nDate1 = nDate2;
nDate2 = fSign;
fSign = -1.0;
}
else
fSign = 1.0;
Date aDate1 = *(pFormatter->GetNullDate());
aDate1 += (long) ::rtl::math::approxFloor(nDate1);
Date aDate2 = *(pFormatter->GetNullDate());
aDate2 += (long) ::rtl::math::approxFloor(nDate2);
if (aDate1.GetDay() == 31)
aDate1 -= (sal_uLong) 1;
else if (!bFlag)
{
if (aDate1.GetMonth() == 2)
{
switch ( aDate1.GetDay() )
{
case 28 :
if ( !aDate1.IsLeapYear() )
aDate1.SetDay(30);
break;
case 29 :
aDate1.SetDay(30);
break;
}
}
}
if (aDate2.GetDay() == 31)
{
if (!bFlag )
{
if (aDate1.GetDay() == 30)
aDate2 -= (sal_uLong) 1;
}
else
aDate2.SetDay(30);
}
PushDouble( fSign * (double)
( (double) aDate2.GetDay() + (double) aDate2.GetMonth() * 30.0 +
(double) aDate2.GetYear() * 360.0
- (double) aDate1.GetDay() - (double) aDate1.GetMonth() * 30.0
- (double)aDate1.GetYear() * 360.0) );
}
}
}
void ScInterpreter::ScGetTimeValue()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetTimeValue" );
String aInputString = GetString();
sal_uInt32 nFIndex = 0; // damit default Land/Spr.
double fVal;
if (pFormatter->IsNumberFormat(aInputString, nFIndex, fVal))
{
short eType = pFormatter->GetType(nFIndex);
if (eType == NUMBERFORMAT_TIME || eType == NUMBERFORMAT_DATETIME)
{
double fDateVal = rtl::math::approxFloor(fVal);
double fTimeVal = fVal - fDateVal;
PushDouble(fTimeVal);
}
else
PushIllegalArgument();
}
else
PushIllegalArgument();
}
void ScInterpreter::ScPlusMinus()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScPlusMinus" );
double nVal = GetDouble();
short n = 0;
if (nVal < 0.0)
n = -1;
else if (nVal > 0.0)
n = 1;
PushInt( n );
}
void ScInterpreter::ScAbs()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScAbs" );
PushDouble(fabs(GetDouble()));
}
void ScInterpreter::ScInt()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScInt" );
PushDouble(::rtl::math::approxFloor(GetDouble()));
}
void ScInterpreter::RoundNumber( rtl_math_RoundingMode eMode )
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::RoundNumber" );
sal_uInt8 nParamCount = GetByte();
if ( MustHaveParamCount( nParamCount, 1, 2 ) )
{
double fVal = 0.0;
if (nParamCount == 1)
fVal = ::rtl::math::round( GetDouble(), 0, eMode );
else
{
sal_Int32 nDec = (sal_Int32) ::rtl::math::approxFloor(GetDouble());
if( nDec < -20 || nDec > 20 )
PushIllegalArgument();
else
fVal = ::rtl::math::round( GetDouble(), (short)nDec, eMode );
}
PushDouble(fVal);
}
}
void ScInterpreter::ScRound()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScRound" );
RoundNumber( rtl_math_RoundingMode_Corrected );
}
void ScInterpreter::ScRoundDown()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScRoundDown" );
RoundNumber( rtl_math_RoundingMode_Down );
}
void ScInterpreter::ScRoundUp()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScRoundUp" );
RoundNumber( rtl_math_RoundingMode_Up );
}
void ScInterpreter::ScCeil()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScCeil" );
sal_uInt8 nParamCount = GetByte();
if ( MustHaveParamCount( nParamCount, 2, 3 ) )
{
sal_Bool bAbs = ( nParamCount == 3 ? GetBool() : sal_False );
double fDec = GetDouble();
double fVal = GetDouble();
if ( fDec == 0.0 )
PushInt(0);
else if (fVal*fDec < 0.0)
PushIllegalArgument();
else
{
if ( !bAbs && fVal < 0.0 )
PushDouble(::rtl::math::approxFloor(fVal/fDec) * fDec);
else
PushDouble(::rtl::math::approxCeil(fVal/fDec) * fDec);
}
}
}
void ScInterpreter::ScFloor()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScFloor" );
sal_uInt8 nParamCount = GetByte();
if ( MustHaveParamCount( nParamCount, 2, 3 ) )
{
sal_Bool bAbs = ( nParamCount == 3 ? GetBool() : sal_False );
double fDec = GetDouble();
double fVal = GetDouble();
if ( fDec == 0.0 )
PushInt(0);
else if (fVal*fDec < 0.0)
PushIllegalArgument();
else
{
if ( !bAbs && fVal < 0.0 )
PushDouble(::rtl::math::approxCeil(fVal/fDec) * fDec);
else
PushDouble(::rtl::math::approxFloor(fVal/fDec) * fDec);
}
}
}
void ScInterpreter::ScEven()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScEven" );
double fVal = GetDouble();
if (fVal < 0.0)
PushDouble(::rtl::math::approxFloor(fVal/2.0) * 2.0);
else
PushDouble(::rtl::math::approxCeil(fVal/2.0) * 2.0);
}
void ScInterpreter::ScOdd()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScOdd" );
double fVal = GetDouble();
if (fVal >= 0.0)
{
fVal = ::rtl::math::approxCeil(fVal);
if (fmod(fVal, 2.0) == 0.0)
fVal += 1.0;
}
else
{
fVal = ::rtl::math::approxFloor(fVal);
if (fmod(fVal, 2.0) == 0.0)
fVal -= 1.0;
}
PushDouble(fVal);
}
void ScInterpreter::ScArcTan2()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScArcTan2" );
if ( MustHaveParamCount( GetByte(), 2 ) )
{
double nVal2 = GetDouble();
double nVal1 = GetDouble();
PushDouble(atan2(nVal2, nVal1));
}
}
void ScInterpreter::ScLog()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScLog" );
sal_uInt8 nParamCount = GetByte();
if ( MustHaveParamCount( nParamCount, 1, 2 ) )
{
double nBase;
if (nParamCount == 2)
nBase = GetDouble();
else
nBase = 10.0;
double nVal = GetDouble();
if (nVal > 0.0 && nBase > 0.0 && nBase != 1.0)
PushDouble(log(nVal) / log(nBase));
else
PushIllegalArgument();
}
}
void ScInterpreter::ScLn()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScLn" );
double fVal = GetDouble();
if (fVal > 0.0)
PushDouble(log(fVal));
else
PushIllegalArgument();
}
void ScInterpreter::ScLog10()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScLog10" );
double fVal = GetDouble();
if (fVal > 0.0)
PushDouble(log10(fVal));
else
PushIllegalArgument();
}
void ScInterpreter::ScNPV()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScNPV" );
nFuncFmtType = NUMBERFORMAT_CURRENCY;
short nParamCount = GetByte();
if ( MustHaveParamCount( nParamCount, 2, 31 ) )
{
double nVal = 0.0;
// Wir drehen den Stack um!!
FormulaToken* pTemp[ 31 ];
for( short i = 0; i < nParamCount; i++ )
pTemp[ i ] = pStack[ sp - i - 1 ];
memcpy( &pStack[ sp - nParamCount ], pTemp, nParamCount * sizeof( FormulaToken* ) );
if (nGlobalError == 0)
{
double nCount = 1.0;
double nZins = GetDouble();
--nParamCount;
size_t nRefInList = 0;
ScRange aRange;
while (nParamCount-- > 0)
{
switch (GetStackType())
{
case svDouble :
{
nVal += (GetDouble() / pow(1.0 + nZins, (double)nCount));
nCount++;
}
break;
case svSingleRef :
{
ScAddress aAdr;
PopSingleRef( aAdr );
ScBaseCell* pCell = GetCell( aAdr );
if (!HasCellEmptyData(pCell) && HasCellValueData(pCell))
{
double nCellVal = GetCellValue( aAdr, pCell );
nVal += (nCellVal / pow(1.0 + nZins, (double)nCount));
nCount++;
}
}
break;
case svDoubleRef :
case svRefList :
{
sal_uInt16 nErr = 0;
double nCellVal;
PopDoubleRef( aRange, nParamCount, nRefInList);
ScHorizontalValueIterator aValIter( pDok, aRange, glSubTotal);
while ((nErr == 0) && aValIter.GetNext(nCellVal, nErr))
{
nVal += (nCellVal / pow(1.0 + nZins, (double)nCount));
nCount++;
}
if ( nErr != 0 )
SetError(nErr);
}
break;
default : SetError(errIllegalParameter); break;
}
}
}
PushDouble(nVal);
}
}
void ScInterpreter::ScDateDif()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDateDif");
if ( MustHaveParamCount( GetByte(), 3 ) )
{
String ReturnFormat = GetString();
double StartDate = GetDouble();
Date tmpDate = *( pFormatter->GetNullDate() );
tmpDate += (long) ::rtl::math::approxFloor( tmpDate );
sal_Int32 StartDays = tmpDate.GetDay();
sal_Int32 StartMonth = tmpDate.GetMonth();
sal_Int32 StartYear = tmpDate.GetYear();
double EndDate = GetDouble();
tmpDate = *( pFormatter->GetNullDate() );
tmpDate += (long) ::rtl::math::approxFloor( EndDate );
sal_Int32 EndDays = tmpDate.GetDay();
sal_Int32 EndMonth = tmpDate.GetMonth();
sal_Int32 EndYear = tmpDate.GetYear();
switch(ReturnFormat.ToLowerAscii()) {
case d:
// return number of days
PushDouble( EndDate - StartDate);
break;
case m:
// return number of months
sal_Int32 diffMonth = EndMonth - StartMonth + 12 * (EndYear - StartYear);
if (StartDate > EndDate && StartDays > EndDays )
{
diffMonth -= 1;
}
else if ( EndDays > StartDays )
{
diffMonth += 1;
}
PushInt(diffMonth);
break;
case ym:
// return number of months, ignoring years
sal_Int32 diffMonth = EndMonth - StartMonth;
if (StartDate > EndDate && StartDays > EndDays )
{
diffMonth -= 1;
}
else if ( EndMonth > StartMonth && StartDays > EndDays )
{
diffMonth += 1;
}
PushInt(diffMonth);
break;
case md:
// return number of days, ignoring months and years
tmpDate = Date(EndDays,StartMonth,StartYear)
double sameYear = double(tmpDate - *( pFormatter->GetNullDate() );
PushDouble( fabs(sameYear - StartDate));
break;
case yd:
// return number of days, ignoring years
tmpDate = Date(EndDays,EndMonth,StartYear)
double sameYear = double(tmpDate - *( pFormatter->GetNullDate() );
PushDouble( fabs(sameYear - StartDate));
break;
case y:
// return number of years
sal_Int32 diffYears = EndYear - StartYear;
if (EndYear > StartYear) {
if((EndMonth = StartMonth && StartDate >= EndDate) || (EndMonth > StartMonth))
diffYears -= 1;
}
else if ((EndMonth = StartMonth && EndDate >= StartDate) || (StartMonth > EndMonth))
diffYears += 1;
PushInt(diffYears);
break;
default:
// unsupported format
PushIllegalArgument();
}
}
}
void ScInterpreter::ScIRR()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIRR" );
double fSchaetzwert;
nFuncFmtType = NUMBERFORMAT_PERCENT;
sal_uInt8 nParamCount = GetByte();
if ( !MustHaveParamCount( nParamCount, 1, 2 ) )
return;
if (nParamCount == 2)
fSchaetzwert = GetDouble();
else
fSchaetzwert = 0.1;
sal_uInt16 sPos = sp; // Stack-Position merken
double fEps = 1.0;
double x, xNeu, fWert, fZaehler, fNenner, nCount;
if (fSchaetzwert == -1.0)
x = 0.1; // default gegen Nulldivisionen
else
x = fSchaetzwert; // Startwert
switch (GetStackType())
{
case svDoubleRef :
break;
default:
{
PushIllegalParameter();
return;
}
}
const sal_uInt16 nIterationsMax = 20;
sal_uInt16 nItCount = 0;
ScRange aRange;
while (fEps > SCdEpsilon && nItCount < nIterationsMax)
{ // Newton-Verfahren:
sp = sPos; // Stack zuruecksetzen
nCount = 0.0;
fZaehler = 0.0;
fNenner = 0.0;
sal_uInt16 nErr = 0;
PopDoubleRef( aRange );
ScValueIterator aValIter(pDok, aRange, glSubTotal);
if (aValIter.GetFirst(fWert, nErr))
{
fZaehler += fWert / pow(1.0+x,(double)nCount);
fNenner += -nCount * fWert / pow(1.0+x,nCount+1.0);
nCount++;
while ((nErr == 0) && aValIter.GetNext(fWert, nErr))
{
fZaehler += fWert / pow(1.0+x,(double)nCount);
fNenner += -nCount * fWert / pow(1.0+x,nCount+1.0);
nCount++;
}
SetError(nErr);
}
xNeu = x - fZaehler / fNenner; // x(i+1) = x(i)-f(x(i))/f'(x(i))
nItCount++;
fEps = fabs(xNeu - x);
x = xNeu;
}
if (fSchaetzwert == 0.0 && fabs(x) < SCdEpsilon)
x = 0.0; // auf Null normieren
if (fEps < SCdEpsilon)
PushDouble(x);
else
PushError( errNoConvergence);
}
void ScInterpreter::ScMIRR()
{ // range_of_values ; rate_invest ; rate_reinvest
nFuncFmtType = NUMBERFORMAT_PERCENT;
if( MustHaveParamCount( GetByte(), 3 ) )
{
double fRate1_reinvest = GetDouble() + 1;
double fNPV_reinvest = 0.0;
double fPow_reinvest = 1.0;
double fRate1_invest = GetDouble() + 1;
double fNPV_invest = 0.0;
double fPow_invest = 1.0;
ScRange aRange;
PopDoubleRef( aRange );
if( nGlobalError )
PushError( nGlobalError);
else
{
ScValueIterator aValIter( pDok, aRange, glSubTotal );
double fCellValue;
sal_uLong nCount = 0;
sal_uInt16 nIterError = 0;
sal_Bool bLoop = aValIter.GetFirst( fCellValue, nIterError );
while( bLoop )
{
if( fCellValue > 0.0 ) // reinvestments
fNPV_reinvest += fCellValue * fPow_reinvest;
else if( fCellValue < 0.0 ) // investments
fNPV_invest += fCellValue * fPow_invest;
fPow_reinvest /= fRate1_reinvest;
fPow_invest /= fRate1_invest;
nCount++;
bLoop = aValIter.GetNext( fCellValue, nIterError );
}
if( nIterError )
PushError( nIterError );
else
{
double fResult = -fNPV_reinvest / fNPV_invest;
fResult *= pow( fRate1_reinvest, (double) nCount - 1 );
fResult = pow( fResult, 1.0 / (nCount - 1) );
PushDouble( fResult - 1.0 );
}
}
}
}
void ScInterpreter::ScISPMT()
{ // rate ; period ; total_periods ; invest
if( MustHaveParamCount( GetByte(), 4 ) )
{
double fInvest = GetDouble();
double fTotal = GetDouble();
double fPeriod = GetDouble();
double fRate = GetDouble();
if( nGlobalError )
PushError( nGlobalError);
else
PushDouble( fInvest * fRate * (fPeriod / fTotal - 1.0) );
}
}
//----------------------- Finanzfunktionen ------------------------------------
double ScInterpreter::ScGetBw(double fZins, double fZzr, double fRmz,
double fZw, double fF)
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScMIRR" );
double fBw;
if (fZins == 0.0)
fBw = fZw + fRmz * fZzr;
else if (fF > 0.0)
fBw = (fZw * pow(1.0 + fZins, -fZzr))
+ (fRmz * (1.0 - pow(1.0 + fZins, -fZzr + 1.0)) / fZins)
+ fRmz;
else
fBw = (fZw * pow(1.0 + fZins, -fZzr))
+ (fRmz * (1.0 - pow(1.0 + fZins, -fZzr)) / fZins);
return -fBw;
}
void ScInterpreter::ScBW()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScBW" );
nFuncFmtType = NUMBERFORMAT_CURRENCY;
double nRmz, nZzr, nZins, nZw = 0, nFlag = 0;
sal_uInt8 nParamCount = GetByte();
if ( !MustHaveParamCount( nParamCount, 3, 5 ) )
return;
if (nParamCount == 5)
nFlag = GetDouble();
if (nParamCount >= 4)
nZw = GetDouble();
nRmz = GetDouble();
nZzr = GetDouble();
nZins = GetDouble();
PushDouble(ScGetBw(nZins, nZzr, nRmz, nZw, nFlag));
}
void ScInterpreter::ScDIA()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScDIA" );
nFuncFmtType = NUMBERFORMAT_CURRENCY;
if ( MustHaveParamCount( GetByte(), 4 ) )
{
double nZr = GetDouble();
double nDauer = GetDouble();
double nRest = GetDouble();
double nWert = GetDouble();
double nDia = ((nWert - nRest) * (nDauer - nZr + 1.0)) /
((nDauer * (nDauer + 1.0)) / 2.0);
PushDouble(nDia);
}
}
double ScInterpreter::ScGetGDA(double fWert, double fRest, double fDauer,
double fPeriode, double fFaktor)
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGetGDA" );
double fGda, fZins, fAlterWert, fNeuerWert;
fZins = fFaktor / fDauer;
if (fZins >= 1.0)
{
fZins = 1.0;
if (fPeriode == 1.0)
fAlterWert = fWert;
else
fAlterWert = 0.0;
}
else
fAlterWert = fWert * pow(1.0 - fZins, fPeriode - 1.0);
fNeuerWert = fWert * pow(1.0 - fZins, fPeriode);
if (fNeuerWert < fRest)
fGda = fAlterWert - fRest;
else
fGda = fAlterWert - fNeuerWert;
if (fGda < 0.0)
fGda = 0.0;
return fGda;
}
void ScInterpreter::ScGDA()
{
RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScGDA" );
nFuncFmtType = NUMBERFORMAT_CURRENCY;
sal_uInt8 nParamCount = GetByte();
if ( MustHaveParamCount( nParamCount, 4, 5 ) )
{
double nFaktor;
if (nParamCount == 5)
nFaktor = GetDouble();
else
nFaktor = 2.0;