Commit 16b4888b03b369881ff9174c7c2431777f95e259

Thomas de Grivel 2024-11-20T20:06:05

fix floats

diff --git a/.ikc3_history b/.ikc3_history
index f4faa5c..ae9be53 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,17 +1,3 @@
-type(1 + 100000000000000000000000000000000000000000000000000000000)
-type(1 + 1)
-type(255)
-type(256)
-type(1)
-type(1 + 256)
-1 + 256
-(U8) 256
-(U8) 257
-(U16) 1
-(Str) 1
-(Str) 100000000000000000000000000000000000000000000000000000000
-(Str) 100000
-(Str) %{a: 1, b: 2}
 puts(inspect(%{a: 1, b: 2}))
 puts(inspect(quote if true do 1 else %{a: 1, b: 2} end))
 puts(quote if true do 1 else %{a: 1, b: 2} end)
@@ -97,3 +83,17 @@ Time.to_str(Time.from_str("2024-10-31 23:00:00"))
 List.map([1, 2, 3], fn (x) { x * 2 })
 Facts.collect_with(Facts.env_db(), [[op = ?, :is_a, :operator]], fn (fact) { op })
 1 + 1
+123
+type(123)
+type(1234)
+type(12345)
+type(123456)
+type(123456789)
+type(123456789012)
+type(1234567890123456789)
+type(123456789012345678901234)
+type(123456789012345678901234/2)
+123456789012345678901234/2
+123456789012345678901234/2 * 0.1
+(123456789012345678901234/2) * 0.1
+(F128) 123456789012345678901234/2 * 0.1
diff --git a/lib/kc3/0.1/f128.facts b/lib/kc3/0.1/f128.facts
index 4f7cba0..296ec9d 100644
--- a/lib/kc3/0.1/f128.facts
+++ b/lib/kc3/0.1/f128.facts
@@ -3,3 +3,11 @@
 replace {F128, :is_a, :module}
 replace {F128, :symbol, F128.cast}
 replace {F128.cast, :symbol_value, cfn F128 "f128_init_cast" (Result, Sym, Tag)}
+add {F128, :symbol, F128.pi}
+replace {F128.pi, :symbol_value, (F128) 3.141592653589793238462643383279502}
+add {F128, :symbol, F128.cos}
+replace {F128.cos, :symbol_value, cfn F128 "cosl" (F128)}
+add {F128, :symbol, F128.sin}
+replace {F128.sin, :symbol_value, cfn F128 "sinl" (F128)}
+add {F128, :symbol, F128.tan}
+replace {F128.tan, :symbol_value, cfn F128 "tanl" (F128)}
diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index 062f474..bd72eaf 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -1517,8 +1517,15 @@ sw buf_inspect_f32 (s_buf *buf, const f32 *f)
   assert(f);
   exp = 0;
   x = *f;
