[goffice] Math: fix rounding.



commit ddd0150aeefa812f78371bcf86b717ea3a3282bd
Author: Morten Welinder <terra gnome org>
Date:   Wed Apr 2 19:47:03 2014 -0400

    Math: fix rounding.
    
    Surprise: 2^51+0.5 = 2^51+1.  So don't do that.

 ChangeLog              |    3 +++
 NEWS                   |    1 +
 goffice/math/go-math.c |   13 ++++++++++++-
 3 files changed, 16 insertions(+), 1 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index df81feb..f974ee2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2014-04-02  Morten Welinder  <terra gnome org>
 
+       * goffice/math/go-math.c (go_fake_round): Fix problem for 2^51 and
+       like integers.
+
        * tests/test-format.c: New test program.
 
        * goffice/utils/go-format.c (go_format_output_conditional_to_odf):
diff --git a/NEWS b/NEWS
index 1fdf864..7c8c1a0 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,7 @@ Morten:
        * Fix plugin deactivation.
        * Plug leaks.
        * New function go_ascii_dtoa.
+       * Fix rounding problem.
 
 --------------------------------------------------------------------------
 goffice 0.10.13:
diff --git a/goffice/math/go-math.c b/goffice/math/go-math.c
index 7e5efde..ffaaddb 100644
--- a/goffice/math/go-math.c
+++ b/goffice/math/go-math.c
@@ -269,7 +269,18 @@ go_fake_ceil (double x)
 double
 go_fake_round (double x)
 {
-       double y = go_fake_floor (fabs (x) + 0.5);
+       double y;
+
+       x = go_d2d (x);
+
+       if (x == floor (x))
+               return x;
+
+       /*
+        * Adding a half here is ok.  The only problematic non-integer
+        * case is nextafter(0.5,-1) for which we want to produce 1 here.
+        */
+       y = go_fake_floor (fabs (x) + 0.5);
        return (x < 0) ? -y : y;
 }
 


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