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 .
27 static bool SafePlus( double& fVal1
, double fVal2
);
28 static bool SafeMult( double& fVal1
, double fVal2
);
29 static bool SafeDiv( double& fVal1
, double fVal2
);
34 /** Implements the Welford Online one-pass algorithm.
35 See https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_Online_algorithm
36 and Donald E. Knuth, TAoCP vol.2, 3rd edn., p. 232
41 WelfordRunner() : mfMean(0.0), mfM2(0.0), mnCount(0) {}
42 void update( double fVal
);
43 sal_uInt64
getCount() const { return mnCount
; }
44 double getVarianceSample() const { return mnCount
> 1 ? mfM2
/ (mnCount
-1) : 0.0; }
45 double getVariancePopulation() const { return mnCount
> 0 ? mfM2
/ mnCount
: 0.0; }
47 // The private variables can be abused by ScFunctionData as general
48 // sum/min/max/ave/count/... variables to reduce memory footprint for that
49 // ScFunctionData may be a mass object during consolidation.
50 // ScFunctionData::update() and getResult() take care that purposes are not
52 friend class ScFunctionData
;
59 /** To calculate a single subtotal function. */
63 ScFunctionData() : meFunc(SUBTOTAL_FUNC_NONE
), mbError(false) {}
64 ScFunctionData( ScSubTotalFunc eFn
) : meFunc(eFn
), mbError(false) {}
66 void update( double fNewVal
);
67 /// Check getError() after (!) obtaining the result.
69 bool getError() const { return mbError
; }
70 ScSubTotalFunc
getFunc() const { return meFunc
; }
71 void setError() { mbError
= true; }
74 WelfordRunner maWelford
;
75 ScSubTotalFunc meFunc
;
78 double& getValueRef() { return maWelford
.mfMean
; }
79 sal_uInt64
& getCountRef() { return maWelford
.mnCount
; }
82 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */