00001
00002
00003
00077 #include <cmath>
00078 #include <functional>
00079
00080
00081 #ifndef CXXUTILS_FPCOMPARE_H
00082 #define CXXUTILS_FPCOMPARE_H
00083
00084
00085
00086 #if defined(__FLT_EVAL_METHOD__) && \
00087 (__FLT_EVAL_METHOD__ == 2 || __FLT_EVAL_METHOD__ < 0)
00088
00089
00090 # define CXXUTILS_FPCOMPARE_VOLATILE volatile
00091 #elif defined(__i386__) && !defined(__SSE2__)
00092
00093
00094
00095
00096
00097 # define CXXUTILS_FPCOMPARE_VOLATILE volatile
00098 #else
00099 # define CXXUTILS_FPCOMPARE_VOLATILE
00100 #endif
00101
00102
00103 namespace CxxUtils {
00104 namespace fpcompare {
00105
00106
00111 inline
00112 bool equal (double a, double b)
00113 {
00114 CXXUTILS_FPCOMPARE_VOLATILE double va = a;
00115 CXXUTILS_FPCOMPARE_VOLATILE double vb = b;
00116 return va == vb;
00117 }
00118
00119
00124 inline
00125 bool equal (float a, float b)
00126 {
00127 CXXUTILS_FPCOMPARE_VOLATILE float va = a;
00128 CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
00129 return va == vb;
00130 }
00131
00132
00137 inline
00138 bool greater (double a, double b)
00139 {
00140 CXXUTILS_FPCOMPARE_VOLATILE double va = a;
00141 CXXUTILS_FPCOMPARE_VOLATILE double vb = b;
00142 return va > vb;
00143 }
00144
00145
00150 inline
00151 bool greater (float a, float b)
00152 {
00153 CXXUTILS_FPCOMPARE_VOLATILE float va = a;
00154 CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
00155 return va > vb;
00156 }
00157
00158
00163 inline
00164 bool less (double a, double b)
00165 {
00166 CXXUTILS_FPCOMPARE_VOLATILE double va = a;
00167 CXXUTILS_FPCOMPARE_VOLATILE double vb = b;
00168 return va < vb;
00169 }
00170
00171
00176 inline
00177 bool less (float a, float b)
00178 {
00179 CXXUTILS_FPCOMPARE_VOLATILE float va = a;
00180 CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
00181 return va < vb;
00182 }
00183
00184
00189 inline
00190 bool greater_equal (double a, double b)
00191 {
00192 CXXUTILS_FPCOMPARE_VOLATILE double va = a;
00193 CXXUTILS_FPCOMPARE_VOLATILE double vb = b;
00194 return va >= vb;
00195 }
00196
00197
00202 inline
00203 bool greater_equal (float a, float b)
00204 {
00205 CXXUTILS_FPCOMPARE_VOLATILE float va = a;
00206 CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
00207 return va >= vb;
00208 }
00209
00210
00215 inline
00216 bool less_equal (double a, double b)
00217 {
00218 CXXUTILS_FPCOMPARE_VOLATILE double va = a;
00219 CXXUTILS_FPCOMPARE_VOLATILE double vb = b;
00220 return va <= vb;
00221 }
00222
00223
00228 inline
00229 bool less_equal (float a, float b)
00230 {
00231 CXXUTILS_FPCOMPARE_VOLATILE float va = a;
00232 CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
00233 return va <= vb;
00234 }
00235
00236
00237 }
00238
00239
00240 namespace fpcompare_fn {
00241
00242
00246 struct equal
00247 : public std::binary_function<double, double, bool>
00248 {
00249 bool
00250 operator()(double a, double b) const
00251 { return fpcompare::equal (a, b); }
00252 };
00253
00254
00258 struct equalf
00259 : public std::binary_function<float, float, bool>
00260 {
00261 bool
00262 operator()(float a, float b) const
00263 { return fpcompare::equal (a, b); }
00264 };
00265
00266
00270 struct greater
00271 : public std::binary_function<double, double, bool>
00272 {
00273 bool
00274 operator()(double a, double b) const
00275 { return fpcompare::greater (a, b); }
00276 };
00277
00278
00282 struct greaterf
00283 : public std::binary_function<float, float, bool>
00284 {
00285 bool
00286 operator()(float a, float b) const
00287 { return fpcompare::greater (a, b); }
00288 };
00289
00290
00294 struct less
00295 : public std::binary_function<double, double, bool>
00296 {
00297 bool
00298 operator()(double a, double b) const
00299 { return fpcompare::less (a, b); }
00300 };
00301
00302
00306 struct lessf
00307 : public std::binary_function<float, float, bool>
00308 {
00309 bool
00310 operator()(float a, float b) const
00311 { return fpcompare::less (a, b); }
00312 };
00313
00314
00318 struct greater_equal
00319 : public std::binary_function<double, double, bool>
00320 {
00321 bool
00322 operator()(double a, double b) const
00323 { return fpcompare::greater_equal (a, b); }
00324 };
00325
00326
00330 struct greater_equalf
00331 : public std::binary_function<float, float, bool>
00332 {
00333 bool
00334 operator()(float a, float b) const
00335 { return fpcompare::greater_equal (a, b); }
00336 };
00337
00338
00342 struct less_equal
00343 : public std::binary_function<double, double, bool>
00344 {
00345 bool
00346 operator()(double a, double b) const
00347 { return fpcompare::less_equal (a, b); }
00348 };
00349
00350
00354 struct less_equalf
00355 : public std::binary_function<float, float, bool>
00356 {
00357 bool
00358 operator()(float a, float b) const
00359 { return fpcompare::less_equal (a, b); }
00360 };
00361
00362
00363 }
00364 }
00365
00366
00367 #endif // not CXXUTILS_FPCOMPARE_H