// Fixed point number
//
// Simplified code as example for a fixed point number class.
//
// by feyd//godX.de
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
// Fixed point base class (without exceptions/error handling)
template<class _TYPE, class _TYPEEX, int _MULTIPLICATOR>
class CFixedPoint
{
public:
// Assignments
CFixedPoint() : m_nNumber(0) {}
CFixedPoint(_TYPE nNumber) : m_nNumber(nNumber) {}
CFixedPoint(const CFixedPoint& other) : m_nNumber(other.m_nNumber) {}
CFixedPoint(float fValue) : m_nNumber((_TYPE)(fValue*(float)_MULTIPLICATOR)) {}
CFixedPoint(double fValue) : m_nNumber((_TYPE)(fValue*(double)_MULTIPLICATOR)) {}
operator float() const { return (float)m_nNumber/(float)_MULTIPLICATOR; }
operator double() const { return (float)m_nNumber/(float)_MULTIPLICATOR; }
CFixedPoint& operator=(const CFixedPoint& other)
{
m_nNumber = other.m_nNumber;
return *this;
}
// Comparation
bool operator==(const CFixedPoint& other) const
{
return m_nNumber==other.m_nNumber;
}
bool operator>(const CFixedPoint& other) const
{
return m_nNumber>other.m_nNumber;
}
bool operator>=(const CFixedPoint& other) const
{
return m_nNumber>=other.m_nNumber;
}
bool operator!=(const CFixedPoint& other) const
{
return !operator==(other);
}
bool operator<(const CFixedPoint& other) const
{
return !operator>=(other);
}
bool operator<=(const CFixedPoint& other) const
{
return !operator>(other);
}
// Arithmetic
const CFixedPoint operator+(const CFixedPoint& other)
{
return m_nNumber+other.m_nNumber;
}
const CFixedPoint operator-(const CFixedPoint& other)
{
return m_nNumber-other.m_nNumber;
}
const CFixedPoint operator*(const CFixedPoint& other)
{
return (_TYPE)(((_TYPEEX)m_nNumber*(_TYPEEX)other.m_nNumber)/(_TYPEEX)_MULTIPLICATOR);
}
const CFixedPoint operator/(const CFixedPoint& other)
{
return (_TYPE)(((_TYPEEX)m_nNumber*(_TYPEEX)_MULTIPLICATOR)/(_TYPEEX)other.m_nNumber);
}
const CFixedPoint operator%(const CFixedPoint& other)
{
return m_nNumber%other.m_nNumber;
}
protected:
_TYPE m_nNumber;
};
// Application entry point (from libc)
int main(int argc, const char* argv[])
{
printf("Fixed point number...\r\n\r\n");
{
std::vector<CFixedPoint<int, int64_t, 1000> > numbers;
for(int n=1; n<argc; n++)
numbers.push_back(atof(argv[n]));
printf(" Multiplication:\r\n");
for(size_t m=0; m<numbers.size(); m++)
for(size_t n=0; n<numbers.size(); n++)
if(m!=n)
printf(" %3.3f * %3.3f = %3.3f\r\n", (double)numbers[m], (double)numbers[n], (double)(numbers[m]*numbers[n]));
printf("\r\n");
printf(" Division:\r\n");
for(size_t m=0; m<numbers.size(); m++)
for(size_t n=0; n<numbers.size(); n++)
if(m!=n)
printf(" %3.3f / %3.3f = %3.3f\r\n", (double)numbers[m], (double)numbers[n], (double)(numbers[m]/numbers[n]));
printf("\r\n");
printf(" Modulo:\r\n");
for(size_t m=0; m<numbers.size(); m++)
for(size_t n=0; n<numbers.size(); n++)
if(m!=n)
printf(" %3.3f %% %3.3f = %3.3f\r\n", (double)numbers[m], (double)numbers[n], (double)(numbers[m]%numbers[n]));
printf("\r\n");
printf(" Equality:\r\n");
for(size_t m=0; m<numbers.size(); m++)
for(size_t n=0; n<numbers.size(); n++)
if(m!=n)
printf(" %3.3f == %3.3f = %s\r\n", (double)numbers[m], (double)numbers[n], numbers[m]==numbers[n]?"true":"false");
printf("\r\n");
printf(" Greater than:\r\n");
for(size_t m=0; m<numbers.size(); m++)
for(size_t n=0; n<numbers.size(); n++)
if(m!=n)
printf(" %3.3f > %3.3f = %s\r\n", (double)numbers[m], (double)numbers[n], numbers[m]>numbers[n]?"true":"false");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133