// 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 ***** // #include #include "stdc.h" #include "util.hh" //****************************************************************************** namespace jtutil { template bool fuzzy::EQ(fp x, fp y) { fp max_abs = std::max(jtutil::abs(x), jtutil::abs(y)); fp epsilon = std::max(tolerance, tolerance*max_abs); return jtutil::abs(x-y) <= epsilon; } } // namespace jtutil:: //****************************************************************************** namespace jtutil { template bool fuzzy::is_integer(fp x) { int i = round::to_integer(x); return EQ(x, fp(i)); } } // namespace jtutil:: //****************************************************************************** namespace jtutil { template int fuzzy::floor(fp x) { return fuzzy::is_integer(x) ? round::to_integer(x) : round::floor(x); } } // namespace jtutil:: //****************************************************************************** namespace jtutil { template int fuzzy::ceiling(fp x) { return fuzzy::is_integer(x) ? round::to_integer(x) : round::ceiling(x); } } // namespace jtutil:: //****************************************************************************** //****************************************************************************** //****************************************************************************** // // ***** template instantiations ***** // // instantiation for template class jtutil::fuzzy; float jtutil::fuzzy::tolerance = 1.0e-5; // about 100 * FLT_EPSILON // instantiations for template class jtutil::fuzzy; double jtutil::fuzzy::tolerance = 1.0e-12; // about 1e4 * DBL_EPSILON