Author: wyoung
Date: Fri Dec 28 09:33:06 2007
New Revision: 2030

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2030&view=rev
Log:
sql_cmp(float, float) and sql_cmp(double, double), which make up part of
the implementation of less-than comparison for SSQLS no longer do exact
equality tests.  They now offer a user-settable threshold, which if the
absolute value of the difference between the values is under this
threshold, causes the test to return "equal".

Modified:
    trunk/ChangeLog
    trunk/Wishlist
    trunk/lib/custom.pl

Modified: trunk/ChangeLog
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/ChangeLog?rev=2030&r1=2029&r2=2030&view=diff
==============================================================================
--- trunk/ChangeLog (original)
+++ trunk/ChangeLog Fri Dec 28 09:33:06 2007
@@ -39,6 +39,17 @@
         undocumented and unexemplified for a very long time now.
     
       - You can now use 'bool' type in SSQLS.
+
+      - SSQLSes have always had a < operator defined for them for
+        various reasons, which compares the first N fields, where N
+        is the second parameter to sql_create_X.  Earlier versions
+        of MySQL++ did comparisons using exact equality for all data
+        types including floating-point.  Now there is a configurable
+        value at the top of lib/custom.pl that lets you set the
+        minimum threshold between two floating point values such
+        that they will be considered equal.  It's also possible to
+        redefine this threshold on a per-module basis by #defining
+        MYSQLPP_FP_MIN_DELTA before #including custom.h.
 
     o You can now use mysqlpp::null as a template query parameter to
       get a SQL null.

Modified: trunk/Wishlist
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/Wishlist?rev=2030&r1=2029&r2=2030&view=diff
==============================================================================
--- trunk/Wishlist (original)
+++ trunk/Wishlist Fri Dec 28 09:33:06 2007
@@ -82,11 +82,6 @@
 
          http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
 
-    o Define custom template for sql_double et al., with the comparison
-      precision being the parameter:
-
-        http://lists.mysql.com/plusplus/3984
-
     o Define operator<< for Fields, Row, StoreQueryResult, etc.  In other
       words, there should be a way to get a user-readable version of
       received data without a lot of code.  CSV format by default, and

Modified: trunk/lib/custom.pl
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/custom.pl?rev=2030&r1=2029&r2=2030&view=diff
==============================================================================
--- trunk/lib/custom.pl (original)
+++ trunk/lib/custom.pl Fri Dec 28 09:33:06 2007
@@ -35,6 +35,14 @@
 # only if you must.
 my $max_data_members = 25;
 
+# To make comparisons between floating point values, we subtract them,
+# take the absolute value, and test to see if that delta is under this
+# value.  If it is, we call the two values "equal".  Change this as fits
+# your need for precision.  Note that we express it as a string because
+# we want the value copied literally into custom.h, not "preprocessed" 
+# by Perl as a floating-point value.
+my $fp_min_delta = "0.00001";
+
 
 # No user-serviceable parts below.
 
@@ -61,6 +69,18 @@
 #include "sql_types.h"
 
 #include <string>
+
+#include <math.h>
+
+// Smallest difference between two floating point numbers recognized
+// in making comparisons.  If the absolute delta is under this
+// threshold, the two values are considered equal.  You can either
+// override this permanently by changing custom.pl, or you can do it
+// on a case-by-case basis at compile time by defining this to another
+// value before #including this header.
+#if !defined(MYSQLPP_FP_MIN_DELTA)
+#      define MYSQLPP_FP_MIN_DELTA $fp_min_delta
+#endif
 
 namespace mysqlpp {
 
@@ -113,13 +133,26 @@
 ---
 }
 
[EMAIL PROTECTED] = ("double", "float", "longlong", "ulonglong");
[EMAIL PROTECTED] = ("longlong", "ulonglong");
 foreach my $type (@types) {
     print OUT0 << "---";
 
 inline int sql_cmp($type a, $type b) 
 {
        if (a == b) return 0;
+       if (a <  b) return -1;
+       return 1;
+}
+---
+}      
+
[EMAIL PROTECTED] = ("double", "float");
+foreach my $type (@types) {
+    print OUT0 << "---";
+
+inline int sql_cmp($type a, $type b) 
+{
+       if (fabs(a - b) < MYSQLPP_FP_MIN_DELTA) return 0;
        if (a <  b) return -1;
        return 1;
 }


_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits

Reply via email to