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:
parent
aac33c44b7
commit
6a25b8d3aa
30
src/math.c
30
src/math.c
|
@ -1,6 +1,6 @@
|
||||||
/* -*-comment-start: "//";comment-end:""-*-
|
/* -*-comment-start: "//";comment-end:""-*-
|
||||||
* GNU Mes --- Maxwell Equations of Software
|
* GNU Mes --- Maxwell Equations of Software
|
||||||
* Copyright © 2016,2017,2018,2019,2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
* Copyright © 2016,2017,2018,2019,2020,2021 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
||||||
* Copyright © 2021 W. J. van der Laan <laanwj@protonmail.com>
|
* Copyright © 2021 W. J. van der Laan <laanwj@protonmail.com>
|
||||||
*
|
*
|
||||||
* This file is part of GNU Mes.
|
* This file is part of GNU Mes.
|
||||||
|
@ -190,16 +190,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 *
|
||||||
|
|
Loading…
Reference in a new issue