Skip to content

Commit 80094ed

Browse files
gh-126: Fix LCM.
1 parent f8e26fe commit 80094ed

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

src/builtins.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4367,29 +4367,30 @@ static Value builtin_lcm(Interpreter* interp, Value* args, int argc, Expr** arg_
43674367
if (args[0].type != args[1].type) {
43684368
RUNTIME_ERROR(interp, "LCM cannot mix INT and FLT", line, col);
43694369
}
4370-
4370+
int out_base = result_base_from_values(args[0], args[1]);
4371+
43714372
if (args[0].type == VAL_INT) {
43724373
int64_t a = args[0].as.i;
43734374
int64_t b = args[1].as.i;
4374-
if (a == 0 || b == 0) return value_int(0);
4375+
if (a == 0 || b == 0) return value_int_base(0, out_base);
43754376
int64_t g = gcd_int(a, b);
43764377
if (a < 0) a = -a;
43774378
if (b < 0) b = -b;
4378-
return value_int((a / g) * b);
4379+
return value_int_base((a / g) * b, out_base);
43794380
}
4380-
4381+
43814382
double a = args[0].as.f;
43824383
double b = args[1].as.f;
43834384
if (floor(a) != a || floor(b) != b) {
43844385
RUNTIME_ERROR(interp, "LCM expects integer-valued floats", line, col);
43854386
}
43864387
int64_t ai = (int64_t)a;
43874388
int64_t bi = (int64_t)b;
4388-
if (ai == 0 || bi == 0) return value_flt(0.0);
4389+
if (ai == 0 || bi == 0) return value_flt_base(0.0, out_base);
43894390
int64_t g = gcd_int(ai, bi);
43904391
if (ai < 0) ai = -ai;
43914392
if (bi < 0) bi = -bi;
4392-
return value_flt((double)((ai / g) * bi));
4393+
return value_flt_base((double)((ai / g) * bi), out_base);
43934394
}
43944395

43954396
// ============ Comparison operators ============

0 commit comments

Comments
 (0)