Commit 6fc80e8bf08e7e43eb5b68d6d2d352759780b330

Thomas de Grivel 2024-03-04T20:10:01

call ratio_simplify in ratio_mul and ratio_div, add test operations on ratios

diff --git a/.ic3_history b/.ic3_history
index 7e0499f..56c52c6 100644
--- a/.ic3_history
+++ b/.ic3_history
@@ -1,25 +1,3 @@
-m(^name)
-m(^ name)
-m = macro (name) { quote "Hello, " + unquote name + " !" }
-name = "Plop"
-m(name)
-m = macro (name) { quote "Hello, " + unquote name + " !" }
-name = "Plop"
-m(name)
-m(^ name)
-name
-type(name)
-m(name)
-n = "123"
-m(n)
-n
-m(n)
-m = macro (name) { quote "Hello, " + unquote name + " !" }
-n = "123"
-m(n)
-1 + 1
-m = macro (name) { quote "Hello, " + unquote name + " !" }
-m("Patrice")
 m = macro (name) { quote "Hello, " + (unquote name) + " !" }
 m("Patrice")
 variable = "Patrice"
@@ -97,3 +75,25 @@ end
 1/2 * 2/3
 1/2 - 2/3
 1/2 / -2/3
+1/2 + 2/3
+1/2 * 2/3
+1/2 / 2/3
+1/2 / 3/2
+1/2 + 2/3
+3/2 + 2/3
+3/2 + 4/3
+1/2 + 4/3
+1/2 + 2/3
+1/2 + 1/3
+3/2 + 1/3
+4/2 + 1/3
+4/1 + 1/3
+4/1 + 2/3
+1/6 + 2/3
+1/6 + 1/3
+1/6 - 1/3
+1/3 - 1/6
+1/3 * 1/6
+2/3 * 2/6
+2/3 * 5/6
+2/3 / 5/6
diff --git a/libc3/list_init.c b/libc3/list_init.c
index c11c7f6..979911e 100644
--- a/libc3/list_init.c
+++ b/libc3/list_init.c
@@ -1,15 +1,4 @@
-/* c3
- * Copyright 2022-2024 kmx.io <contact@kmx.io>
- *
- * Permission is hereby granted to use this software granted the above
- * copyright notice and this permission paragraph are included in all
- * copies and substantial portions of this software.
- *
- * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
- * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
- * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
- * THIS SOFTWARE.
- */
+
 #include "assert.h"
 #include <string.h>
 #include "array.h"
diff --git a/libc3/list_init.h b/libc3/list_init.h
index 6414bc6..372b6ef 100644
--- a/libc3/list_init.h
+++ b/libc3/list_init.h
@@ -1,15 +1,4 @@
-/* c3
- * Copyright 2022-2024 kmx.io <contact@kmx.io>
- *
- * Permission is hereby granted to use this software granted the above
- * copyright notice and this permission paragraph are included in all
- * copies and substantial portions of this software.
- *
- * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
- * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
- * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
- * THIS SOFTWARE.
- */
+
 #ifndef LIBC3_LIST_INIT_H
 #define LIBC3_LIST_INIT_H
 
diff --git a/libc3/ratio.c b/libc3/ratio.c
index 0ebd1f1..fd76f1f 100644
--- a/libc3/ratio.c
+++ b/libc3/ratio.c
@@ -93,20 +93,23 @@ s_ratio * ratio_div (const s_ratio *a, const s_ratio *b,
     return NULL;
   }
   if (integer_is_negative(&tmp.denominator)) {
-    if (! integer_neg(&tmp.numerator, &tmp2.numerator)) {
-      ratio_clean(&tmp);
+    tmp2 = tmp;
+    if (! integer_neg(&tmp2.numerator, &tmp.numerator)) {
+      ratio_clean(&tmp2);
       return NULL;
     }
-    if (! integer_neg(&tmp.denominator, &tmp2.denominator)) {
-      integer_clean(&tmp2.numerator);
-      ratio_clean(&tmp);
+    if (! integer_neg(&tmp2.denominator, &tmp.denominator)) {
+      integer_clean(&tmp.numerator);
+      ratio_clean(&tmp2);
       return NULL;
     }
-    *dest = tmp2;
+    ratio_clean(&tmp2);
+  }
+  if (! ratio_simplify(&tmp, dest)) {
     ratio_clean(&tmp);
+    return NULL;
   }
-  else
-    *dest = tmp;
+  ratio_clean(&tmp);
   return dest;
 }
 
@@ -259,7 +262,11 @@ s_ratio * ratio_mul (const s_ratio *a, const s_ratio *b,
     integer_clean(&tmp.numerator);
     return NULL;
   }
-  *dest = tmp;
+  if (! ratio_simplify(&tmp, dest)) {
+    ratio_clean(&tmp);
+    return NULL;
+  }
+  ratio_clean(&tmp);
   return dest;
 }
 
@@ -286,22 +293,15 @@ s_ratio * ratio_simplify (s_ratio *r, s_ratio *dest)
   assert(r);
   assert(dest);
   assert(integer_is_positive(&r->denominator));
-  if (! integer_init(&tmp.numerator))
-    return NULL;
-  if (! integer_init(&tmp.denominator)) {
-    integer_clean(&tmp.numerator);
+  if (! integer_gcd(&r->numerator, &r->denominator, &gcd))
     return NULL;
-  }
-  if (! integer_gcd(&r->numerator, &r->denominator, &gcd)) {
-    ratio_clean(&tmp);
-    return NULL;
-  }
   if (! integer_div(&r->numerator, &gcd, &tmp.numerator)) {
-    ratio_clean(&tmp);
+    integer_clean(&gcd);
     return NULL;
   }
   if (! integer_div(&r->denominator, &gcd, &tmp.denominator)) {
-    ratio_clean(&tmp);
+    integer_clean(&tmp.numerator);
+    integer_clean(&gcd);
     return NULL;
   }
   integer_clean(&gcd);
@@ -364,15 +364,13 @@ s_ratio * ratio_sub (const s_ratio *a, const s_ratio *b,
     ratio_clean(&tmp);
     return NULL;
   }
+  integer_clean(&i);
+  integer_clean(&j);
   if (! integer_mul(&a->denominator, &b->denominator,
                     &tmp.denominator)) {
-    integer_clean(&i);
-    integer_clean(&j);
     ratio_clean(&tmp);
     return NULL;
   }
-  integer_clean(&i);
-  integer_clean(&j);
   if (! ratio_simplify(&tmp, dest)) {
     ratio_clean(&tmp);
     return NULL;
diff --git a/libc3/tag_init.c b/libc3/tag_init.c
index f974f47..8699cc7 100644
--- a/libc3/tag_init.c
+++ b/libc3/tag_init.c
@@ -1,15 +1,4 @@
-/* c3
- * Copyright 2022-2024 kmx.io <contact@kmx.io>
- *
- * Permission is hereby granted to use this software granted the above
- * copyright notice and this permission paragraph are included in all
- * copies and substantial portions of this software.
- *
- * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
- * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
- * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
- * THIS SOFTWARE.
- */
+
 #include "alloc.h"
 #include "assert.h"
 #include "array.h"
diff --git a/libc3/tag_init.h b/libc3/tag_init.h
index b99d94a..91589f3 100644
--- a/libc3/tag_init.h
+++ b/libc3/tag_init.h
@@ -1,15 +1,4 @@
-/* c3
- * Copyright 2022-2024 kmx.io <contact@kmx.io>
- *
- * Permission is hereby granted to use this software granted the above
- * copyright notice and this permission paragraph are included in all
- * copies and substantial portions of this software.
- *
- * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
- * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
- * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
- * THIS SOFTWARE.
- */
+
 #ifndef LIBC3_TAG_INIT_H
 #define LIBC3_TAG_INIT_H
 
diff --git a/test/ic3/ratio.in b/test/ic3/ratio.in
index 4b5e317..ed2b89e 100644
--- a/test/ic3/ratio.in
+++ b/test/ic3/ratio.in
@@ -28,3 +28,11 @@ quote - -1/3
 - -1/3
 quote - -123456/3
 - -123456/3
+quote 1/3 + 1/6
+1/3 + 1/6
+quote 1/3 - 1/6
+1/3 - 1/6
+quote 2/3 * 5/6
+2/3 * 5/6
+quote 2/3 / 5/6
+2/3 / 5/6
diff --git a/test/ic3/ratio.out.expected b/test/ic3/ratio.out.expected
index bcc1c52..56b493e 100644
--- a/test/ic3/ratio.out.expected
+++ b/test/ic3/ratio.out.expected
@@ -28,3 +28,11 @@
 1/3
 - -123456/3
 123456/3
+1/3 + 1/6
+1/2
+1/3 - 1/6
+1/6
+2/3 * 5/6
+5/9
+2/3 / 5/6
+4/5