]> ocean-lang.org Git - ocean/commitdiff
oceani: use more precision when printing numbers
authorNeilBrown <neil@brown.name>
Fri, 3 Dec 2021 07:58:43 +0000 (18:58 +1100)
committerNeilBrown <neil@brown.name>
Fri, 3 Dec 2021 08:14:44 +0000 (19:14 +1100)
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 <neil@brown.name>
csrc/oceani-tests.mdc
csrc/oceani.mdc

index d65b6f97d8593548f13648e3216f09f80a34a44b..359c8e504675d0dc36ceed1f4a0f880da8130d26 100644 (file)
@@ -136,7 +136,7 @@ calculations on them.
 
 ###### output: valvar
 
 
 ###### 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
        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
                print a, a/a
 
 ###### output: setvar
-       1.07374e+09 1
+       1073741824 1
 
 Now some contants
 
 
 Now some contants
 
@@ -181,7 +181,7 @@ Now some contants
 ###### output: consts
        Hello World, what lovely oceans you have!
        are there 5 ?
 ###### 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
 
 
 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
        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
        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
        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
        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!
 
 ###### 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
        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"
 
 ###### test list
        oceani_tests += "insert_sort"
index 5bf754e8fe52ced6af10691f0519f2c47f2c18fc..180c36fb4d9bb059f30ca0f2b101f6d1b4a1946c 100644 (file)
@@ -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);
                        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;
                        }
                        mpf_clear(fl);
                        break;
                        }
@@ -1239,6 +1239,7 @@ executable.
                struct val *v = cast(val, e);
                if (v->vtype == Tstr)
                        printf("\"");
                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("\"");
                print_value(v->vtype, &v->val, stdout);
                if (v->vtype == Tstr)
                        printf("\"");