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