-  if (x == 0.0)
-    return buf_write_1(buf, "0.0f");
+  if ((r = buf_write_1(buf, "(F32) ")) < 0)
+    return r;
+  result += r;
+  if (x == 0.0f) {
+    if ((r = buf_write_1(buf, "0.0")) < 0)
+      return r;
+    result += r;
+    return result;
+  }
   if (x < 0) {
     if ((r = buf_write_1(buf, "-")) <= 0)
       return r;
@@ -1586,8 +1593,15 @@ sw buf_inspect_f32_size (s_pretty *pretty, const f32 *f)
   assert(f);
   exp = 0;
   x = *f;
-  if (x == 0.0)
-    return buf_write_1_size(pretty, "0.0f");
+  if ((r = buf_write_1_size(pretty, "(F32) ")) < 0)
+    return r;
+  result += r;
+  if (x == 0.0f) {
+    if ((r = buf_write_1_size(pretty, "0.0")) < 0)
+      return r;
+    result += r;
+    return result;
+  }
   if (x < 0) {
     if ((r = buf_write_1_size(pretty, "-")) <= 0)
       return r;
@@ -1651,8 +1665,15 @@ sw buf_inspect_f64 (s_buf *buf, const f64 *f)
   assert(f);
   exp = 0.0;
   x = *f;
-  if (x == 0.0)
-    return buf_write_1(buf, "0.0");
+  if ((r = buf_write_1(buf, "(F64) ")) < 0)
+    return r;
+  result += r;
+  if (x == 0.0f) {
+    if ((r = buf_write_1(buf, "0.0")) < 0)
+      return r;
+    result += r;
+    return result;
+  }
   if (x < 0) {
     if ((r = buf_write_1(buf, "-")) <= 0)
       return r;
@@ -1717,8 +1738,15 @@ sw buf_inspect_f64_size (s_pretty *pretty, const f64 *f)
   assert(f);
   exp = 0.0;
   x = *f;
-  if (x == 0.0)
-    return buf_write_1_size(pretty, "0.0");
+  if ((r = buf_write_1_size(pretty, "(F64) ")) < 0)
+    return r;
+  result += r;
+  if (x == 0.0f) {
+    if ((r = buf_write_1_size(pretty, "0.0")) < 0)
+      return r;
+    result += r;
+    return result;
+  }
   if (x < 0) {
     if ((r = buf_write_1_size(pretty, "-")) <= 0)
       return r;
@@ -1779,8 +1807,15 @@ sw buf_inspect_f128 (s_buf *buf, const f128 *x)
   assert(x);
   exp = 0.0;
   y = *x;
-  if (y == 0.0)
-    return buf_write_1(buf, "0.0");
+  if ((r = buf_write_1(buf, "(F128) ")) < 0)
+    return r;
+  result += r;
+  if (y == 0.0) {
+    if ((r = buf_write_1(buf, "0.0")) < 0)
+      return r;
+    result += r;
+    return result;
+  }
   if (y < 0) {
     if ((r = buf_write_1(buf, "-")) <= 0)
       return r;
@@ -1826,7 +1861,7 @@ sw buf_inspect_f128 (s_buf *buf, const f128 *x)
         return r;
       result += r;
     }
-    if ((r = buf_inspect_s64(buf, &exp)) <= 0)
+    if ((r = buf_inspect_s64_decimal(buf, &exp)) <= 0)
       return r;
     result += r;
   }
diff --git a/libkc3/buf_parse.c b/libkc3/buf_parse.c
index 479f424..79063c1 100644
--- a/libkc3/buf_parse.c
+++ b/libkc3/buf_parse.c
@@ -1532,13 +1532,21 @@ sw buf_parse_f32 (s_buf *buf, f32 *dest)
   assert(buf);
   assert(dest);
   buf_save_init(buf, &save);
+  if ((r = buf_read_1(buf, "(F32)")) < 0)
+    goto restore;
+  result += r;
+  if (r > 0) {
+    if ((r = buf_ignore_spaces(buf)) < 0)
+      goto restore;
+    result += r;
+  }
   if ((r = buf_parse_digit_dec(buf, &digit)) <= 0)
     goto restore;
-  tmp = digit;
   result += r;
+  tmp = digit;
   while ((r = buf_parse_digit_dec(buf, &digit)) > 0) {
-    tmp = tmp * 10 + digit;
     result += r;
+    tmp = tmp * 10 + digit;
   }
   if (r < 0 ||
       (r = buf_read_1(buf, ".")) <= 0)
@@ -1563,9 +1571,9 @@ sw buf_parse_f32 (s_buf *buf, f32 *dest)
       exp = exp * 10 + digit;
       result += r;
     }
-    tmp *= pow(10, exp_sign * exp);
+    tmp *= powf(10, exp_sign * exp);
   }
-  if ((r = buf_read_1(buf, "f")) <= 0)
+  if ((r = buf_read_1(buf, "f")) < 0)
     goto restore;
   result += r;
   if ((r = buf_peek_character_utf8(buf, &c)) > 0 &&
@@ -1595,6 +1603,14 @@ sw buf_parse_f64 (s_buf *buf, f64 *dest) {
   assert(buf);
   assert(dest);
   buf_save_init(buf, &save);
+  if ((r = buf_read_1(buf, "(F64)")) < 0)
+    goto restore;
+  result += r;
+  if (r > 0) {
+    if ((r = buf_ignore_spaces(buf)) < 0)
+      goto restore;
+    result += r;
+  }
   if ((r = buf_parse_digit_dec(buf, &digit)) <= 0)
     goto restore;
   tmp = digit;
@@ -1625,15 +1641,81 @@ sw buf_parse_f64 (s_buf *buf, f64 *dest) {
       r = buf_read_1(buf, "+");
       if (r < 0)
         goto restore;
-      if (r > 0) {
+      result += r;
+      while ((r = buf_parse_digit_dec(buf, &digit)) > 0) {
         result += r;
+        exp = exp * 10 + digit;
       }
+    }
+    tmp *= pow(10, exp_sign * exp);
+  }
+  *dest = tmp;
+  r = result;
+  goto clean;
+  restore:
+  buf_save_restore_rpos(buf, &save);
+  clean:
+  buf_save_clean(buf, &save);
+  return r;
+}
+
+sw buf_parse_f128 (s_buf *buf, f128 *dest) {
+  sw r;
+  sw result = 0;
+  u8 digit;
+  s_buf_save save;
+  f128 tmp = 0;
+  s64 exp = 0;
+  s8  exp_sign = 1;
+  uw i;
+  assert(buf);
+  assert(dest);
+  buf_save_init(buf, &save);
+  if ((r = buf_read_1(buf, "(F128)")) < 0)
+    goto restore;
+  result += r;
+  if (r > 0) {
+    if ((r = buf_ignore_spaces(buf)) < 0)
+      goto restore;
+    result += r;
+  }
+  if ((r = buf_parse_digit_dec(buf, &digit)) <= 0)
+    goto restore;
+  tmp = digit;
+  result += r;
+  while ((r = buf_parse_digit_dec(buf, &digit)) > 0) {
+    tmp = tmp * 10 + digit;
+    result += r;
+  }
+  if (r < 0 ||
+      (r = buf_read_1(buf, ".")) <= 0)
+    goto restore;
+  result += r;
+  i = 10;
+  while ((r = buf_parse_digit_dec(buf, &digit)) > 0) {
+    result += r;
+    tmp += (f128) digit / i;
+    i *= 10;
+  }
+  if ((r = buf_read_1(buf, "e")) > 0) {
+    result += r;
+    if ((r = buf_read_1(buf, "-")) < 0)
+      goto restore;
+    if (r > 0) {
+      result += r;
+      exp_sign = -1;
+    }
+    else {
+      r = buf_read_1(buf, "+");
+      if (r < 0)
+        goto restore;
+      result += r;
       while ((r = buf_parse_digit_dec(buf, &digit)) > 0) {
-        exp = exp * 10 + digit;
         result += r;
+        exp = exp * 10 + digit;
       }
     }
-    tmp *= pow(10, exp_sign * exp);
+    tmp *= powl(10, exp_sign * exp);
   }
   *dest = tmp;
   r = result;
@@ -3924,6 +4006,16 @@ sw buf_parse_tag_f64 (s_buf *buf, s_tag *dest)
   return r;
 }
 
+sw buf_parse_tag_f128 (s_buf *buf, s_tag *dest)
+{
+  sw r;
+  assert(buf);
+  assert(dest);
+  if ((r = buf_parse_f128(buf, &dest->data.f128)) > 0)
+    dest->type = TAG_F128;
+  return r;
+}
+
 sw buf_parse_tag_ident (s_buf *buf, s_tag *dest)
 {
   sw r;
@@ -4007,12 +4099,15 @@ sw buf_parse_tag_number (s_buf *buf, s_tag *dest)
   sw r;
   assert(buf);
   assert(dest);
-  r = buf_parse_tag_f32(buf, dest);
+  r = buf_parse_tag_f128(buf, dest);
   if (r > 0)
     return r;
   r = buf_parse_tag_f64(buf, dest);
   if (r > 0)
     return r;
+  r = buf_parse_tag_f32(buf, dest);
+  if (r > 0)
+    return r;
   r = buf_parse_tag_ratio(buf, dest);
   if (r > 0)
     return r;