-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumformat.h
170 lines (126 loc) · 4.87 KB
/
numformat.h
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#pragma once
#include <iostream>
#include <math.h>
template < typename type>
class NumFormat {
public:
//allow just declaration without initilization, initialize to zero
NumFormat() {_value = 0;};
NumFormat(type value) : _value(value) {};
// it is not possible to make pure otherwise linking error
virtual type getValue() const {return _value;};
virtual void setValue(type value) {_value = value;}
virtual type convertTo(type value) const {return _value;};
virtual NumFormat<type> & operator = (NumFormat<type> assign) {
//printf("assignment %f\n", assign.getValue());
_value = convertTo(assign.getValue());
return *this;
}
//is not called
template<typename any_type> NumFormat<type> & operator = (any_type assign) {
//printf("assignment type %f\n", assign);
_value = convertTo(assign);
return *this;
}
//prefix
virtual NumFormat<type> & operator ++ () {
_value = convertTo(this->getValue() + 1.0);
return *this;
}
virtual NumFormat<type> & operator -- () {
_value = convertTo(this->getValue() - 1.0);
return *this;
}
//Postfix not properly implemented yet
virtual NumFormat<type> & operator ++ (int) {
_value = convertTo(this->getValue() + 1.0);
return *this;
}
virtual NumFormat<type> & operator -- (int) {
type tmp = convertTo(this->getValue() - 1.0);
return *this;
}
virtual NumFormat<type> & operator += (NumFormat<type> addend) {
_value = convertTo(this->getValue() + addend.getValue());
return *this;
}
virtual NumFormat<type> & operator -= (NumFormat<type> subtrahend) {
_value = convertTo(this->getValue() - subtrahend.getValue());
return *this;
}
virtual NumFormat<type> & operator *= (NumFormat<type> multiplicand) {
_value = convertTo(this->getValue() * multiplicand.getValue());
return *this;
}
virtual NumFormat<type> & operator /= (NumFormat<type> dividend) {
_value = convertTo(this->getValue() / dividend.getValue());
return *this;
}
//format this is saved to is not clear yet, dont convert
virtual type operator + (NumFormat<type> addend) const{
return this->getValue() + addend.getValue();
}
virtual type operator - (NumFormat<type> subtrahend) const{
return this->getValue() - subtrahend.getValue();
}
virtual type operator * (NumFormat<type> multiplicand) const{
return this->getValue() * multiplicand.getValue();
}
virtual type operator / (NumFormat<type> dividend) const{
return this->getValue() / dividend.getValue();
}
//comparisons
virtual bool operator > (NumFormat<type> cmp) {
return this->getValue() > cmp.getValue();
}
virtual bool operator >= (NumFormat<type> cmp) {
return this->getValue() >= cmp.getValue();
}
virtual bool operator < (NumFormat<type> cmp) {
return this->getValue() < cmp.getValue();
}
virtual bool operator <= (NumFormat<type> cmp) {
return this->getValue() <= cmp.getValue();
}
virtual bool operator == (NumFormat<type> cmp) {
return this->getValue() == cmp.getValue();
}
virtual bool operator != (NumFormat<type> cmp) {
return this->getValue() != cmp.getValue();
}
virtual bool operator && (NumFormat<type> cmp) {
return this->getValue() && cmp.getValue();
}
virtual bool operator || (NumFormat<type> cmp) {
return this->getValue() || cmp.getValue();
}
//ostream
friend std::ostream& operator<< (std::ostream &out, const NumFormat<type> &value) {
value.print(out);
return out;
};
//this function can be overloaded
virtual void print(std::ostream& out) const {
out << " " << _value << " ";
}
protected:
type _value;
};
//allow different order e.g. float + Float
template<typename type, typename any_type> type operator + (any_type add_1, NumFormat<type> add_2) {
return add_1 + add_2.getValue();
}
template<typename type, typename any_type> type operator - (any_type sub_1, NumFormat<type> sub_2) {
return sub_1 - sub_2.getValue();
}
template<typename type, typename any_type> type operator * (any_type mul_1, NumFormat<type> mul_2) {
return mul_1 * mul_2.getValue();
}
template<typename type, typename any_type> type operator / (any_type div_1, NumFormat<type> div_2) {
return div_1 / div_2.getValue();
}
template<typename type> float sinf(NumFormat<type> value) {return sinf(value.getValue());};
template<typename type> float cosf(NumFormat<type> value) {return cosf(value.getValue());};
template<typename type> float expf(NumFormat<type> value) {return expf(value.getValue());};
template<typename type> float sqrtf(NumFormat<type> value) {return sqrtf(value.getValue());};
enum eRoundingType {RoundToClosest, RoundToNext};