From 1633bb8942f3f573322a48f49e0e0dec96352d01 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Fri, 3 Dec 2021 18:58:43 +1100 Subject: [PATCH] oceani: use more precision when printing numbers I really want constants to be reproduced accurately when printing a program. Maybe I should detect and store the precision used, but for now, just use a larger precision. Signed-off-by: NeilBrown --- csrc/oceani-tests.mdc | 22 +++++++++++----------- csrc/oceani.mdc | 3 ++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/csrc/oceani-tests.mdc b/csrc/oceani-tests.mdc index d65b6f9..359c8e5 100644 --- a/csrc/oceani-tests.mdc +++ b/csrc/oceani-tests.mdc @@ -136,7 +136,7 @@ calculations on them. ###### output: valvar - 23 12 35 11 276 1.91667 11 + 23 12 35 11 276 1.916666667 11 False False True True False False True 23 12 12 -23 -12 12 False True True False False False @@ -158,7 +158,7 @@ Next we change the value of variables print a, a/a ###### output: setvar - 1.07374e+09 1 + 1073741824 1 Now some contants @@ -181,7 +181,7 @@ Now some contants ###### output: consts Hello World, what lovely oceans you have! are there 5 ? - 3.14159 I like Pie but The cake is a lie + 3.141592653 I like Pie but The cake is a lie Test merging of variables from multiple cases @@ -280,13 +280,13 @@ Now we need to test if/else and some different loops Success 1 2 4 8 16 32 64 0 1.5 - 1 1.41667 - 2 1.41422 - 3 1.41421 - error is -4.51095e-12 + 1 1.416666667 + 2 1.414215686 + 3 1.414213562 + error is -4.510950445e-12 I won't calculate 20 / 0 - 20 / 3 = 6.66667 - 20 / 6 = 3.33333 + 20 / 3 = 6.666666667 + 20 / 6 = 3.333333333 I won't calculate 20 / 9 Close enough higher @@ -392,7 +392,7 @@ Here I break it into two parts, keeping the array code separate. GCD of 55 and 33 is 11 Fibonacci: 1 1 2 3 5 8 13 21 34 55 89 144 hello was the str - Closest I found was 77.3438 + Closest I found was 77.34375 ###### output: sayhello,12,60,there,False Hello World, what lovely oceans you have! @@ -402,7 +402,7 @@ Here I break it into two parts, keeping the array code separate. GCD of 12 and 60 is 12 Fibonacci: 1 1 2 3 5 8 13 21 34 55 89 144 I found the str over there - Closest I found was 77.3438 + Closest I found was 77.34375 ###### test list oceani_tests += "insert_sort" diff --git a/csrc/oceani.mdc b/csrc/oceani.mdc index 5bf754e..180c36f 100644 --- a/csrc/oceani.mdc +++ b/csrc/oceani.mdc @@ -1125,7 +1125,7 @@ A separate function encoding these cases will simplify some code later. mpf_t fl; mpf_init2(fl, 20); mpf_set_q(fl, v->num); - gmp_fprintf(f, "%Fg", fl); + gmp_fprintf(f, "%.10Fg", fl); mpf_clear(fl); break; } @@ -1239,6 +1239,7 @@ executable. struct val *v = cast(val, e); if (v->vtype == Tstr) printf("\""); + // FIXME how to ensure numbers have same precision. print_value(v->vtype, &v->val, stdout); if (v->vtype == Tstr) printf("\""); -- 2.43.0