Commit f4f3e95f

f4f3e95f2977Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

feat: add Float#divmod

Returns tuple:int,float with integer quotient (floor division) and floating-point remainder.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
f4f3e95f2977
Parent
1550670ddc93

spinel_codegen.rbmodifiedView file

@@ -1840,6 +1840,12 @@
18401840 return "int"
18411841 end
18421842 if mname == "divmod"
1843+ if recv >= 0
1844+ rt = infer_type(recv)
1845+ if rt == "float"
1846+ return "tuple:int,float"
1847+ end
1848+ end
18431849 return "tuple:int,int"
18441850 end
18451851 if mname == "minmax"
@@ -12616,6 +12622,18 @@
1261612622 if mname == "fdiv"
1261712623 return "((" + rc + ") / (mrb_float)" + compile_arg0(nid) + ")"
1261812624 end
12625+ if mname == "divmod"
12626+ tt = "tuple:int,float"
12627+ register_tuple_type(tt)
12628+ @needs_gc = 1
12629+ tname = tuple_c_name(tt)
12630+ arg = compile_arg0(nid)
12631+ tmp = new_temp
12632+ emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);")
12633+ emit(" " + tmp + "->_0 = (mrb_int)floor(" + rc + " / " + arg + ");")
12634+ emit(" " + tmp + "->_1 = " + rc + " - " + tmp + "->_0 * " + arg + ";")
12635+ return tmp
12636+ end
1261912637 ""
1262012638 end
1262112639