summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--mrblib/numeric.rb104
-rw-r--r--src/numeric.c17
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());