Commits

Anonymous committed dd9138d

Add fpcompare overloads for float as well as double.

  • Participants
  • Parent commits 982db2f
  • Tags CxxUtils-00-00-38

Comments (0)

Files changed (3)

+2009-10-22  scott s snyder  <snyder@bnl.gov>
+
+	* Tagging CxxUtils-00-00-37.
+	* CxxUtils/fpcompare.h: Add overloads for float as well as
+	double.
+	* test/fpcompare_test.cxx: Test changes.
+
 2009-10-09  scott snyder  <snyder@bnl.gov>
 
 	* Tagging CxxUtils-00-00-36.

CxxUtils/fpcompare.h

 
 /**
  * @brief Compare two FP numbers, working around x87 precision issues.
+ * @return @a a > @a b
+ */
+inline
+bool greater (float a, float b)
+{
+  CXXUTILS_FPCOMPARE_VOLATILE float va = a;
+  CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
+  return va > vb;
+}
+
+
+/**
+ * @brief Compare two FP numbers, working around x87 precision issues.
  * @return @a a < @a b
  */
 inline
 
 /**
  * @brief Compare two FP numbers, working around x87 precision issues.
+ * @return @a a < @a b
+ */
+inline
+bool less (float a, float b)
+{
+  CXXUTILS_FPCOMPARE_VOLATILE float va = a;
+  CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
+  return va < vb;
+}
+
+
+/**
+ * @brief Compare two FP numbers, working around x87 precision issues.
  * @return @a a >= @a b
  */
 inline
 
 /**
  * @brief Compare two FP numbers, working around x87 precision issues.
+ * @return @a a >= @a b
+ */
+inline
+bool greater_equal (float a, float b)
+{
+  CXXUTILS_FPCOMPARE_VOLATILE float va = a;
+  CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
+  return va >= vb;
+}
+
+
+/**
+ * @brief Compare two FP numbers, working around x87 precision issues.
  * @return @a a <= @a b
  */
 inline
 }
 
 
+/**
+ * @brief Compare two FP numbers, working around x87 precision issues.
+ * @return @a a <= @a b
+ */
+inline
+bool less_equal (float a, float b)
+{
+  CXXUTILS_FPCOMPARE_VOLATILE float va = a;
+  CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
+  return va <= vb;
+}
+
+
 } // namespace fpcompare
 
 
 /**
  * @brief Compare two FP numbers, working around x87 precision issues.
  */
+struct greaterf
+  : public std::binary_function<float, float, bool>
+{
+  bool
+  operator()(float a, float b) const
+  { return fpcompare::greater (a, b); }
+};
+
+
+/**
+ * @brief Compare two FP numbers, working around x87 precision issues.
+ */
 struct less
   : public std::binary_function<double, double, bool>
 {
 /**
  * @brief Compare two FP numbers, working around x87 precision issues.
  */
+struct lessf
+  : public std::binary_function<float, float, bool>
+{
+  bool
+  operator()(float a, float b) const
+  { return fpcompare::less (a, b); }
+};
+
+
+/**
+ * @brief Compare two FP numbers, working around x87 precision issues.
+ */
 struct greater_equal
   : public std::binary_function<double, double, bool>
 {
 /**
  * @brief Compare two FP numbers, working around x87 precision issues.
  */
+struct greater_equalf
+  : public std::binary_function<float, float, bool>
+{
+  bool
+  operator()(float a, float b) const
+  { return fpcompare::greater_equal (a, b); }
+};
+
+
+/**
+ * @brief Compare two FP numbers, working around x87 precision issues.
+ */
 struct less_equal
   : public std::binary_function<double, double, bool>
 {
 };
 
 
+/**
+ * @brief Compare two FP numbers, working around x87 precision issues.
+ */
+struct less_equalf
+  : public std::binary_function<float, float, bool>
+{
+  bool
+  operator()(float a, float b) const
+  { return fpcompare::less_equal (a, b); }
+};
+
+
 } // namespace fpcompare_fn
 } // namespace CxxUtils
 

test/fpcompare_test.cxx

 {
   tester (double x) : m_x (x) {}
   double pt() const;
+  double ptf() const { return pt(); }
   double m_x;
 };
 
   assert (   less_equal (a->pt(), b->pt()) ); // FAILX
   assert (   less_equal (a->pt(), c->pt()) );
   assert ( ! less_equal (c->pt(), a->pt()) );
+
+  //********************************************************
+
+  assert ( ! greater (a->ptf(), b->ptf()) );
+  assert (   greater (c->ptf(), b->ptf()) );
+  assert ( ! greater (b->ptf(), c->ptf()) );
+  
+  assert ( ! less (a->ptf(), b->ptf()) );
+  assert (   less (a->ptf(), c->ptf()) );
+  assert ( ! less (c->ptf(), a->ptf()) );
+
+  assert (   greater_equal (a->ptf(), b->ptf()) );
+  assert (   greater_equal (c->ptf(), b->ptf()) );
+  assert ( ! greater_equal (b->ptf(), c->ptf()) );
+
+  assert (   less_equal (a->ptf(), b->ptf()) );
+  assert (   less_equal (a->ptf(), c->ptf()) );
+  assert ( ! less_equal (c->ptf(), a->ptf()) );
 }
 
 
   assert (   le (a->pt(), b->pt()) ); // FAILX
   assert (   le (a->pt(), c->pt()) );
   assert ( ! le (c->pt(), a->pt()) );
+
+  //********************************************************
+
+  greaterf gtf;
+  assert ( ! gtf (a->ptf(), b->ptf()) );
+  assert (   gtf (c->ptf(), b->ptf()) );
+  assert ( ! gtf (b->ptf(), c->ptf()) );
+
+  lessf ltf;
+  assert ( ! ltf (a->ptf(), b->ptf()) );
+  assert (   ltf (a->ptf(), c->ptf()) );
+  assert ( ! ltf (c->ptf(), a->ptf()) );
+
+  greater_equalf gef;
+  assert (   gef (a->ptf(), b->ptf()) );
+  assert (   gef (c->ptf(), b->ptf()) );
+  assert ( ! gef (b->ptf(), c->ptf()) );
+
+  less_equalf lef;
+  assert (   lef (a->ptf(), b->ptf()) );
+  assert (   lef (a->ptf(), c->ptf()) );
+  assert ( ! lef (c->ptf(), a->ptf()) );
 }