diff options
Diffstat (limited to 'src/jtutil/fuzzy.cc')
-rw-r--r-- | src/jtutil/fuzzy.cc | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/src/jtutil/fuzzy.cc b/src/jtutil/fuzzy.cc index 1ebfe54..80f9adb 100644 --- a/src/jtutil/fuzzy.cc +++ b/src/jtutil/fuzzy.cc @@ -1,11 +1,11 @@ // fuzzy.cc -- template for fuzzy comparisons et al on floating point values // $Id$ // -// fuzzy::tolerance - comparison tolerance -// fuzzy::EQ -// fuzzy::is_integer -// fuzzy::floor -// fuzzy::ceiling +// jtutil::fuzzy::tolerance - comparison tolerance +// jtutil::fuzzy::EQ +// jtutil::fuzzy::is_integer +// jtutil::fuzzy::floor +// jtutil::fuzzy::ceiling // // ***** template instantiations ***** // @@ -15,6 +15,8 @@ //****************************************************************************** +namespace jtutil + { template <typename fp> bool fuzzy<fp>::EQ(fp x, fp y) { @@ -23,18 +25,24 @@ fp epsilon = jtutil::max(tolerance, tolerance*max_abs); return jtutil::abs<fp>(x-y) <= epsilon; } + } // namespace jtutil:: //****************************************************************************** +namespace jtutil + { template <typename fp> bool fuzzy<fp>::is_integer(fp x) { int i = round<fp>::to_integer(x); return EQ(x, fp(i)); } + } // namespace jtutil:: //****************************************************************************** +namespace jtutil + { template <typename fp> int fuzzy<fp>::floor(fp x) { @@ -42,9 +50,12 @@ return fuzzy<fp>::is_integer(x) ? round<fp>::to_integer(x) : round<fp>::floor(x); } + } // namespace jtutil:: //****************************************************************************** +namespace jtutil + { template <typename fp> int fuzzy<fp>::ceiling(fp x) { @@ -52,6 +63,7 @@ return fuzzy<fp>::is_integer(x) ? round<fp>::to_integer(x) : round<fp>::ceiling(x); } + } // namespace jtutil:: //****************************************************************************** //****************************************************************************** @@ -62,9 +74,9 @@ return fuzzy<fp>::is_integer(x) // // instantiation for <float> -template class fuzzy<float>; -float fuzzy<float>::tolerance = 1.0e-5; // about 100 * FLT_EPSILON +template class jtutil::fuzzy<float>; +float jtutil::fuzzy<float>::tolerance = 1.0e-5; // about 100 * FLT_EPSILON // instantiations for <double> -template class fuzzy<double>; -double fuzzy<double>::tolerance = 1.0e-12; // about 1e4 * DBL_EPSILON +template class jtutil::fuzzy<double>; +double jtutil::fuzzy<double>::tolerance = 1.0e-12; // about 1e4 * DBL_EPSILON |