Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
Precision.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11namespace correlation {
12
13#if defined(CORRELATION_USE_SINGLE_PRECISION) && CORRELATION_USE_SINGLE_PRECISION
14using real_t = float;
15inline constexpr const char *REAL_TYPE_NAME = "float";
16inline constexpr bool is_single_precision = true;
17#else
18using real_t = double;
19inline constexpr const char *REAL_TYPE_NAME = "double";
20inline constexpr bool is_single_precision = false;
21#endif
22
29template <typename T = real_t> struct KahanAccumulator {
30 T sum{0};
32
33 constexpr void add(T val) noexcept {
34 T y_val = val - compensation;
35 T t_val = sum + y_val;
36 compensation = (t_val - sum) - y_val;
37 sum = t_val;
38 }
39
40 constexpr void reset() noexcept {
41 sum = static_cast<T>(0);
42 compensation = static_cast<T>(0);
43 }
44
45 [[nodiscard]] constexpr T value() const noexcept { return sum; }
46
47 constexpr operator T() const noexcept { return sum; }
48};
49
50} // namespace correlation
Definition AnalysisTypes.hpp:11
constexpr const char * REAL_TYPE_NAME
Definition Precision.hpp:19
constexpr bool is_single_precision
Definition Precision.hpp:20
double real_t
Definition Precision.hpp:18
Performs Kahan-Babuška-Neumaier compensated summation to prevent floating-point loss of precision.
Definition Precision.hpp:29
T sum
Definition Precision.hpp:30
T compensation
Definition Precision.hpp:31
constexpr void add(T val) noexcept
Definition Precision.hpp:33
constexpr void reset() noexcept
Definition Precision.hpp:40
constexpr T value() const noexcept
Definition Precision.hpp:45