core: Avoid Floating point exception dividing negative numbers.

This fixes

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

* src/math.c (modulo): Use unsigned division.
This commit is contained in:
Jan (janneke) Nieuwenhuizen 2021-01-01 09:04:39 +01:00
parent 7c3d55323d
commit 87e73e5b01
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
1 changed files with 19 additions and 9 deletions

View File

@ -188,16 +188,26 @@ modulo (struct scm *a, struct scm *b)
{ {
assert_number ("modulo", a); assert_number ("modulo", a);
assert_number ("modulo", b); assert_number ("modulo", b);
long x = a->value; long n = a->value;
long y = b->value; long v = b->value;
if (y == 0) if (v == 0)
error (cstring_to_symbol ("divide-by-zero"), a); error (cstring_to_symbol ("divide-by-zero"), a);
while (x < 0) int sign_p = 0;
x = x + y; size_t w = v;
if (x != 0) if (v < 0)
x = x % y; {
sign_p = 1;
return make_number (x); w = -v;
}
while (n < 0)
n = n + w;
size_t u = n;
if (u != 0)
u = u % w;
n = u;
if (sign_p)
n = -n;
return make_number (n);
} }
struct scm * struct scm *