🐐 Compiler fix

This commit is contained in:
2026-04-22 13:24:55 +02:00
parent 24b187c23c
commit f5b8986681
2 changed files with 28 additions and 3 deletions

View File

@@ -380,7 +380,9 @@ static long gcd(long a, long b) {
static CentValue frac_reduce(long num, long den) {
if (den < 0) { num = -num; den = -den; }
long g = gcd(num < 0 ? -num : num, den);
return cent_frac(num / g, den / g);
num /= g; den /= g;
if (den == 1) return cent_int(num);
return cent_frac(num, den);
}
static void to_frac(CentValue v, long *num, long *den) {
@@ -445,11 +447,16 @@ CentValue cent_mul(CentValue a, CentValue b) {
}
CentValue cent_div(CentValue a, CentValue b) {
if (a.type == CENT_NULL) a = cent_int(0);
if (b.type == CENT_NULL) b = cent_int(0);
if (a.type != CENT_INT || b.type != CENT_INT)
cent_type_error("'/' requires two integers");
if (b.ival == 0)
cent_runtime_error("division by zero");
return cent_int(a.ival / b.ival);
/* floored division (Python // semantics) */
long q = a.ival / b.ival;
if ((a.ival % b.ival != 0) && ((a.ival < 0) != (b.ival < 0))) q -= 1;
return cent_int(q);
}
CentValue cent_div_frac(CentValue a, CentValue b) {
@@ -460,11 +467,16 @@ CentValue cent_div_frac(CentValue a, CentValue b) {
}
CentValue cent_mod(CentValue a, CentValue b) {
if (a.type == CENT_NULL) a = cent_int(0);
if (b.type == CENT_NULL) b = cent_int(0);
if (a.type != CENT_INT || b.type != CENT_INT)
cent_type_error("'RELIQVVM' requires two integers");
if (b.ival == 0)
cent_runtime_error("modulo by zero");
return cent_int(a.ival % b.ival);
/* floored modulo (Python % semantics) */
long r = a.ival % b.ival;
if (r != 0 && ((a.ival < 0) != (b.ival < 0))) r += b.ival;
return cent_int(r);
}
CentValue cent_mod_frac(CentValue a, CentValue b) {