mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
move some methods to make floats and integers compatible [mruby special]
This commit is contained in:
+63
-41
@@ -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+.
|
||||
@@ -60,14 +53,6 @@ class Integer
|
||||
self
|
||||
end
|
||||
|
||||
##
|
||||
# Returns the receiver simply.
|
||||
#
|
||||
# ISO 15.2.8.3.17
|
||||
def floor
|
||||
self
|
||||
end
|
||||
|
||||
##
|
||||
# Returns self + 1
|
||||
#
|
||||
@@ -91,22 +76,6 @@ class Integer
|
||||
self
|
||||
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
|
||||
|
||||
+16
-1
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user