Re: [xslt] floating point conversion



Nick Wellnhofer writes:
>That the second number is different seems to be a bug in libxml2. The 
>implementations of xmlXPathStringEvalNumber and xmlXPathCompNumber are 
>slightly different AFAICS.

Thanks for the pointer.  The current xmlXPathCompNumber logic does
multiple divisions, which accumulates rounding errors.  Here's the
fix:

--- ../old/libxml2-2.6.32/xpath.c       2008-04-03 00:37:47.000000000 -0400
+++ xpath.c     2010-09-10 17:38:32.000000000 -0400
@@ -10065,15 +10065,23 @@
     }
 #endif
     if (CUR == '.') {
+       int v, frac = 0;
+       double fraction = 0;
+
         NEXT;
         if (((CUR < '0') || (CUR > '9')) && (!ok)) {
             XP_ERROR(XPATH_NUMBER_ERROR);
         }
-        while ((CUR >= '0') && (CUR <= '9')) {
-            mult /= 10;
-            ret = ret + (CUR - '0') * mult;
+        while ((CUR >= '0') && (CUR <= '9') && (frac < MAX_FRAC)) {
+           v = (CUR - '0');
+           fraction = fraction * 10 + v;
+           frac = frac + 1;
             NEXT;
-        }
+       }
+       fraction /= my_pow10[frac];
+       ret = ret + fraction;
+       while ((CUR >= '0') && (CUR <= '9'))
+           NEXT;
     }
     if ((CUR == 'e') || (CUR == 'E')) {
         NEXT;

I'll bugzilla it.

Thanks,
 Phil


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]