ACF $AcfVersion:0$
CFixedPointManip.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// ACF includes
7
8
9namespace imath
10{
11
12
127{
128public:
130
147
148 CFixedPointManip(int precision = 1, RoundingType roundingType = RT_NORMAL);
149
153 void SetPrecision(int precision);
154
158
161 void SetRoundingType(RoundingType roundingType);
162
163 // reimplemented (imath::IDoubleManip)
164 virtual int GetPrecision() const override;
165
166 // reimplemented (imath::TIValueManip)
167 virtual double GetRounded(const double& value) const override;
168 virtual bool AreValuesEqual(const double& value1, const double& value2) const override;
169 virtual bool IsSmaller(const double& value1, const double& value2) const override;
170 virtual bool IsSmallerEqual(const double& value1, const double& value2) const override;
171 virtual double GetSmallerValue(const double& value) const override;
172 virtual double GetBiggerValue(const double& value) const override;
173 virtual QString GetString(const double& value) const override;
174 virtual bool GetParsed(const QString& text, double& result) const override;
175
176protected:
177 typedef int (*RoundingFuntionPtr)(qreal value);
178
179 virtual qint32 GetInternalValue(double value) const;
180 virtual double GetNormalValue(qint32 intValue) const;
181
182 QString GetString(const double& value, int precision) const;
183
184private:
185 int m_precision;
186 RoundingType m_roundingType;
187
188 static RoundingFuntionPtr m_roundingFuntionsPtr[RT_LAST + 1];
189
190 double m_scaleToIntFactor;
191};
192
193
194// inline methods
195
197{
198 return m_roundingType;
199}
200
201
202// reimplemented (imath::TIValueManip)
203
204inline double CFixedPointManip::GetRounded(const double& value) const
205{
206 return GetNormalValue(GetInternalValue(value));
207}
208
209
210inline bool CFixedPointManip::AreValuesEqual(const double& value1, const double& value2) const
211{
212 return GetInternalValue(value1) == GetInternalValue(value2);
213}
214
215
216inline bool CFixedPointManip::IsSmaller(const double& value1, const double& value2) const
217{
218 return GetInternalValue(value1) < GetInternalValue(value2);
219}
220
221
222inline bool CFixedPointManip::IsSmallerEqual(const double& value1, const double& value2) const
223{
224 return GetInternalValue(value1) <= GetInternalValue(value2);
225}
226
227
228inline double CFixedPointManip::GetSmallerValue(const double& value) const
229{
230 return GetNormalValue(GetInternalValue(value) - 1);
231}
232
233
234inline double CFixedPointManip::GetBiggerValue(const double& value) const
235{
236 return GetNormalValue(GetInternalValue(value) + 1);
237}
238
239
240// protected methods
241
242inline qint32 CFixedPointManip::GetInternalValue(double value) const
243{
244 RoundingFuntionPtr function = m_roundingFuntionsPtr[m_roundingType];
245
246 return qint32(function(value * m_scaleToIntFactor));
247}
248
249
250inline double CFixedPointManip::GetNormalValue(qint32 intValue) const
251{
252 return intValue / m_scaleToIntFactor;
253}
254
255
256} // namespace imath
257
258
Simple implementation of double value manipulator without rounding or quantization.
Implementation of double value manipulator with fixed-point arithmetic and rounding.
virtual bool GetParsed(const QString &text, double &result) const override
Get value converted from string.
virtual double GetNormalValue(qint32 intValue) const
virtual qint32 GetInternalValue(double value) const
QString GetString(const double &value, int precision) const
virtual double GetSmallerValue(const double &value) const override
Return biggest value smaller that specified one.
void SetRoundingType(RoundingType roundingType)
Set type of rounding.
@ RT_NORMAL
Normal mathematical rounding to the nearest value.
int(* RoundingFuntionPtr)(qreal value)
virtual double GetBiggerValue(const double &value) const override
Return smallest value bigger that specified one.
virtual int GetPrecision() const override
Get number of digits after point.
virtual bool IsSmallerEqual(const double &value1, const double &value2) const override
Compare two values and check, if first one is smaller or equal as the second.
virtual bool AreValuesEqual(const double &value1, const double &value2) const override
Compare two values and check, if there are equal.
virtual QString GetString(const double &value) const override
Get this value as string.
void SetPrecision(int precision)
Set fixed point precision.
CFixedPointManip(int precision=1, RoundingType roundingType=RT_NORMAL)
virtual double GetRounded(const double &value) const override
Get the nearest value rounded used this arithmetik.
virtual bool IsSmaller(const double &value1, const double &value2) const override
Compare two values and check, if first one is smaller as the second.
RoundingType GetRoundingType() const
Get type of rounding.
Package with mathematical functions and algebraical primitives.