ITK  6.0.0
Insight Toolkit
itkCompensatedSummation.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkCompensatedSummation_h
19#define itkCompensatedSummation_h
20
21#include "itkNumericTraits.h"
22#include "itkConceptChecking.h"
23
24namespace itk
25{
26
65template <typename TFloat>
66class ITK_TEMPLATE_EXPORT CompensatedSummation
67{
68public:
70 using FloatType = TFloat;
71
74
77
84 CompensatedSummation(const Self &) = default;
85
87 Self &
88 operator=(const Self &) = default;
89
91 void
92 AddElement(const FloatType & element);
93 Self &
94 operator+=(const FloatType & rhs);
95 Self &
96 operator+=(const Self & rhs);
100 Self &
101 operator-=(const FloatType & rhs);
102
105 Self &
106 operator*=(const FloatType & rhs);
107 Self &
108 operator/=(const FloatType & rhs);
112 void
114
116 Self &
117 operator=(const FloatType & rhs);
118
120 const AccumulateType &
121 GetSum() const;
122
124 explicit operator FloatType() const;
125
126private:
128 AccumulateType m_Compensation{};
129
130// Maybe support more types in the future with template specialization.
131#ifdef ITK_USE_CONCEPT_CHECKING
132 itkConceptMacro(OnlyDefinedForFloatingPointTypes, (itk::Concept::IsFloatingPoint<TFloat>));
133#endif // ITK_USE_CONCEPT_CHECKING
134};
135
136void ITKCommon_EXPORT
137 CompensatedSummationAddElement(float & compensation, float & sum, const float element);
138void ITKCommon_EXPORT
139 CompensatedSummationAddElement(double & compensation, double & sum, const double element);
140
141} // end namespace itk
142
143#ifndef ITK_MANUAL_INSTANTIATION
144# include "itkCompensatedSummation.hxx"
145#endif
146
147#endif
Perform more precise accumulation of floating point numbers.
Self & operator=(const Self &)=default
Self & operator-=(const FloatType &rhs)
const AccumulateType & GetSum() const
typename NumericTraits< FloatType >::AccumulateType AccumulateType
CompensatedSummation(FloatType value)
Self & operator/=(const FloatType &rhs)
Self & operator+=(const Self &rhs)
Self & operator*=(const FloatType &rhs)
CompensatedSummation(const Self &)=default
Self & operator+=(const FloatType &rhs)
void AddElement(const FloatType &element)
Self & operator=(const FloatType &rhs)
Define additional traits for native types such as int or float.
#define itkConceptMacro(name, concept)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void ITKCommon_EXPORT CompensatedSummationAddElement(float &compensation, float &sum, const float element)