blob: e444c304e4868a441d1a36143954175ac899b8ce (
plain)
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
|
// fuzzy.cc -- template for fuzzy comparisons et al on floating point values
// $Header$
//
// jtutil::fuzzy::tolerance - comparison tolerance
// jtutil::fuzzy::EQ
// jtutil::fuzzy::is_integer
// jtutil::fuzzy::floor
// jtutil::fuzzy::ceiling
//
// ***** template instantiations and specializations *****
//
#include <stdlib.h>
#include <stdio.h>
#include "stdc.h"
#include "util.hh"
// everything in this file is inside these namespaces
namespace AHFinderDirect
{
namespace jtutil
{
//******************************************************************************
template <typename fp_t>
bool fuzzy<fp_t>::EQ(fp_t x, fp_t y)
{
fp_t max_abs = max(jtutil::abs(x), jtutil::abs(y));
fp_t epsilon = max(tolerance_, tolerance_*max_abs);
return jtutil::abs(x-y) <= epsilon;
}
//******************************************************************************
template <typename fp_t>
bool fuzzy<fp_t>::is_integer(fp_t x)
{
int i = round<fp_t>::to_integer(x);
return EQ(x, fp_t(i));
}
//******************************************************************************
template <typename fp_t>
int fuzzy<fp_t>::floor(fp_t x)
{
return fuzzy<fp_t>::is_integer(x)
? round<fp_t>::to_integer(x)
: round<fp_t>::floor(x);
}
//******************************************************************************
template <typename fp_t>
int fuzzy<fp_t>::ceiling(fp_t x)
{
return fuzzy<fp_t>::is_integer(x)
? round<fp_t>::to_integer(x)
: round<fp_t>::ceiling(x);
}
//******************************************************************************
//******************************************************************************
//******************************************************************************
//
// ***** template instantiations and specializations *****
//
//
// Thanks to Thomas Mang <a9804814@unet.univie.ac.at> for helping
// me figure out the correct syntax here!
//
// initializations of fuzzy::tolerance for each instantation we're going to make
template <>
float fuzzy<float>::tolerance_ = 1.0e-5; // about 100 * FLT_EPSILON
template <>
double fuzzy<double>::tolerance_ = 1.0e-12; // about 1e4 * DBL_EPSILON
// template instantiations
template class fuzzy<float>;
template class fuzzy<double>;
//******************************************************************************
//******************************************************************************
//******************************************************************************
} // namespace jtutil
} // namespace AHFinderDirect
|