summaryrefslogtreecommitdiff
path: root/sc/source/core/tool/subtotal.cxx
blob: 4c213893b14d02750fa14296047fba8943f26ab1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <subtotal.hxx>
#include <sal/mathconf.h>
#include <cfloat>

bool SubTotal::SafePlus(double& fVal1, double fVal2)
{
    bool bOk = true;
    SAL_MATH_FPEXCEPTIONS_OFF();
    fVal1 += fVal2;
    if (!std::isfinite(fVal1))
    {
        bOk = false;
        if (fVal2 > 0.0)
            fVal1 = DBL_MAX;
        else
            fVal1 = -DBL_MAX;
    }
    return bOk;
}

bool SubTotal::SafeMult(double& fVal1, double fVal2)
{
    bool bOk = true;
    SAL_MATH_FPEXCEPTIONS_OFF();
    fVal1 *= fVal2;
    if (!std::isfinite(fVal1))
    {
        bOk = false;
        fVal1 = DBL_MAX;
    }
    return bOk;
}

bool SubTotal::SafeDiv(double& fVal1, double fVal2)
{
    bool bOk = true;
    SAL_MATH_FPEXCEPTIONS_OFF();
    fVal1 /= fVal2;
    if (!std::isfinite(fVal1))
    {
        bOk = false;
        fVal1 = DBL_MAX;
    }
    return bOk;
}

void ScFunctionData::update(double fNewVal)
{
    if (mbError)
        return;

    switch (meFunc)
    {
        case SUBTOTAL_FUNC_SUM:
            if (!SubTotal::SafePlus(getValueRef(), fNewVal))
                mbError = true;
            break;
        case SUBTOTAL_FUNC_PROD:
            if (getCountRef() == 0) // copy first value (nVal is initialized to 0)
            {
                getValueRef() = fNewVal;
                getCountRef() = 1; // don't care about further count
            }
            else if (!SubTotal::SafeMult(getValueRef(), fNewVal))
                mbError = true;
            break;
        case SUBTOTAL_FUNC_CNT:
        case SUBTOTAL_FUNC_CNT2:
            ++getCountRef();
            break;
        case SUBTOTAL_FUNC_SELECTION_COUNT:
            getCountRef() += fNewVal;
            break;
        case SUBTOTAL_FUNC_AVE:
            if (!SubTotal::SafePlus(getValueRef(), fNewVal))
                mbError = true;
            else
                ++getCountRef();
            break;
        case SUBTOTAL_FUNC_MAX:
            if (getCountRef() == 0) // copy first value (nVal is initialized to 0)
            {
                getValueRef() = fNewVal;
                getCountRef() = 1; // don't care about further count
            }
            else if (fNewVal > getValueRef())
                getValueRef() = fNewVal;
            break;
        case SUBTOTAL_FUNC_MIN:
            if (getCountRef() == 0) // copy first value (nVal is initialized to 0)
            {
                getValueRef() = fNewVal;
                getCountRef() = 1; // don't care about further count
            }
            else if (fNewVal < getValueRef())
                getValueRef() = fNewVal;
            break;
        case SUBTOTAL_FUNC_VAR:
        case SUBTOTAL_FUNC_STD:
        case SUBTOTAL_FUNC_VARP:
        case SUBTOTAL_FUNC_STDP:
            maWelford.update(fNewVal);
            break;
        default:
            // unhandled unknown
            mbError = true;
    }
}

double ScFunctionData::getResult()
{
    if (mbError)
        return 0.0;

    double fRet = 0.0;
    switch (meFunc)
    {
        case SUBTOTAL_FUNC_CNT:
        case SUBTOTAL_FUNC_CNT2:
        case SUBTOTAL_FUNC_SELECTION_COUNT:
            fRet = getCountRef();
            break;
        case SUBTOTAL_FUNC_SUM:
        case SUBTOTAL_FUNC_MAX:
        case SUBTOTAL_FUNC_MIN:
            // Note that nVal is 0.0 for MAX and MIN if nCount==0, that's also
            // how it is defined in ODFF.
            fRet = getValueRef();
            break;
        case SUBTOTAL_FUNC_PROD:
            fRet = (getCountRef() > 0) ? getValueRef() : 0.0;
            break;
        case SUBTOTAL_FUNC_AVE:
            if (getCountRef() == 0)
                mbError = true;
            else
                fRet = getValueRef() / getCountRef();
            break;
        case SUBTOTAL_FUNC_VAR:
        case SUBTOTAL_FUNC_STD:
            if (maWelford.getCount() < 2)
                mbError = true;
            else
            {
                fRet = maWelford.getVarianceSample();
                if (fRet < 0.0)
                    mbError = true;
                else if (meFunc == SUBTOTAL_FUNC_STD)
                    fRet = sqrt(fRet);
            }
            break;
        case SUBTOTAL_FUNC_VARP:
        case SUBTOTAL_FUNC_STDP:
            if (maWelford.getCount() < 1)
                mbError = true;
            else if (maWelford.getCount() == 1)
                fRet = 0.0;
            else
            {
                fRet = maWelford.getVariancePopulation();
                if (fRet < 0.0)
                    mbError = true;
                else if (meFunc == SUBTOTAL_FUNC_STDP)
                    fRet = sqrt(fRet);
            }
            break;
        default:
            assert(!"unhandled unknown");
            mbError = true;
            break;
    }
    if (mbError)
        fRet = 0.0;
    return fRet;
}

void WelfordRunner::update(double fVal)
{
    ++mnCount;
    const double fDelta = fVal - mfMean;
    mfMean += fDelta / mnCount;
    mfM2 += fDelta * (fVal - mfMean);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */