CFixedPointManip.h
Go to the documentation of this file.
1 #ifndef imath_CFixedPointManip_included
2 #define imath_CFixedPointManip_included
3 
4 
5 #include <imath/CDoubleManip.h>
6 
7 
8 namespace imath
9 {
10 
11 
16 {
17 public:
19 
21  {
35  };
36 
37  CFixedPointManip(int precision = 1, RoundingType roundingType = RT_NORMAL);
38 
42  void SetPrecision(int precision);
43 
47 
50  void SetRoundingType(RoundingType roundingType);
51 
52  // reimplemented (imath::IDoubleManip)
53  virtual int GetPrecision() const;
54 
55  // reimplemented (imath::TIValueManip)
56  virtual double GetRounded(const double& value) const;
57  virtual bool IsEqual(const double& value1, const double& value2) const;
58  virtual bool IsSmaller(const double& value1, const double& value2) const;
59  virtual bool IsSmallerEqual(const double& value1, const double& value2) const;
60  virtual double GetSmallerValue(const double& value) const;
61  virtual double GetBiggerValue(const double& value) const;
62  virtual QString GetString(const double& value) const;
63  virtual bool GetParsed(const QString& text, double& result) const;
64 
65 protected:
66  typedef int (*RoundingFuntionPtr)(qreal value);
67 
68  virtual qint32 GetInternalValue(double value) const;
69  virtual double GetNormalValue(qint32 intValue) const;
70 
71  QString GetString(const double& value, int precision) const;
72 
73 private:
74  int m_precision;
75  RoundingType m_roundingType;
76 
77  static RoundingFuntionPtr m_roundingFuntionsPtr[RT_LAST + 1];
78 
79  double m_scaleToIntFactor;
80 };
81 
82 
83 // inline methods
84 
86 {
87  return m_roundingType;
88 }
89 
90 
91 // reimplemented (imath::TIValueManip)
92 
93 inline double CFixedPointManip::GetRounded(const double& value) const
94 {
95  return GetNormalValue(GetInternalValue(value));
96 }
97 
98 
99 inline bool CFixedPointManip::IsEqual(const double& value1, const double& value2) const
100 {
101  return GetInternalValue(value1) == GetInternalValue(value2);
102 }
103 
104 
105 inline bool CFixedPointManip::IsSmaller(const double& value1, const double& value2) const
106 {
107  return GetInternalValue(value1) < GetInternalValue(value2);
108 }
109 
110 
111 inline bool CFixedPointManip::IsSmallerEqual(const double& value1, const double& value2) const
112 {
113  return GetInternalValue(value1) <= GetInternalValue(value2);
114 }
115 
116 
117 inline double CFixedPointManip::GetSmallerValue(const double& value) const
118 {
119  return GetNormalValue(GetInternalValue(value) - 1);
120 }
121 
122 
123 inline double CFixedPointManip::GetBiggerValue(const double& value) const
124 {
125  return GetNormalValue(GetInternalValue(value) + 1);
126 }
127 
128 
129 // protected methods
130 
131 inline qint32 CFixedPointManip::GetInternalValue(double value) const
132 {
133  RoundingFuntionPtr function = m_roundingFuntionsPtr[m_roundingType];
134 
135  return qint32(function(value * m_scaleToIntFactor));
136 }
137 
138 
139 inline double CFixedPointManip::GetNormalValue(qint32 intValue) const
140 {
141  return intValue / m_scaleToIntFactor;
142 }
143 
144 
145 } // namespace imath
146 
147 
148 #endif // !imath_CFixedPointManip_included
149 
150 
CFixedPointManip(int precision=1, RoundingType roundingType=RT_NORMAL)
virtual double GetNormalValue(qint32 intValue) const
virtual bool IsSmallerEqual(const double &value1, const double &value2) const
Compare two values and check, if first one is smaller or equal as the second.
virtual qint32 GetInternalValue(double value) const
Normal mathematical rounding to the nearest value.
virtual bool IsEqual(const double &value1, const double &value2) const
Compare two values and check, if there are equal.
Simple implementation of imath::IDoubleManip interface for double values without rounding.
Definition: CDoubleManip.h:15
void SetPrecision(int precision)
Set fixed point precision.
void SetRoundingType(RoundingType roundingType)
Set type of rounding.
virtual double GetBiggerValue(const double &value) const
Return smallest value bigger that specified one.
virtual QString GetString(const double &value) const
Get this value as string.
RoundingType GetRoundingType() const
Get type of rounding.
virtual double GetSmallerValue(const double &value) const
Return biggest value smaller that specified one.
virtual double GetRounded(const double &value) const
Get the nearest value rounded used this arithmetik.
virtual bool IsSmaller(const double &value1, const double &value2) const
Compare two values and check, if first one is smaller as the second.
Implementation of imath::IDoubleManip interface for fixed point arithmentic.
virtual bool GetParsed(const QString &text, double &result) const
Get value converted from string.
virtual int GetPrecision() const
Get number of digits after point.
int(* RoundingFuntionPtr)(qreal value)

© 2007-2017 Witold Gantzke and Kirill Lepskiy