diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-10-22 16:18:11 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-10-22 16:18:11 +0900 |
| commit | 2607970ed9ab59165d9532c05440fb57846d636e (patch) | |
| tree | 7d95b33c858762f4473b78438af9aba9eb92f32e | |
| parent | 620f8b2721e2bf05b610116cc3857a87b6c140e6 (diff) | |
| download | mruby-2607970ed9ab59165d9532c05440fb57846d636e.tar.gz mruby-2607970ed9ab59165d9532c05440fb57846d636e.zip | |
move some methods to make floats and integers compatible [mruby special]
| -rw-r--r-- | mrblib/numeric.rb | 104 | ||||
| -rw-r--r-- | src/numeric.c | 17 |
2 files changed, 79 insertions, 42 deletions
diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb index f3f1a2a7e..9c799df5a 100644 --- a/mrblib/numeric.rb +++ b/mrblib/numeric.rb @@ -3,6 +3,7 @@ # # ISO 15.2.7 class Numeric + include Comparable ## # Returns the receiver simply. # @@ -33,19 +34,11 @@ class Numeric end ## -# Integer +# Integral # -# ISO 15.2.8 -class Integer - - ## - # Returns the receiver simply. - # - # ISO 15.2.8.3.14 - def ceil - self - end - +# mruby special - module to share methods between Floats and Integers +# to make them compatible +module Integral ## # Calls the given block once for each Integer # from +self+ downto +num+. @@ -61,14 +54,6 @@ class Integer end ## - # Returns the receiver simply. - # - # ISO 15.2.8.3.17 - def floor - self - end - - ## # Returns self + 1 # # ISO 15.2.8.3.19 @@ -92,22 +77,6 @@ class Integer end ## - # Returns the receiver simply. - # - # ISO 15.2.8.3.24 - def round - self - end - - ## - # Returns the receiver simply. - # - # ISO 15.2.8.3.26 - def truncate - self - end - - ## # Calls the given block once for each Integer # from +self+ upto +num+. # @@ -136,10 +105,63 @@ class Integer end ## -# Numeric is comparable +# Integer # -# ISO 15.2.7.3 -module Comparable; end -class Numeric - include Comparable +# ISO 15.2.8 +class Integer + include Integral + ## + # Returns the receiver simply. + # + # ISO 15.2.8.3.14 + def ceil + self + end + + ## + # Returns the receiver simply. + # + # ISO 15.2.8.3.17 + def floor + self + end + + ## + # Returns the receiver simply. + # + # ISO 15.2.8.3.24 + alias round floor + + ## + # Returns the receiver simply. + # + # ISO 15.2.8.3.26 + alias truncate floor +end + +## +# Float +# +# ISO 15.2.9 +class Float + include Integral + # mruby special - since mruby integers may be upgraded to floats, + # floats should be compatible to integers. + def >> other + n = self.to_i + other.to_i.times { + n /= 2 + } + n + end + def << other + n = self.to_i + other.to_i.times { + n *= 2 + } + n.to_i + end + + def divmod(other) + end end diff --git a/src/numeric.c b/src/numeric.c index 65fe5051b..414522fb6 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -784,6 +784,21 @@ fix_divmod(mrb_state *mrb, mrb_value x) } } +static mrb_value +flo_divmod(mrb_state *mrb, mrb_value x) +{ + mrb_value y; + mrb_float div, mod; + mrb_value a, b; + + mrb_get_args(mrb, "o", &y); + + flodivmod(mrb, mrb_float(x), mrb_to_flo(mrb, y), &div, &mod); + a = mrb_float_value(mrb, (mrb_int)div); + b = mrb_float_value(mrb, mod); + return mrb_assoc_new(mrb, a, b); +} + /* 15.2.8.3.7 */ /* * call-seq: @@ -1270,7 +1285,6 @@ mrb_init_numeric(mrb_state *mrb) /* Numeric Class */ numeric = mrb_define_class(mrb, "Numeric", mrb->object_class); - mrb_include_module(mrb, numeric, mrb_class_get(mrb, "Comparable")); mrb_define_method(mrb, numeric, "**", num_pow, MRB_ARGS_REQ(1)); mrb_define_method(mrb, numeric, "/", num_div, MRB_ARGS_REQ(1)); /* 15.2.8.3.4 */ @@ -1319,6 +1333,7 @@ mrb_init_numeric(mrb_state *mrb) mrb_define_method(mrb, fl, "to_i", flo_truncate, MRB_ARGS_NONE()); /* 15.2.9.3.14 */ mrb_define_method(mrb, fl, "to_int", flo_truncate, MRB_ARGS_NONE()); mrb_define_method(mrb, fl, "truncate", flo_truncate, MRB_ARGS_NONE()); /* 15.2.9.3.15 */ + mrb_define_method(mrb, fixnum, "divmod", flo_divmod, MRB_ARGS_REQ(1)); mrb_define_method(mrb, fl, "to_s", flo_to_s, MRB_ARGS_NONE()); /* 15.2.9.3.16(x) */ mrb_define_method(mrb, fl, "inspect", flo_to_s, MRB_ARGS_NONE()); |
