[goffice] Constants: improve double math checking.



commit 8dd94aff02de2cc6db1e9ed412f9075972c7da7e
Author: Morten Welinder <terra gnome org>
Date:   Tue Dec 5 22:22:47 2017 -0500

    Constants: improve double math checking.
    
    Interesting conclusions:
    
    Computing sqrt(2pi) as double yields the right value?  no
    Computing sqrt(2)*sqrt(pi) as double yields the right value?  yes
    Computing 1/sqrt(2pi) as double yields the right value?  yes
    
    So 1/sqrt(2pi) is correct even though sqrt(2pi) is not.

 ChangeLog         |    3 +++
 tests/constants.c |   30 ++++++++++++++++++++++++++----
 2 files changed, 29 insertions(+), 4 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 6bcff9b..c799891 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2017-12-05  Morten Welinder  <terra gnome org>
 
+       * tests/constants.c (main): Improve checking if double math
+       produces the right values for direct and inverse.
+
        * goffice/gtk/goffice-gtk.c (_go_gtk_widget_add_css_provider):
        Install the css provider screen-wide.
 
diff --git a/tests/constants.c b/tests/constants.c
index 45e0a9f..9a8f499 100644
--- a/tests/constants.c
+++ b/tests/constants.c
@@ -158,6 +158,9 @@ qmlogabs (GOQuad const *qx)
        return -go_quad_value (&qy);
 }
 
+static double last_direct;
+static double last_inverse;
+static gboolean last_need_nl;
 
 static void
 examine_constant (const char *descr, GOQuad const *qc)
@@ -170,7 +173,7 @@ examine_constant (const char *descr, GOQuad const *qc)
        g_printerr ("Examining constant %s\n", descr);
        g_printerr ("\n");
 
-       dc = go_quad_value (qc);
+       last_direct = dc = go_quad_value (qc);
        g_printerr ("Value: ");
        print_decimal (qc);
        g_printerr ("\n");
@@ -190,7 +193,7 @@ examine_constant (const char *descr, GOQuad const *qc)
        // For the inverse we assume that using double-double is good enough to
        // answer our question.
        go_quad_div (&qic, &go_quad_one, qc);
-       dic = go_quad_value (&qic);
+       last_inverse = dic = go_quad_value (&qic);
        g_printerr ("Inverse value: ");
        print_decimal (&qic);
        g_printerr ("\n");
@@ -213,6 +216,17 @@ examine_constant (const char *descr, GOQuad const *qc)
 
        g_printerr ("Conclusion: for %s, use %s\n", descr,
                    (dig_direct >= dig_inverse ? "direct" : "inverse"));
+       last_need_nl = TRUE;
+}
+
+static void
+check_computable (const char *fmla, double x, gboolean direct)
+{
+       gboolean ok = (x == (direct ? last_direct : last_inverse));
+
+       g_printerr ("%sComputing %s as double yields the right value?  %s\n",
+                   (last_need_nl ? "\n" : ""), fmla, (ok ? "yes" : "no"));
+       last_need_nl = FALSE;
 }
 
 int
@@ -245,16 +259,24 @@ main (int argc, char **argv)
        go_quad_init (&qc, 5);
        go_quad_sqrt (&qc, &qc);
        examine_constant ("sqrt(5)", &qc);
+
        go_quad_sqrt (&qc, &go_quad_pi);
        examine_constant ("sqrt(pi)", &qc);
+       check_computable ("sqrt(pi)", sqrt (M_PI), TRUE);
+       check_computable ("1/sqrt(pi)", 1 / sqrt (M_PI), FALSE);
+       check_computable ("sqrt(1/pi)", sqrt (1 / M_PI), FALSE);
+
        go_quad_sqrt (&qc, &go_quad_2pi);
        examine_constant ("sqrt(2pi)", &qc);
+       check_computable ("sqrt(2pi)", sqrt (2 * M_PI), TRUE);
+       check_computable ("sqrt(2)*sqrt(pi)", sqrt (2) * sqrt (M_PI), TRUE);
+       check_computable ("1/sqrt(2pi)", 1 / sqrt (2 * M_PI), FALSE);
 
        go_quad_init (&qc, 180);
        go_quad_div (&qc, &qc, &go_quad_pi);
        examine_constant ("180/pi", &qc);
-       g_printerr ("180/pi computed as double: %.55g\n", 180 / M_PI);
-       g_printerr ("pi/180 computed as double: %.55g\n", M_PI / 180);
+       check_computable ("180/pi", 180 / M_PI, TRUE);
+       check_computable ("pi/180", M_PI / 180, FALSE);
 
        go_quad_end (state);
 


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