1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_SC_INC_SUBTOTAL_HXX
21 #define INCLUDED_SC_INC_SUBTOTAL_HXX
28 static bool SafePlus( double& fVal1
, double fVal2
);
29 static bool SafeMult( double& fVal1
, double fVal2
);
30 static bool SafeDiv( double& fVal1
, double fVal2
);
35 /** Implements the Welford Online one-pass algorithm.
36 See https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_Online_algorithm
37 and Donald E. Knuth, TAoCP vol.2, 3rd edn., p. 232
42 WelfordRunner() : mfMean(0.0), mfM2(0.0), mnCount(0) {}
43 void update( double fVal
);
44 sal_uInt64
getCount() const { return mnCount
; }
45 double getVarianceSample() const { return mnCount
> 1 ? mfM2
/ (mnCount
-1) : 0.0; }
46 double getVariancePopulation() const { return mnCount
> 0 ? mfM2
/ mnCount
: 0.0; }
48 // The private variables can be abused by ScFunctionData as general
49 // sum/min/max/ave/count/... variables to reduce memory footprint for that
50 // ScFunctionData may be a mass object during consolidation.
51 // ScFunctionData::update() and getResult() take care that purposes are not
53 friend class ScFunctionData
;
60 /** To calculate a single subtotal function. */
64 ScFunctionData() : meFunc(SUBTOTAL_FUNC_NONE
), mbError(false) {}
65 ScFunctionData( ScSubTotalFunc eFn
) : meFunc(eFn
), mbError(false) {}
67 void update( double fNewVal
);
68 /// Check getError() after (!) obtaining the result.
70 bool getError() const { return mbError
; }
71 ScSubTotalFunc
getFunc() const { return meFunc
; }
72 void setError() { mbError
= true; }
75 WelfordRunner maWelford
;
76 ScSubTotalFunc meFunc
;
79 double& getValueRef() { return maWelford
.mfMean
; }
80 sal_uInt64
& getCountRef() { return maWelford
.mnCount
; }
85 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */