core: Avoid Floating point exception dividing negative numbers.

This fixes

    kaem --verbose --strict
    bin/mes-m2 -c '(display (/ -1 1))'

* src/math.c (divide): Use signed division.
This commit is contained in:
Jan (janneke) Nieuwenhuizen 2020-12-29 22:39:31 +01:00
parent d984618a48
commit c16bdb576b
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273

View file

@ -156,18 +156,32 @@ divide (struct scm *x) /*:((name . "/") (arity . n)) */
n = v;
x = cdr (x);
}
int sign_p = 0;
size_t u = n;
if (n < 0)
{
sign_p = 1;
u = -n;
}
size_t w;
while (x != cell_nil)
{
i = car (x);
assert_number ("divide", i);
v = i->value;
sign_p = sign_p && v > 0 || !sign_p && v < 0;
w = v;
if (v == 0)
error (cstring_to_symbol ("divide-by-zero"), x);
if (n == 0)
if (u == 0)
break;
n = n / v;
if (w != 1)
u = u / w;
x = cdr (x);
}
n = u;
if (sign_p)
n = -n;
return make_number (n);
}