xref: /aoo41x/main/sc/source/core/inc/interpre.hxx (revision 61e64f4a)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #ifndef SC_INTERPRE_HXX
23 #define SC_INTERPRE_HXX
24 
25 #include <math.h>
26 #include <rtl/math.hxx>
27 #include "formula/errorcodes.hxx"
28 #include "cell.hxx"
29 #include "scdll.hxx"
30 #include "document.hxx"
31 #include "scmatrix.hxx"
32 
33 #include <math.h>
34 #include <map>
35 
36 class ScDocument;
37 class SbxVariable;
38 class ScBaseCell;
39 class ScFormulaCell;
40 class SvNumberFormatter;
41 class ScDBRangeBase;
42 struct MatrixDoubleOp;
43 struct ScQueryParam;
44 struct ScDBQueryParamBase;
45 
46 struct ScCompare
47 {
48     double  nVal[2];
49     String* pVal[2];
50     sal_Bool    bVal[2];
51     sal_Bool    bEmpty[2];
52         ScCompare( String* p1, String* p2 )
53         {
54             pVal[ 0 ] = p1;
55             pVal[ 1 ] = p2;
56             bEmpty[0] = sal_False;
57             bEmpty[1] = sal_False;
58         }
59 };
60 
61 struct ScCompareOptions
62 {
63     ScQueryEntry        aQueryEntry;
64     bool                bRegEx;
65     bool                bMatchWholeCell;
66     bool                bIgnoreCase;
67 
68                         ScCompareOptions( ScDocument* pDoc, const ScQueryEntry& rEntry, bool bReg );
69 private:
70                         // Not implemented, prevent usage.
71                         ScCompareOptions();
72                         ScCompareOptions( const ScCompareOptions & );
73      ScCompareOptions&  operator=( const ScCompareOptions & );
74 };
75 
76 class ScToken;
77 
78 #define MAXSTACK      (4096 / sizeof(formula::FormulaToken*))
79 
80 class ScTokenStack
81 {
82 public:
83     DECL_FIXEDMEMPOOL_NEWDEL( ScTokenStack )
84     formula::FormulaToken* pPointer[ MAXSTACK ];
85 };
86 
87 enum ScIterFunc
88 {
89     ifSUM,     // Sum
90     ifSUMSQ,   // Sum squares
91     ifPRODUCT, // Product
92     ifAVERAGE, // Average
93     ifCOUNT,   // Count
94     ifCOUNT2,  // Count non-empty
95     ifMIN,     // Minimum
96     ifMAX      // Maximum
97 };
98 
99 enum ScIterFuncIf
100 {
101     ifSUMIF,    // Conditional sum
102     ifAVERAGEIF // Conditional average
103 };
104 
105 enum ScIterFuncIfs
106 {
107     ifSUMIFS,     // Multi-Conditional sum
108     ifAVERAGEIFS, // Multi-Conditional average
109     ifCOUNTIFS    // Multi-Conditional count
110 };
111 
112 struct FormulaTokenRef_less
113 {
114     bool operator () ( const formula::FormulaConstTokenRef& r1, const formula::FormulaConstTokenRef& r2 ) const
115         { return &r1 < &r2; }
116 };
117 typedef ::std::map< const formula::FormulaConstTokenRef, formula::FormulaTokenRef, FormulaTokenRef_less> ScTokenMatrixMap;
118 
119 class ScInterpreter
120 {
121     // distibution function objects need the GetxxxDist methods
122     friend class ScGammaDistFunction;
123     friend class ScBetaDistFunction;
124     friend class ScTDistFunction;
125     friend class ScFDistFunction;
126     friend class ScChiDistFunction;
127     friend class ScChiSqDistFunction;
128 
129 public:
130     DECL_FIXEDMEMPOOL_NEWDEL( ScInterpreter )
131 
132     static void GlobalExit();           // aus ScGlobal::Clear() gerufen
133 
134     /// Could string be a regular expression?
135     /// If pDoc!=NULL the document options are taken into account and if
136     /// RegularExpressions are disabled the function returns sal_False regardless
137     /// of the string content.
138     static sal_Bool MayBeRegExp( const String& rStr, const ScDocument* pDoc );
139 
140     /// Fail safe division, returning an errDivisionByZero coded into a double
141     /// if denominator is 0.0
142     static inline double div( const double& fNumerator, const double& fDenominator );
143 
144     ScMatrixRef GetNewMat(SCSIZE nC, SCSIZE nR);
145 private:
146     static ScTokenStack*    pGlobalStack;
147     static sal_Bool         bGlobalStackInUse;
148 
149     formula::FormulaTokenIterator aCode;
150     ScAddress   aPos;
151     ScTokenArray& rArr;
152     ScDocument* pDok;
153     formula::FormulaTokenRef  xResult;
154     ScJumpMatrix*   pJumpMatrix;        // currently active array condition, if any
155     ScTokenMatrixMap* pTokenMatrixMap;  // map ScToken* to formula::FormulaTokenRef if in array condition
156     ScFormulaCell* pMyFormulaCell;      // the cell of this formula expression
157     SvNumberFormatter* pFormatter;
158 
159     const formula::FormulaToken*
160                 pCur;                // current token
161     ScToken*    pLastStackRefToken;     // i120962: current valid reference token
162     bool        bRefFunc;               // i120962: is a reference function
163     String      aTempStr;               // for GetString()
164     ScTokenStack* pStackObj;            // contains the stacks
165     formula::FormulaToken**   pStack;                 // the current stack
166     sal_uInt16      nGlobalError;           // global (local to this formula expression) error
167     sal_uInt16      sp;                     // stack pointer
168     sal_uInt16      maxsp;                  // the maximal used stack pointer
169     sal_uLong       nFuncFmtIndex;          // NumberFormatIndex of a function
170     sal_uLong       nCurFmtIndex;           // current NumberFormatIndex
171     sal_uLong       nRetFmtIndex;           // NumberFormatIndex of an expression, if any
172     short       nFuncFmtType;           // NumberFormatType of a function
173     short       nCurFmtType;            // current NumberFormatType
174     short       nRetFmtType;            // NumberFormatType of an expression
175     sal_uInt16      mnStringNoValueError;   // the error set in ConvertStringToValue() if no value
176     sal_Bool        glSubTotal;             // flag for subtotal functions
177     sal_uInt8        cPar;                   // current count of parameters
178     sal_Bool        bCalcAsShown;           // precision as shown
179     sal_Bool        bMatrixFormula;         // formula cell is a matrix formula
180 
181 //---------------------------------Funktionen in interpre.cxx---------
182 // nMust <= nAct <= nMax ? ok : PushError
183 inline sal_Bool MustHaveParamCount( short nAct, short nMust );
184 inline sal_Bool MustHaveParamCount( short nAct, short nMust, short nMax );
185 inline sal_Bool MustHaveParamCountMin( short nAct, short nMin );
186 void PushParameterExpected();
187 void PushIllegalParameter();
188 void PushIllegalArgument();
189 void PushNoValue();
190 void PushNA();
191 //-------------------------------------------------------------------------
192 // Funktionen fuer den Zugriff auf das Document
193 //-------------------------------------------------------------------------
194 void ReplaceCell( ScAddress& );     // for TableOp
195 void ReplaceCell( SCCOL& rCol, SCROW& rRow, SCTAB& rTab );  // for TableOp
196 sal_Bool IsTableOpInRange( const ScRange& );
197 sal_uLong GetCellNumberFormat( const ScAddress&, const ScBaseCell* );
198 double ConvertStringToValue( const String& );
199 double GetCellValue( const ScAddress&, const ScBaseCell* );
200 double GetCellValueOrZero( const ScAddress&, const ScBaseCell* );
201 double GetValueCellValue( const ScAddress&, const ScValueCell* );
202 ScBaseCell* GetCell( const ScAddress& rPos )
203     { return pDok->GetCell( rPos ); }
204 void GetCellString( String& rStr, const ScBaseCell* pCell );
205 inline sal_uInt16 GetCellErrCode( const ScBaseCell* pCell )
206     { return pCell ? pCell->GetErrorCode() : 0; }
207 inline CellType GetCellType( const ScBaseCell* pCell )
208     { return pCell ? pCell->GetCellType() : CELLTYPE_NONE; }
209 /// Really empty or inherited emptiness.
210 inline sal_Bool HasCellEmptyData( const ScBaseCell* pCell )
211     { return pCell ? pCell->HasEmptyData() : sal_True; }
212 /// This includes inherited emptiness, which usually is regarded as value!
213 inline sal_Bool HasCellValueData( const ScBaseCell* pCell )
214     { return pCell ? pCell->HasValueData() : sal_False; }
215 /// Not empty and not value.
216 inline sal_Bool HasCellStringData( const ScBaseCell* pCell )
217     { return pCell ? pCell->HasStringData() : sal_False; }
218 
219 sal_Bool CreateDoubleArr(SCCOL nCol1, SCROW nRow1, SCTAB nTab1,
220                      SCCOL nCol2, SCROW nRow2, SCTAB nTab2, sal_uInt8* pCellArr);
221 sal_Bool CreateStringArr(SCCOL nCol1, SCROW nRow1, SCTAB nTab1,
222                      SCCOL nCol2, SCROW nRow2, SCTAB nTab2, sal_uInt8* pCellArr);
223 sal_Bool CreateCellArr(SCCOL nCol1, SCROW nRow1, SCTAB nTab1,
224                    SCCOL nCol2, SCROW nRow2, SCTAB nTab2, sal_uInt8* pCellArr);
225 
226 //-----------------------------------------------------------------------------
227 // Stack operations
228 //-----------------------------------------------------------------------------
229 
230 /** Does substitute with formula::FormulaErrorToken in case nGlobalError is set and the token
231     passed is not formula::FormulaErrorToken.
232     Increments RefCount of the original token if not substituted. */
233 void Push( formula::FormulaToken& r );
234 
235 /** Does not substitute with formula::FormulaErrorToken in case nGlobalError is set.
236     Used to push RPN tokens or from within Push() or tokens that are already
237     explicit formula::FormulaErrorToken. Increments RefCount. */
238 void PushWithoutError( formula::FormulaToken& r );
239 
240 /** Clones the token to be pushed or substitutes with formula::FormulaErrorToken if
241     nGlobalError is set and the token passed is not formula::FormulaErrorToken. */
242 void PushTempToken( const formula::FormulaToken& );
243 
244 /** Does substitute with formula::FormulaErrorToken in case nGlobalError is set and the token
245     passed is not formula::FormulaErrorToken.
246     Increments RefCount of the original token if not substituted.
247     ATTENTION! The token had to be allocated with `new' and must not be used
248     after this call if no RefCount was set because possibly it gets immediately
249     deleted in case of an errStackOverflow or if substituted with formula::FormulaErrorToken! */
250 void PushTempToken( formula::FormulaToken* );
251 
252 /** Does not substitute with formula::FormulaErrorToken in case nGlobalError is set.
253     Used to push tokens from within PushTempToken() or tokens that are already
254     explicit formula::FormulaErrorToken. Increments RefCount.
255     ATTENTION! The token had to be allocated with `new' and must not be used
256     after this call if no RefCount was set because possibly it gets immediately
257     decremented again and thus deleted in case of an errStackOverflow! */
258 void PushTempTokenWithoutError( formula::FormulaToken* );
259 
260 /** If nGlobalError is set push formula::FormulaErrorToken.
261     If nGlobalError is not set do nothing.
262     Used in PushTempToken() and alike to simplify handling.
263     @return: <TRUE/> if nGlobalError. */
264 inline bool IfErrorPushError()
265 {
266     if (nGlobalError)
267     {
268         PushTempTokenWithoutError( new formula::FormulaErrorToken( nGlobalError));
269         return true;
270     }
271     return false;
272 }
273 
274 /** Obtain cell result / content from address and push as temp token.
275     bDisplayEmptyAsString is passed to ScEmptyCell in case of an empty cell
276     result. Also obtain number format and type if _both_, type and index
277     pointer, are not NULL. */
278 void PushCellResultToken( bool bDisplayEmptyAsString, const ScAddress & rAddress,
279         short * pRetTypeExpr, sal_uLong * pRetIndexExpr );
280 
281 formula::FormulaTokenRef PopToken();
282 void Pop();
283 void PopError();
284 double PopDouble();
285 const String& PopString();
286 void ValidateRef( const ScSingleRefData & rRef );
287 void ValidateRef( const ScComplexRefData & rRef );
288 void ValidateRef( const ScRefList & rRefList );
289 void SingleRefToVars( const ScSingleRefData & rRef, SCCOL & rCol, SCROW & rRow, SCTAB & rTab );
290 void PopSingleRef( ScAddress& );
291 void PopSingleRef(SCCOL& rCol, SCROW &rRow, SCTAB& rTab);
292 void DoubleRefToRange( const ScComplexRefData&, ScRange&, sal_Bool bDontCheckForTableOp = sal_False );
293 /** If formula::StackVar formula::svDoubleRef pop ScDoubleRefToken and return values of
294     ScComplexRefData.
295     Else if StackVar svRefList return values of the ScComplexRefData where
296     rRefInList is pointing to. rRefInList is incremented. If rRefInList was the
297     last element in list pop ScRefListToken and set rRefInList to 0, else
298     rParam is incremented (!) to allow usage as in
299     while(nParamCount--) PopDoubleRef(aRange,nParamCount,nRefInList);
300   */
301 void PopDoubleRef( ScRange & rRange, short & rParam, size_t & rRefInList );
302 void PopDoubleRef( ScRange&, sal_Bool bDontCheckForTableOp = sal_False );
303 void DoubleRefToVars( const ScToken* p,
304         SCCOL& rCol1, SCROW &rRow1, SCTAB& rTab1,
305         SCCOL& rCol2, SCROW &rRow2, SCTAB& rTab2,
306         sal_Bool bDontCheckForTableOp = sal_False );
307 ScDBRangeBase* PopDoubleRef();
308 void PopDoubleRef(SCCOL& rCol1, SCROW &rRow1, SCTAB& rTab1,
309                           SCCOL& rCol2, SCROW &rRow2, SCTAB& rTab2,
310                           sal_Bool bDontCheckForTableOp = sal_False );
311 sal_Bool PopDoubleRefOrSingleRef( ScAddress& rAdr );
312 void PopDoubleRefPushMatrix();
313 // If MatrixFormula: convert formula::svDoubleRef to svMatrix, create JumpMatrix.
314 // Else convert area reference parameters marked as ForceArray to array.
315 // Returns sal_True if JumpMatrix created.
316 bool ConvertMatrixParameters();
317 inline void MatrixDoubleRefToMatrix();      // if MatrixFormula: PopDoubleRefPushMatrix
318 // If MatrixFormula or ForceArray: ConvertMatrixParameters()
319 inline bool MatrixParameterConversion();
320 ScMatrixRef PopMatrix();
321 //void PushByte(sal_uInt8 nVal);
322 void PushDouble(double nVal);
323 void PushInt( int nVal );
324 void PushStringBuffer( const sal_Unicode* pString );
325 void PushString( const String& rString );
326 void PushSingleRef(SCCOL nCol, SCROW nRow, SCTAB nTab);
327 void PushDoubleRef(SCCOL nCol1, SCROW nRow1, SCTAB nTab1,
328                                  SCCOL nCol2, SCROW nRow2, SCTAB nTab2);
329 void PushMatrix(ScMatrix* pMat);
330 void PushError( sal_uInt16 nError );
331 /// Raw stack type without default replacements.
332 formula::StackVar GetRawStackType();
333 /// Stack type with replacement of defaults, e.g. svMissing and formula::svEmptyCell will result in formula::svDouble.
334 formula::StackVar GetStackType();
335 // peek StackType of Parameter, Parameter 1 == TOS, 2 == TOS-1, ...
336 formula::StackVar GetStackType( sal_uInt8 nParam );
337 sal_uInt8 GetByte() { return cPar; }
338 // generiert aus DoubleRef positionsabhaengige SingleRef
339 sal_Bool DoubleRefToPosSingleRef( const ScRange& rRange, ScAddress& rAdr );
340 double GetDouble();
341 double GetDoubleWithDefault(double nDefault);
342 sal_Bool IsMissing();
343 sal_Bool GetBool() { return GetDouble() != 0.0; }
344 const String& GetString();
345 // pop matrix and obtain one element, upper left or according to jump matrix
346 ScMatValType GetDoubleOrStringFromMatrix( double& rDouble, String& rString );
347 ScMatrixRef CreateMatrixFromDoubleRef( const formula::FormulaToken* pToken,
348         SCCOL nCol1, SCROW nRow1, SCTAB nTab1,
349         SCCOL nCol2, SCROW nRow2, SCTAB nTab2 );
350 inline ScTokenMatrixMap& GetTokenMatrixMap();
351 ScTokenMatrixMap* CreateTokenMatrixMap();
352 ScMatrixRef GetMatrix();
353 void ScTableOp();                                       // Mehrfachoperationen
354 void ScErrCell();                                       // Sonderbehandlung
355                                                         // Fehlerzelle
356 //-----------------------------allgemeine Hilfsfunktionen
357 void SetMaxIterationCount(sal_uInt16 n);
358 inline void CurFmtToFuncFmt()
359     { nFuncFmtType = nCurFmtType; nFuncFmtIndex = nCurFmtIndex; }
360 // Check for String overflow of rResult+rAdd and set error and erase rResult
361 // if so. Return sal_True if ok, sal_False if overflow
362 inline sal_Bool CheckStringResultLen( String& rResult, const String& rAdd );
363 // Set error according to rVal, and set rVal to 0.0 if there was an error.
364 inline void TreatDoubleError( double& rVal );
365 // Lookup using ScLookupCache, @returns sal_True if found and result address
366 bool LookupQueryWithCache( ScAddress & o_rResultPos,
367         const ScQueryParam & rParam ) const;
368 
369 //---------------------------------Funktionen in interpr1.cxx---------
370 void ScIfJump();
371 void ScChoseJump();
372 
373 // Be sure to only call this if pStack[sp-nStackLevel] really contains a
374 // ScJumpMatrixToken, no further checks are applied!
375 // Returns true if last jump was executed and result matrix pushed.
376 bool JumpMatrix( short nStackLevel );
377 
378 /** @param pOptions
379         NULL means case sensitivity document option is to be used!
380  */
381 double CompareFunc( const ScCompare& rComp, ScCompareOptions* pOptions = NULL );
382 double Compare();
383 /** @param pOptions
384         NULL means case sensitivity document option is to be used!
385  */
386 ScMatrixRef CompareMat( ScCompareOptions* pOptions = NULL );
387 ScMatrixRef QueryMat( ScMatrix* pMat, ScCompareOptions& rOptions );
388 void ScEqual();
389 void ScNotEqual();
390 void ScLess();
391 void ScGreater();
392 void ScLessEqual();
393 void ScGreaterEqual();
394 void ScAnd();
395 void ScOr();
396 void ScXor();
397 void ScNot();
398 void ScNeg();
399 void ScPercentSign();
400 void ScIntersect();
401 void ScRangeFunc();
402 void ScUnionFunc();
403 void ScPi();
404 void ScRandom();
405 void ScTrue();
406 void ScFalse();
407 void ScDeg();
408 void ScRad();
409 void ScSin();
410 void ScCos();
411 void ScTan();
412 void ScCot();
413 void ScArcSin();
414 void ScArcCos();
415 void ScArcTan();
416 void ScArcCot();
417 void ScSinHyp();
418 void ScCosHyp();
419 void ScTanHyp();
420 void ScCotHyp();
421 void ScArcSinHyp();
422 void ScArcCosHyp();
423 void ScArcTanHyp();
424 void ScArcCotHyp();
425 void ScCosecant();
426 void ScSecant();
427 void ScCosecantHyp();
428 void ScSecantHyp();
429 void ScExp();
430 void ScLn();
431 void ScLog10();
432 void ScSqrt();
433 void ScIsEmpty();
434 short IsString();
435 void ScIsString();
436 void ScIsNonString();
437 void ScIsLogical();
438 void ScType();
439 void ScCell();
440 void ScIsRef();
441 void ScIsValue();
442 void ScIsFormula();
443 void ScFormula();
444 void ScRoman();
445 void ScArabic();
446 void ScIsNV();
447 void ScIsErr();
448 void ScIsError();
449 short IsEven();
450 void ScIsEven();
451 void ScIsOdd();
452 void ScN();
453 void ScCode();
454 void ScTrim();
455 void ScUpper();
456 void ScPropper();
457 void ScLower();
458 void ScLen();
459 void ScT();
460 void ScValue();
461 void ScClean();
462 void ScChar();
463 void ScJis();
464 void ScAsc();
465 void ScUnicode();
466 void ScUnichar();
467 void ScMin( sal_Bool bTextAsZero = sal_False );
468 void ScMax( sal_Bool bTextAsZero = sal_False );
469 double IterateParameters( ScIterFunc, sal_Bool bTextAsZero = sal_False );
470 void ScSumSQ();
471 void ScSum();
472 void ScProduct();
473 void ScAverage( sal_Bool bTextAsZero = sal_False );
474 void ScCount();
475 void ScCount2();
476 void GetStVarParams( double& rVal, double& rValCount, sal_Bool bTextAsZero = sal_False );
477 void ScVar( sal_Bool bTextAsZero = sal_False );
478 void ScVarP( sal_Bool bTextAsZero = sal_False );
479 void ScStDev( sal_Bool bTextAsZero = sal_False );
480 void ScStDevP( sal_Bool bTextAsZero = sal_False );
481 void ScColumns();
482 void ScRows();
483 void ScTables();
484 void ScColumn();
485 void ScRow();
486 void ScTable();
487 void ScMatch();
488 double IterateParametersIf( ScIterFuncIf );
489 void ScCountIf();
490 void ScSumIf();
491 void ScAverageIf();
492 double IterateParametersIfs( ScIterFuncIfs );
493 void ScSumIfs();
494 void ScAverageIfs();
495 void ScCountIfs();
496 void ScCountEmptyCells();
497 void ScLookup();
498 void ScHLookup();
499 void ScVLookup();
500 void ScSubTotal();
501 
502 // If upon call rMissingField==sal_True then the database field parameter may be
503 // missing (Xcl DCOUNT() syntax), or may be faked as missing by having the
504 // value 0.0 or being exactly the entire database range reference (old SO
505 // compatibility). If this was the case then rMissingField is set to sal_True upon
506 // return. If rMissingField==sal_False upon call all "missing cases" are considered
507 // to be an error.
508 ScDBQueryParamBase* GetDBParams( sal_Bool& rMissingField );
509 
510 void DBIterator( ScIterFunc );
511 void ScDBSum();
512 void ScDBCount();
513 void ScDBCount2();
514 void ScDBAverage();
515 void ScDBGet();
516 void ScDBMax();
517 void ScDBMin();
518 void ScDBProduct();
519 void GetDBStVarParams( double& rVal, double& rValCount );
520 void ScDBStdDev();
521 void ScDBStdDevP();
522 void ScDBVar();
523 void ScDBVarP();
524 void ScIndirect();
525 void ScAddressFunc();
526 void ScOffset();
527 void ScIndex();
528 void ScMultiArea();
529 void ScAreas();
530 void ScCurrency();
531 void ScReplace();
532 void ScFixed();
533 void ScFind();
534 void ScExact();
535 void ScLeft();
536 void ScRight();
537 void ScSearch();
538 void ScMid();
539 void ScText();
540 void ScSubstitute();
541 void ScRept();
542 void ScConcat();
543 void ScExternal();
544 void ScMissing();
545 void ScMacro();
546 sal_Bool SetSbxVariable( SbxVariable* pVar, const ScAddress& );
547 sal_Bool SetSbxVariable( SbxVariable* pVar, SCCOL nCol, SCROW nRow, SCTAB nTab );
548 void ScErrorType();
549 void ScDBArea();
550 void ScColRowNameAuto();
551 void ScExternalRef();
552 void ScGetPivotData();
553 void ScHyperLink();
554 void ScBahtText();
555 void ScTTT();
556 
557 //----------------Funktionen in interpr2.cxx---------------
558 
559 /** Obtain the date serial number for a given date.
560     @param bStrict
561         If sal_False, nYear < 100 takes the two-digit year setting into account,
562         and rollover of invalid calendar dates takes place, e.g. 1999-02-31 =>
563         1999-03-03.
564         If sal_True, the date passed must be a valid Gregorian calendar date. No
565         two-digit expanding or rollover is done.
566  */
567 double GetDateSerial( sal_Int16 nYear, sal_Int16 nMonth, sal_Int16 nDay, bool bStrict );
568 
569 void ScGetActDate();
570 void ScGetActTime();
571 void ScGetYear();
572 void ScGetMonth();
573 void ScGetDay();
574 void ScGetDayOfWeek();
575 void ScGetWeekOfYear();
576 void ScEasterSunday();
577 void ScGetHour();
578 void ScGetMin();
579 void ScGetSec();
580 void ScPlusMinus();
581 void ScAbs();
582 void ScInt();
583 void ScEven();
584 void ScOdd();
585 void ScCeil();
586 void ScFloor();
587 void RoundNumber( rtl_math_RoundingMode eMode );
588 void ScRound();
589 void ScRoundUp();
590 void ScRoundDown();
591 void ScGetDateValue();
592 void ScGetTimeValue();
593 void ScArcTan2();
594 void ScLog();
595 void ScGetDate();
596 void ScGetTime();
597 void ScGetDiffDate();
598 void ScGetDiffDate360();
599 void ScPower();
600 void ScAmpersand();
601 void ScAdd();
602 void ScSub();
603 void ScMul();
604 void ScDiv();
605 void ScPow();
606 void ScCurrent();
607 void ScStyle();
608 void ScDde();
609 void ScBase();
610 void ScDecimal();
611 void ScConvert();
612 void ScEuroConvert();
613 
614 //----------------------- Finanzfunktionen ------------------------------------
615 void ScNPV();
616 void ScIRR();
617 void ScMIRR();
618 void ScISPMT();
619 
620 double ScGetBw(double fZins, double fZzr, double fRmz,
621                       double fZw, double fF);
622 void ScBW();
623 void ScDIA();
624 double ScGetGDA(double fWert, double fRest, double fDauer,
625                        double fPeriode, double fFaktor);
626 void ScGDA();
627 void ScGDA2();
628 double ScInterVDB(double fWert,double fRest,double fDauer,double fDauer1,
629                 double fPeriode,double fFaktor);
630 void ScVDB();
631 void ScLaufz();
632 void ScLIA();
633 double ScGetRmz(double fZins, double fZzr, double fBw,
634                        double fZw, double fF);
635 void ScRMZ();
636 void ScZGZ();
637 double ScGetZw(double fZins, double fZzr, double fRmz,
638                       double fBw, double fF);
639 void ScZW();
640 void ScZZR();
641 bool RateIteration(double fNper, double fPayment, double fPv,
642                                 double fFv, double fPayType, double& fGuess);
643 void ScZins();
644 double ScGetZinsZ(double fZins, double fZr, double fZzr, double fBw,
645                          double fZw, double fF, double& fRmz);
646 void ScZinsZ();
647 void ScKapz();
648 void ScKumZinsZ();
649 void ScKumKapZ();
650 void ScEffektiv();
651 void ScNominal();
652 void ScMod();
653 void ScBackSolver();
654 void ScIntercept();
655 //-------------------------Funktionen in interpr5.cxx--------------------------
656 double ScGetGCD(double fx, double fy);
657 void ScGCD();
658 void ScLCM();
659 //-------------------------- Matrixfunktionen ---------------------------------
660 
661 void ScMatValue();
662 void MEMat(ScMatrix* mM, SCSIZE n);
663 void ScMatDet();
664 void ScMatInv();
665 void ScMatMult();
666 void ScMatTrans();
667 void ScEMat();
668 void ScMatRef();
669 ScMatrixRef MatConcat(ScMatrix* pMat1, ScMatrix* pMat2);
670 void ScSumProduct();
671 void ScSumX2MY2();
672 void ScSumX2DY2();
673 void ScSumXMY2();
674 void ScGrowth();
675 bool CalculateSkew(double& fSum,double& fCount,double& vSum,std::vector<double>& values);
676 void CalculateSlopeIntercept(sal_Bool bSlope);
677 void CalculateSmallLarge(sal_Bool bSmall);
678 void CalculatePearsonCovar(sal_Bool _bPearson,sal_Bool _bStexy);
679 bool CalculateTest( sal_Bool _bTemplin
680                    ,const SCSIZE nC1, const SCSIZE nC2,const SCSIZE nR1,const SCSIZE nR2
681                    ,const ScMatrixRef& pMat1,const ScMatrixRef& pMat2
682                    ,double& fT,double& fF);
683 void CalculateLookup(sal_Bool HLookup);
684 bool FillEntry(ScQueryEntry& rEntry);
685 void CalculateAddSub(sal_Bool _bSub);
686 void CalculateTrendGrowth(bool _bGrowth);
687 void CalulateRGPRKP(bool _bRKP);
688 void CalculateSumX2MY2SumX2DY2(sal_Bool _bSumX2DY2);
689 void CalculateMatrixValue(const ScMatrix* pMat,SCSIZE nC,SCSIZE nR);
690 bool CheckMatrix(bool _bLOG,sal_uInt8& nCase,SCSIZE& nCX,SCSIZE& nCY,SCSIZE& nRX,SCSIZE& nRY,SCSIZE& M,SCSIZE& N,ScMatrixRef& pMatX,ScMatrixRef& pMatY);
691 void ScRGP();
692 void ScRKP();
693 void ScForecast();
694 //------------------------- Functions in interpr3.cxx -------------------------
695 void ScNoName();
696 void ScBadName();
697 // Statistik:
698 double phi(double x);
699 double integralPhi(double x);
700 double taylor(double* pPolynom, sal_uInt16 nMax, double x);
701 double gauss(double x);
702 double gaussinv(double x);
703 double GetBetaDist(double x, double alpha, double beta);  //cumulative distribution function
704 double GetBetaDistPDF(double fX, double fA, double fB); //probability density function)
705 double GetChiDist(double fChi, double fDF);     // for LEGACY.CHIDIST, returns right tail
706 double GetChiSqDistCDF(double fX, double fDF);  // for CHISQDIST, returns left tail
707 double GetChiSqDistPDF(double fX, double fDF);  // probability density function
708 double GetFDist(double x, double fF1, double fF2);
709 double GetTDist(double T, double fDF);
710 double Fakultaet(double x);
711 double BinomKoeff(double n, double k);
712 double GetGamma(double x);
713 double GetLogGamma(double x);
714 double GetBeta(double fAlpha, double fBeta);
715 double GetLogBeta(double fAlpha, double fBeta);
716 double GetBinomDistPMF(double x, double n, double p); //probability mass function
717 void ScLogGamma();
718 void ScGamma();
719 void ScPhi();
720 void ScGauss();
721 void ScStdNormDist();
722 void ScFisher();
723 void ScFisherInv();
724 void ScFact();
725 void ScNormDist();
726 void ScGammaDist();
727 void ScGammaInv();
728 void ScExpDist();
729 void ScBinomDist();
730 void ScPoissonDist();
731 void ScKombin();
732 void ScKombin2();
733 void ScVariationen();
734 void ScVariationen2();
735 void ScB();
736 void ScHypGeomDist();
737 void ScLogNormDist();
738 void ScLogNormInv();
739 void ScTDist();
740 void ScFDist();
741 void ScChiDist();   // for LEGACY.CHIDIST, returns right tail
742 void ScChiSqDist(); // returns left tail or density
743 void ScChiSqInv(); //invers to CHISQDIST
744 void ScWeibull();
745 void ScBetaDist();
746 void ScFInv();
747 void ScTInv();
748 void ScChiInv();
749 void ScBetaInv();
750 void ScCritBinom();
751 void ScNegBinomDist();
752 void ScKurt();
753 void ScHarMean();
754 void ScGeoMean();
755 void ScStandard();
756 void ScSkew();
757 void ScMedian();
758 double GetMedian( ::std::vector<double> & rArray );
759 double GetPercentile( ::std::vector<double> & rArray, double fPercentile );
760 void GetNumberSequenceArray( sal_uInt8 nParamCount, ::std::vector<double>& rArray );
761 void GetSortArray(sal_uInt8 nParamCount, ::std::vector<double>& rSortArray, ::std::vector<long>* pIndexOrder = NULL);
762 void QuickSort(::std::vector<double>& rSortArray, ::std::vector<long>* pIndexOrder = NULL);
763 void ScModalValue();
764 void ScAveDev();
765 void ScDevSq();
766 void ScZTest();
767 void ScTTest();
768 void ScFTest();
769 void ScChiTest();
770 void ScRank();
771 void ScPercentile();
772 void ScPercentrank();
773 void ScLarge();
774 void ScSmall();
775 void ScFrequency();
776 void ScQuartile();
777 void ScNormInv();
778 void ScSNormInv();
779 void ScConfidence();
780 void ScTrimMean();
781 void ScProbability();
782 void ScCorrel();
783 void ScCovar();
784 void ScPearson();
785 void ScRSQ();
786 void ScSTEXY();
787 void ScSlope();
788 void ScTrend();
789 void ScInfo();
790 
791 //------------------------ Functions in interpr6.cxx -------------------------
792 
793 static const double fMaxGammaArgument;  // defined in interpr3.cxx
794 
795 double GetGammaContFraction(double fA,double fX);
796 double GetGammaSeries(double fA,double fX);
797 double GetLowRegIGamma(double fA,double fX);    // lower regularized incomplete gamma function, GAMMAQ
798 double GetUpRegIGamma(double fA,double fX);     // upper regularized incomplete gamma function, GAMMAP
799 // probability density function; fLambda is "scale" parameter
800 double GetGammaDistPDF(double fX, double fAlpha, double fLambda);
801 // cumulative distribution function; fLambda is "scale" parameter
802 double GetGammaDist(double fX, double fAlpha, double fLambda);
803 
804 //----------------------------------------------------------------------------
805 public:
806     ScInterpreter( ScFormulaCell* pCell, ScDocument* pDoc,
807                     const ScAddress&, ScTokenArray& );
808     ~ScInterpreter();
809 
810     formula::StackVar Interpret();
811 
812     void SetError(sal_uInt16 nError)
813             { if (nError && !nGlobalError) nGlobalError = nError; }
814 
815     sal_uInt16 GetError()                           const   { return nGlobalError; }
816     formula::StackVar  GetResultType()              const   { return xResult->GetType(); }
817     const String&   GetStringResult()               const   { return xResult->GetString(); }
818     double          GetNumResult()                  const   { return xResult->GetDouble(); }
819     formula::FormulaTokenRef
820                     GetResultToken()                const   { return xResult; }
821     short           GetRetFormatType()              const   { return nRetFmtType; }
822     sal_uLong       GetRetFormatIndex()             const   { return nRetFmtIndex; }
823     ScToken*        GetLastStackRefToken() { return pLastStackRefToken; }
824     bool            IsReferenceFunc() { return bRefFunc; }
825 };
826 
827 
828 inline void ScInterpreter::MatrixDoubleRefToMatrix()
829 {
830     if ( bMatrixFormula && GetStackType() == formula::svDoubleRef )
831     {
832         GetTokenMatrixMap();    // make sure it exists, create if not.
833         PopDoubleRefPushMatrix();
834     }
835 }
836 
837 
838 inline bool ScInterpreter::MatrixParameterConversion()
839 {
840     if ( (bMatrixFormula || pCur->HasForceArray()) && !pJumpMatrix && sp > 0 )
841         return ConvertMatrixParameters();
842     return false;
843 }
844 
845 
846 inline ScTokenMatrixMap& ScInterpreter::GetTokenMatrixMap()
847 {
848     if (!pTokenMatrixMap)
849         pTokenMatrixMap = CreateTokenMatrixMap();
850     return *pTokenMatrixMap;
851 }
852 
853 
854 inline sal_Bool ScInterpreter::MustHaveParamCount( short nAct, short nMust )
855 {
856     if ( nAct == nMust )
857         return sal_True;
858     if ( nAct < nMust )
859         PushParameterExpected();
860     else
861         PushIllegalParameter();
862     return sal_False;
863 }
864 
865 
866 inline sal_Bool ScInterpreter::MustHaveParamCount( short nAct, short nMust, short nMax )
867 {
868     if ( nMust <= nAct && nAct <= nMax )
869         return sal_True;
870     if ( nAct < nMust )
871         PushParameterExpected();
872     else
873         PushIllegalParameter();
874     return sal_False;
875 }
876 
877 
878 inline sal_Bool ScInterpreter::MustHaveParamCountMin( short nAct, short nMin )
879 {
880     if ( nAct >= nMin )
881         return sal_True;
882     PushParameterExpected();
883     return sal_False;
884 }
885 
886 
887 inline sal_Bool ScInterpreter::CheckStringResultLen( String& rResult, const String& rAdd )
888 {
889     if ( (sal_uLong) rResult.Len() + rAdd.Len() > STRING_MAXLEN )
890     {
891         SetError( errStringOverflow );
892         rResult.Erase();
893         return sal_False;
894     }
895     return sal_True;
896 }
897 
898 
899 inline void ScInterpreter::TreatDoubleError( double& rVal )
900 {
901     if ( !::rtl::math::isFinite( rVal ) )
902     {
903         sal_uInt16 nErr = GetDoubleErrorValue( rVal );
904         if ( nErr )
905             SetError( nErr );
906         else
907             SetError( errNoValue );
908         rVal = 0.0;
909     }
910 }
911 
912 
913 // static
914 inline double ScInterpreter::div( const double& fNumerator, const double& fDenominator )
915 {
916     return (fDenominator != 0.0) ? (fNumerator / fDenominator) :
917         CreateDoubleError( errDivisionByZero);
918 }
919 
920 #endif
921