shithub: femtolisp

Download patch

ref: 7c863a1c82360858aa0a5b76b83890ddad60fcff
parent: 32172cdf6a00026675dee9be5c0fa3aef5fdff9d
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Mon Nov 30 10:46:12 EST 2020

fix truncate (does not play well with conversion to double)

--- a/builtins.c
+++ b/builtins.c
@@ -266,6 +266,8 @@
     return -1;
 }
 
+double trunc(double x);
+
 static value_t fl_truncate(value_t *args, u_int32_t nargs)
 {
     argcount("truncate", nargs, 1);
@@ -282,13 +284,16 @@
             d = *(double*)data;
         else
             return args[0];
+
+        d = trunc(d);
+
         if (d > 0) {
-            if (d > (double)U64_MAX)
-                return args[0];
+            if (d > S64_MAX)
+                return mk_double(d);
             return return_from_uint64((uint64_t)d);
         }
-        if (d > (double)S64_MAX || d < (double)S64_MIN)
-            return args[0];
+        if (d > S64_MAX || d < S64_MIN)
+            return mk_double(d);
         return return_from_int64((int64_t)d);
     }
     type_error("truncate", "number", args[0]);
--- a/operators.c
+++ b/operators.c
@@ -3,6 +3,11 @@
 #include "ieee754.h"
 
 #ifdef __plan9__
+double trunc(double x)
+{
+    modf(x, &x);
+    return x;
+}
 STATIC_INLINE double fpart(double arg)
 {
     return modf(arg, NULL);