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 
24 namespace itk
25 {
26 
65 template <typename TFloat>
66 class ITK_TEMPLATE_EXPORT CompensatedSummation
67 {
68 public:
70  using FloatType = TFloat;
71 
74 
77 
79  CompensatedSummation() = default;
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
113  ResetToZero();
114 
116  Self &
117  operator=(const FloatType & rhs);
118 
120  const AccumulateType &
121  GetSum() const;
122 
124  explicit
125  operator FloatType() const;
126 
127 private:
128  AccumulateType m_Sum{};
129  AccumulateType m_Compensation{};
130 
131 // Maybe support more types in the future with template specialization.
132 #ifdef ITK_USE_CONCEPT_CHECKING
133  itkConceptMacro(OnlyDefinedForFloatingPointTypes, (itk::Concept::IsFloatingPoint<TFloat>));
134 #endif // ITK_USE_CONCEPT_CHECKING
135 };
136 
137 void ITKCommon_EXPORT
138 CompensatedSummationAddElement(float & compensation, float & sum, const float element);
139 void ITKCommon_EXPORT
140 CompensatedSummationAddElement(double & compensation, double & sum, const double element);
141 
142 } // end namespace itk
143 
144 #ifndef ITK_MANUAL_INSTANTIATION
145 # include "itkCompensatedSummation.hxx"
146 #endif
147 
148 #endif
itk::CompensatedSummation< RealType >::FloatType
RealType FloatType
Definition: itkCompensatedSummation.h:70
itk::CompensatedSummation
Perform more precise accumulation of floating point numbers.
Definition: itkCompensatedSummation.h:66
itk::CompensatedSummation< RealType >::AccumulateType
typename NumericTraits< FloatType >::AccumulateType AccumulateType
Definition: itkCompensatedSummation.h:73
itkConceptChecking.h
itk::CompensatedSummationAddElement
void ITKCommon_EXPORT CompensatedSummationAddElement(float &compensation, float &sum, const float element)
itk::Concept::IsFloatingPoint
Definition: itkConceptChecking.h:953
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:60
itkConceptMacro
#define itkConceptMacro(name, concept)
Definition: itkConceptChecking.h:65
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itkNumericTraits.h