summaryrefslogtreecommitdiff
path: root/thirdparty/thekla_atlas/nvmath/KahanSum.h
blob: 18d475e7cbb1ceb8a23779c9032013e649e25805 (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
// This code is in the public domain -- Ignacio Castaño <castano@gmail.com>

#pragma once
#ifndef NV_MATH_KAHANSUM_H
#define NV_MATH_KAHANSUM_H

#include "nvmath.h"

namespace nv
{

    class KahanSum
    {
    public:
        KahanSum() : accum(0.0f), err(0) {};

        void add(float f)
        {
            float compensated = f + err;
            float tmp = accum + compensated;
            err = accum - tmp;
            err += compensated;
            accum = tmp;
        }

        float sum() const
        {
            return accum;
        }

    private:
        float accum;
        float err;
    };

} // nv namespace


#endif // NV_MATH_KAHANSUM_H