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