// 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 #include "stdc.h" #include "util.hh" //****************************************************************************** namespace jtutil { template bool fuzzy::EQ(fp_t x, fp_t y) { fp_t max_abs = jtutil::max(jtutil::abs(x), jtutil::abs(y)); fp_t epsilon = jtutil::max(tolerance_, tolerance_*max_abs); return jtutil::abs(x-y) <= epsilon; } } // namespace jtutil:: //****************************************************************************** namespace jtutil { template bool fuzzy::is_integer(fp_t x) { int i = round::to_integer(x); return EQ(x, fp_t(i)); } } // namespace jtutil:: //****************************************************************************** namespace jtutil { template int fuzzy::floor(fp_t x) { return fuzzy::is_integer(x) ? round::to_integer(x) : round::floor(x); } } // namespace jtutil:: //****************************************************************************** namespace jtutil { template int fuzzy::ceiling(fp_t x) { return fuzzy::is_integer(x) ? round::to_integer(x) : round::ceiling(x); } } // namespace jtutil:: //****************************************************************************** //****************************************************************************** //****************************************************************************** // // ***** template instantiations and specializations ***** // // // Thanks to Thomas Mang for helping // me figure out the correct syntax here! // namespace jtutil { // initializations of fuzzy::tolerance for each instantation we're going to make template <> float fuzzy::tolerance_ = 1.0e-5; // about 100 * FLT_EPSILON template <> double fuzzy::tolerance_ = 1.0e-12; // about 1e4 * DBL_EPSILON // template instantiations template class fuzzy; template class fuzzy; } // namespace jtutil::