summaryrefslogtreecommitdiffhomepage
path: root/mrbgems
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2021-08-03 13:13:42 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2021-08-03 13:13:42 +0900
commitdf6ef39d1c588a945588eb80ced16a8e27d777cf (patch)
tree0540a55f6e098866b6d4d9a541f626d25ffac6c8 /mrbgems
parentb9d2b47f163e4934d180455126f1015525916fff (diff)
downloadmruby-df6ef39d1c588a945588eb80ced16a8e27d777cf.tar.gz
mruby-df6ef39d1c588a945588eb80ced16a8e27d777cf.zip
numeric_ext.c: define `modulo` and `remainder` methods for `Integer`.
Diffstat (limited to 'mrbgems')
-rw-r--r--mrbgems/mruby-numeric-ext/src/numeric_ext.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/mrbgems/mruby-numeric-ext/src/numeric_ext.c b/mrbgems/mruby-numeric-ext/src/numeric_ext.c
index 8389f913d..d5d031a9a 100644
--- a/mrbgems/mruby-numeric-ext/src/numeric_ext.c
+++ b/mrbgems/mruby-numeric-ext/src/numeric_ext.c
@@ -50,6 +50,24 @@ int_nobits(mrb_state *mrb, mrb_value self)
return mrb_bool_value((n & m) == 0);
}
+/*
+ * call-seq:
+ * num.remainder(numeric) -> real
+ *
+ * <code>x.remainder(y)</code> means <code>x-y*(x/y).truncate</code>.
+ *
+ * See Numeric#divmod.
+ */
+static mrb_value
+int_remainder(mrb_state *mrb, mrb_value self)
+{
+ mrb_int n, m;
+
+ mrb_get_args(mrb, "i", &m);
+ n = mrb_integer(self);
+ return mrb_int_value(mrb, n % m);
+}
+
void
mrb_mruby_numeric_ext_gem_init(mrb_state* mrb)
{
@@ -59,6 +77,9 @@ mrb_mruby_numeric_ext_gem_init(mrb_state* mrb)
mrb_define_method(mrb, i, "anybits?", int_anybits, MRB_ARGS_REQ(1));
mrb_define_method(mrb, i, "nobits?", int_nobits, MRB_ARGS_REQ(1));
+ mrb_define_alias(mrb, i, "modulo", "%");
+ mrb_define_method(mrb, i, "remainder", int_remainder, MRB_ARGS_REQ(1));
+
#ifndef MRB_NO_FLOAT
mrb_define_const_id(mrb, mrb->float_class, MRB_SYM(RADIX), mrb_fixnum_value(MRB_FLT_RADIX));
mrb_define_const_id(mrb, mrb->float_class, MRB_SYM(MANT_DIG), mrb_fixnum_value(MRB_FLT_MANT_DIG));