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:
parent
d984618a48
commit
c16bdb576b
18
src/math.c
18
src/math.c
|
@ -156,18 +156,32 @@ divide (struct scm *x) /*:((name . "/") (arity . n)) */
|
||||||
n = v;
|
n = v;
|
||||||
x = cdr (x);
|
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)
|
while (x != cell_nil)
|
||||||
{
|
{
|
||||||
i = car (x);
|
i = car (x);
|
||||||
assert_number ("divide", i);
|
assert_number ("divide", i);
|
||||||
v = i->value;
|
v = i->value;
|
||||||
|
sign_p = sign_p && v > 0 || !sign_p && v < 0;
|
||||||
|
w = v;
|
||||||
if (v == 0)
|
if (v == 0)
|
||||||
error (cstring_to_symbol ("divide-by-zero"), x);
|
error (cstring_to_symbol ("divide-by-zero"), x);
|
||||||
if (n == 0)
|
if (u == 0)
|
||||||
break;
|
break;
|
||||||
n = n / v;
|
if (w != 1)
|
||||||
|
u = u / w;
|
||||||
x = cdr (x);
|
x = cdr (x);
|
||||||
}
|
}
|
||||||
|
n = u;
|
||||||
|
if (sign_p)
|
||||||
|
n = -n;
|
||||||
return make_number (n);
|
return make_number (n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue