diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-08-03 16:30:47 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-08-03 16:30:47 +0900 |
| commit | e75572af6a6a286546879eef89c053a7a7fe8ad9 (patch) | |
| tree | 7c90288a59787c75a9b824caef40ca98f178d80b /mrbgems/mruby-numeric-ext | |
| parent | f4afcbef67f4a6bdef373ca15a6a7d45c74fc88c (diff) | |
| download | mruby-e75572af6a6a286546879eef89c053a7a7fe8ad9.tar.gz mruby-e75572af6a6a286546879eef89c053a7a7fe8ad9.zip | |
numeric_ext.c: add `Float#modulo` and `#remainder` methods.
Diffstat (limited to 'mrbgems/mruby-numeric-ext')
| -rw-r--r-- | mrbgems/mruby-numeric-ext/src/numeric_ext.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/mrbgems/mruby-numeric-ext/src/numeric_ext.c b/mrbgems/mruby-numeric-ext/src/numeric_ext.c index 44db4c05d..fb8ea53b1 100644 --- a/mrbgems/mruby-numeric-ext/src/numeric_ext.c +++ b/mrbgems/mruby-numeric-ext/src/numeric_ext.c @@ -1,6 +1,7 @@ #include <mruby.h> #include <mruby/numeric.h> #include <mruby/presym.h> +#include <math.h> /* * call-seq: @@ -87,6 +88,19 @@ int_remainder(mrb_state *mrb, mrb_value x) #endif } +#ifndef MRB_NO_FLOAT +static mrb_value +flo_remainder(mrb_state *mrb, mrb_value self) +{ + mrb_float a, b; + + a = mrb_float(self); + mrb_get_args(mrb, "f", &b); + if (b == 0) zerodiv(mrb); + return mrb_float_value(mrb, a-b*trunc(a/b)); +} +#endif + void mrb_mruby_numeric_ext_gem_init(mrb_state* mrb) { @@ -100,6 +114,11 @@ mrb_mruby_numeric_ext_gem_init(mrb_state* mrb) mrb_define_method(mrb, i, "remainder", int_remainder, MRB_ARGS_REQ(1)); #ifndef MRB_NO_FLOAT + struct RClass *f = mrb_class_get(mrb, "Float"); + + mrb_define_alias(mrb, f, "modulo", "%"); + mrb_define_method(mrb, f, "remainder", flo_remainder, MRB_ARGS_REQ(1)); + 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)); mrb_define_const_id(mrb, mrb->float_class, MRB_SYM(EPSILON), mrb_float_value(mrb, MRB_FLT_EPSILON)); |
