-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfperror.h
256 lines (224 loc) · 8.42 KB
/
fperror.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* A template number class for estimating the floating point rounding
* errors introduced in operations
*
* Replace all variables with
* float -> Float
* double -> Double
*
* The code will double all operations once in the type used (float/double)
* and once in "long double" precision.
* At any moment you can inspect the error of the operation
*
* e.g.
* Float a = 100.00001f;
* Float b = 100.0f;
*
* // The following two are equivalent
* Float c = a*a - b*b; // - introduces a catastrophic cancellation
* Float d = (a+b)*(a-b); // +/- introduces a benign cancellation
*
* c.dump(cout,"c=") << endl;
* d.dump(cout,"d=") << endl;
*
* Author: [email protected]
* Date: 29-Jan-2016
*/
#ifndef __FPERROR_H
#define __FPERROR_H
#include <ostream>
#include <iomanip>
#include <math.h>
#include <assert.h>
/*================================== Number ===================================*/
template <class T>
class Number {
public:
T value;
long double real;
public:
Number() {};
Number(const float f) : value((T)f), real(f) {}
Number(const double f) : value((T)f), real(f) {}
Number(const long double r) : value((T)r), real(r) {}
Number(const T f, const long double r) : value(f), real(r) {}
Number(const Number<T>& f) : value(f.value), real(f.real) {}
/** @return error */
long double error() const { return (long double)value - real; }
/** set number */
Number<T>& operator = (const float f) { value = real = f; return *this; }
Number<T>& operator = (const double f) { value = real = f; return *this; }
Number<T>& operator = (const Number<T> f) { value = f.value; real = f.real; return *this; }
// operator T () const { return value; }
// long double operator () () const { return real; }
Number<T>& operator +=(const Number<T>& f) {
value += f.value;
real += f.real;
return *this;
}
Number<T> operator +(const Number<T>& n) const { return Number<T>(value+n.value, real+n.real); }
Number<T> operator +(const float f) const { return Number<T>(value+f, real+f); }
Number<T> operator +(const double f) const { return Number<T>(value+f, real+f); }
Number<T>& operator -=(const Number<T>& f) {
value -= f.value;
real -= f.real;
return *this;
}
Number<T> operator -(const Number<T>& n) const { return Number<T>(value-n.value, real-n.real); }
Number<T> operator -(const float f) const { return Number<T>(value-f, real-f); }
Number<T> operator -(const double f) const { return Number<T>(value-f, real-f); }
Number<T> operator -() const { return Number<T>(-value, -real); }
Number<T>& operator *=(const Number<T>& f) {
value *= f.value;
real *= f.real;
return *this;
}
Number<T> operator *(const Number<T>& n) const { return Number<T>(value*n.value, real*n.real); }
Number<T> operator *(const float f) const { return Number<T>(value*f, real*f); }
Number<T> operator *(const double f) const { return Number<T>(value*f, real*f); }
Number<T>& operator /=(const Number<T>& f) {
value /= f.value;
real /= f.real;
return *this;
}
Number<T> operator /(const Number<T>& n) const { return Number<T>(value/n.value, real/n.real); }
Number<T> operator /(const float f) const { return Number<T>(value/f, real/f); }
Number<T> operator /(const double f) const { return Number<T>(value/f, real/f); }
bool operator ==(const Number<T>& n) const { return value==n.value; }
bool operator ==(const float f) const { return value==f; }
bool operator ==(const double f) const { return value==f; }
bool operator !=(const Number<T>& n) const { return value!=n.value; }
bool operator !=(const float f) const { return value!=f; }
bool operator !=(const double f) const { return value!=f; }
bool operator <(const Number<T>& n) const { return value<n.value; }
bool operator <(const float f) const { return value<f; }
bool operator <(const double f) const { return value<f; }
bool operator <=(const Number<T>& n) const { return value<=n.value; }
bool operator <=(const float f) const { return value<=f; }
bool operator <=(const double f) const { return value<=f; }
bool operator >(const Number<T>& n) const { return value>n.value; }
bool operator >(const float f) const { return value>f; }
bool operator >(const double f) const { return value>f; }
bool operator >=(const Number<T>& n) const { return value>=n.value; }
bool operator >=(const float f) const { return value>=f; }
bool operator >=(const double f) const { return value>=f; }
std::ostream& dump(std::ostream& s, const char* prefix=NULL) const {
if (prefix) s << prefix;
s << "f:" << std::setw(sizeof(T)*3+6) << std::setprecision(sizeof(T)*3) << std::left << value;
s << " r:" << std::setw(56) << std::setprecision(50)<< std::left << real;
s << " e:" << std::setw(15) << std::setprecision(9) << std::left << error();
return s;
}
}; // class Number<T>
template <class T> inline Number<T> operator + (const float a, const Number<T>& b) { return b+a; }
template <class T> inline Number<T> operator + (const double a, const Number<T>& b) { return b+a; }
template <class T> inline Number<T> operator - (const float a, const Number<T>& b) { return Number<T>(a-b.value, a-b.real); }
template <class T> inline Number<T> operator - (const double a, const Number<T>& b) { return Number<T>(a-b.value, a-b.real); }
template <class T> inline Number<T> operator * (const float a, const Number<T>& b) { return b*a; }
template <class T> inline Number<T> operator * (const double a, const Number<T>& b) { return b*a; }
template <class T> inline Number<T> operator / (const float a, const Number<T>& b) { return Number<T>(a/b.value, a/b.real); }
template <class T> inline Number<T> operator / (const double a, const Number<T>& b) { return Number<T>(a/b.value, a/b.real); }
typedef Number<float> Float;
typedef Number<double> Double;
#define MATH_FUNC(T,F,E) \
inline Number<T> F(const Number<T>& x) \
{ return Number<T>(F##E(x.value), F##l(x.real)); }
#define MATH_FUNCF(T,F,E) \
inline Number<T> F(const Number<T>& x) \
{ return Number<T>(F##f(x.value), F##l(x.real)); }
#define MATH_FUNC2(T,F,E) \
inline Number<T> F(const Number<T>& x, const Number<T>& y) \
{ return Number<T>(F##E(x.value, y.value), F##l(x.real, y.real)); }
MATH_FUNC(float,acos,f)
MATH_FUNC(float,acosh,f)
MATH_FUNC(float,asin,f)
MATH_FUNC(float,asinh,f)
MATH_FUNC(float,atan,f)
MATH_FUNC(float,atanh,f)
MATH_FUNC(float,cbrt,f)
MATH_FUNC(float,ceil,f)
MATH_FUNC(float,cos,f)
MATH_FUNC(float,cosh,f)
MATH_FUNC(float,erf,f)
MATH_FUNC(float,erfc,f)
MATH_FUNC(float,exp,f)
MATH_FUNC(float,exp2,f)
MATH_FUNC(float,expm1,f)
MATH_FUNCF(float,fabs,f)
MATH_FUNC(float,floor,f)
MATH_FUNC(float,lgamma,f)
MATH_FUNC(float,log,f)
MATH_FUNC(float,log10,f)
MATH_FUNC(float,log1p,f)
MATH_FUNC(float,log2,f)
MATH_FUNC(float,logb,f)
MATH_FUNC(float,nearbyint,f)
MATH_FUNC(float,rint,f)
MATH_FUNC(float,round,f)
MATH_FUNC(float,sin,f)
MATH_FUNC(float,sinh,f)
MATH_FUNCF(float,sqrt,f)
MATH_FUNC(float,tan,f)
MATH_FUNC(float,tanh,f)
MATH_FUNC(float,tgamma,f)
MATH_FUNC(float,trunc,f)
MATH_FUNC(float,y0,f)
MATH_FUNC(float,y1,f)
MATH_FUNC2(float,remainder,f)
MATH_FUNC2(float,pow,f)
MATH_FUNC2(float,nextafter,f)
MATH_FUNC2(float,hypot,f)
MATH_FUNC2(float,fmod,f)
MATH_FUNC2(float,fmin,f)
MATH_FUNC2(float,fmax,f)
MATH_FUNC2(float,fdim,f)
MATH_FUNC2(float,copysign,f)
MATH_FUNC2(float,atan2,f)
MATH_FUNC(double,acos,)
MATH_FUNC(double,acosh,)
MATH_FUNC(double,asin,)
MATH_FUNC(double,asinh,)
MATH_FUNC(double,atan,)
MATH_FUNC(double,atanh,)
MATH_FUNC(double,cbrt,)
MATH_FUNC(double,ceil,)
MATH_FUNC(double,cos,)
MATH_FUNC(double,cosh,)
MATH_FUNC(double,erf,)
MATH_FUNC(double,erfc,)
MATH_FUNC(double,exp,)
MATH_FUNC(double,exp2,)
MATH_FUNC(double,expm1,)
MATH_FUNC(double,fabs,)
MATH_FUNC(double,floor,)
MATH_FUNC(double,lgamma,)
MATH_FUNC(double,log,)
MATH_FUNC(double,log10,)
MATH_FUNC(double,log1p,)
MATH_FUNC(double,log2,)
MATH_FUNC(double,logb,)
MATH_FUNC(double,nearbyint,)
MATH_FUNC(double,rint,)
MATH_FUNC(double,round,)
MATH_FUNC(double,sin,)
MATH_FUNC(double,sinh,)
MATH_FUNC(double,sqrt,)
MATH_FUNC(double,tan,)
MATH_FUNC(double,tanh,)
MATH_FUNC(double,tgamma,)
MATH_FUNC(double,trunc,)
MATH_FUNC(double,y0,)
MATH_FUNC(double,y1,)
MATH_FUNC2(double,remainder,)
MATH_FUNC2(double,pow,)
MATH_FUNC2(double,nextafter,)
MATH_FUNC2(double,hypot,)
MATH_FUNC2(double,fmod,)
MATH_FUNC2(double,fmin,)
MATH_FUNC2(double,fmax,)
MATH_FUNC2(double,fdim,)
MATH_FUNC2(double,copysign,)
MATH_FUNC2(double,atan2,)
#undef MATH_FUNC
#undef MATH_FUNC2
#endif