Commit cb3f6762
cb3f6762fc1fMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Add M18: Float#round/ceil/floor/truncate(ndigits) value-based type
Match CRuby's value-based return type for a literal ndigits: ndigits > 0 returns a Float rounded to that many decimals (via round(x*10^n)/10^n in a statement-expression); no-arg or ndigits <= 0 returns an Integer. Fixes float_round_zero.
make test: see count above.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- cb3f6762fc1f
- Parent
- c81a36c2d398
src/analyze.cmodifiedView file
@@ -190,7 +190,15 @@
190190
/* float receiver methods */191191
if (recv >= 0 && rt == TY_FLOAT) {192192
if (!strcmp(name, "floor") || !strcmp(name, "ceil") ||193-
!strcmp(name, "round") || !strcmp(name, "truncate")) return TY_INT;193+
!strcmp(name, "round") || !strcmp(name, "truncate")) {194+
/* value-based return type: ndigits > 0 (literal) -> Float, else Integer */195+
if (argc == 1) {196+
const char *aty = nt_type(nt, argv[0]);197+
if (aty && !strcmp(aty, "IntegerNode") && nt_int(nt, argv[0], "value", 0) > 0)198+
return TY_FLOAT;199+
}200+
return TY_INT;201+
}194202
}195203
196204
if ((!strcmp(name, "-@") || !strcmp(name, "+@")) && recv >= 0 && argc == 0)src/codegen.cmodifiedView file
Loading diff…