Unverified Commit 4e0b1622 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #5622 from dearblue/to_f

Call functions directly from `mrb_ensure_float_type()`
parents ab4baba2 ac22a63a
......@@ -366,3 +366,18 @@ mrb_final_mrbgems(mrb_state *mrb)
{
}
#endif
#ifdef MRB_USE_COMPLEX
mrb_value mrb_complex_to_f(mrb_state *mrb, mrb_value comp)
{
mrb_raise(mrb, E_NOTIMP_ERROR, "unavailable for core only");
}
#endif
#ifdef MRB_USE_RATIONAL
mrb_value
mrb_rational_to_f(mrb_state *mrb, mrb_value rat)
{
mrb_raise(mrb, E_NOTIMP_ERROR, "unavailable for core only");
}
#endif
......@@ -105,8 +105,8 @@ complex_s_rect(mrb_state *mrb, mrb_value self)
return complex_new(mrb, real, imaginary);
}
static mrb_value
complex_to_f(mrb_state *mrb, mrb_value self)
mrb_value
mrb_complex_to_f(mrb_state *mrb, mrb_value self)
{
struct mrb_complex *p = complex_ptr(mrb, self);
......@@ -415,7 +415,7 @@ void mrb_mruby_complex_gem_init(mrb_state *mrb)
mrb_define_method(mrb, mrb->kernel_module, "Complex", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
mrb_define_method(mrb, comp, "real", complex_real, MRB_ARGS_NONE());
mrb_define_method(mrb, comp, "imaginary", complex_imaginary, MRB_ARGS_NONE());
mrb_define_method(mrb, comp, "to_f", complex_to_f, MRB_ARGS_NONE());
mrb_define_method(mrb, comp, "to_f", mrb_complex_to_f, MRB_ARGS_NONE());
mrb_define_method(mrb, comp, "to_i", complex_to_i, MRB_ARGS_NONE());
mrb_define_method(mrb, comp, "to_c", complex_to_c, MRB_ARGS_NONE());
mrb_define_method(mrb, comp, "+", complex_add, MRB_ARGS_REQ(1));
......
......@@ -291,8 +291,8 @@ rat_float(struct mrb_rational *p)
return f;
}
static mrb_value
rational_to_f(mrb_state *mrb, mrb_value self)
mrb_value
mrb_rational_to_f(mrb_state *mrb, mrb_value self)
{
struct mrb_rational *p = rational_ptr(mrb, self);
return mrb_float_value(mrb, rat_float(p));
......@@ -394,7 +394,7 @@ rational_eq(mrb_state *mrb, mrb_value x)
case MRB_TT_COMPLEX:
{
mrb_bool mrb_complex_eq(mrb_state *mrb, mrb_value, mrb_value);
result = mrb_complex_eq(mrb, y, rational_to_f(mrb, x));
result = mrb_complex_eq(mrb, y, mrb_rational_to_f(mrb, x));
break;
}
#endif
......@@ -712,7 +712,7 @@ void mrb_mruby_rational_gem_init(mrb_state *mrb)
mrb_define_method(mrb, rat, "numerator", rational_numerator, MRB_ARGS_NONE());
mrb_define_method(mrb, rat, "denominator", rational_denominator, MRB_ARGS_NONE());
#ifndef MRB_NO_FLOAT
mrb_define_method(mrb, rat, "to_f", rational_to_f, MRB_ARGS_NONE());
mrb_define_method(mrb, rat, "to_f", mrb_rational_to_f, MRB_ARGS_NONE());
#endif
mrb_define_method(mrb, rat, "to_i", rational_to_i, MRB_ARGS_NONE());
mrb_define_method(mrb, rat, "to_r", rational_to_r, MRB_ARGS_NONE());
......
......@@ -499,6 +499,9 @@ mrb_ensure_int_type(mrb_state *mrb, mrb_value val)
}
#ifndef MRB_NO_FLOAT
mrb_value mrb_complex_to_f(mrb_state *mrb, mrb_value comp); // provided by mruby-complex with MRB_USE_COMPLEX
mrb_value mrb_rational_to_f(mrb_state *mrb, mrb_value rat); // provided by mruby-rational with MRB_USE_RATIONAL
MRB_API mrb_value
mrb_ensure_float_type(mrb_state *mrb, mrb_value val)
{
......@@ -512,9 +515,15 @@ mrb_ensure_float_type(mrb_state *mrb, mrb_value val)
case MRB_TT_FLOAT:
return val;
#ifdef MRB_USE_RATIONAL
case MRB_TT_RATIONAL:
return mrb_rational_to_f(mrb, val);
#endif
#ifdef MRB_USE_COMPLEX
case MRB_TT_COMPLEX:
return mrb_type_convert(mrb, val, MRB_TT_FLOAT, MRB_SYM(to_f));
return mrb_complex_to_f(mrb, val);
#endif
default:
mrb_raisef(mrb, E_TYPE_ERROR, "%Y cannot be converted to Float", val);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment