diff options
Diffstat (limited to 'numeric.c')
-rw-r--r-- | numeric.c | 39 |
1 files changed, 35 insertions, 4 deletions
@@ -6,7 +6,7 @@ $Date$ created at: Fri Aug 13 18:33:09 JST 1993 - Copyright (C) 1993-1998 Yukihiro Matsumoto + Copyright (C) 1993-1999 Yukihiro Matsumoto ************************************************/ @@ -778,6 +778,27 @@ int_chr(num) } static VALUE +rb_fix_induced_from(klass, x) + VALUE klass, x; +{ + return rb_funcall(x, rb_intern("to_i"), 0); +} + +static VALUE +rb_int_induced_from(klass, x) + VALUE klass, x; +{ + return rb_funcall(x, rb_intern("to_i"), 0); +} + +static VALUE +rb_flo_induced_from(klass, x) + VALUE klass, x; +{ + return rb_funcall(x, rb_intern("to_f"), 0); +} + +static VALUE fix_uminus(num) VALUE num; { @@ -1309,20 +1330,23 @@ fix_step(from, to, step) if (!FIXNUM_P(to) || !FIXNUM_P(step)) return int_step(from, to, step); + i = FIX2LONG(from); end = FIX2LONG(to); diff = FIX2LONG(step); if (diff == 0) { rb_raise(rb_eArgError, "step cannot be 0"); } - else if (diff > 0) { - for (i=FIX2LONG(from); i <= end; i+=diff) { + if (diff > 0) { + while (i <= end) { rb_yield(INT2FIX(i)); + i += diff; } } else { - for (i=FIX2LONG(from); i >= end; i+=diff) { + while (i >= end) { rb_yield(INT2FIX(i)); + i += diff; } } return from; @@ -1380,11 +1404,15 @@ Init_Numeric() rb_define_method(rb_cInteger, "downto", int_downto, 1); rb_define_method(rb_cInteger, "step", int_step, 2); rb_define_method(rb_cInteger, "times", int_dotimes, 0); + rb_include_module(rb_cInteger, rb_mPrecision); rb_define_method(rb_cInteger, "succ", int_succ, 0); rb_define_method(rb_cInteger, "next", int_succ, 0); rb_define_method(rb_cInteger, "chr", int_chr, 0); rb_cFixnum = rb_define_class("Fixnum", rb_cInteger); + rb_include_module(rb_cFixnum, rb_mPrecision); + rb_define_singleton_method(rb_cFixnum, "induced_from", rb_fix_induced_from, 1); + rb_define_singleton_method(rb_cInteger, "induced_from", rb_int_induced_from, 1); rb_undef_method(CLASS_OF(rb_cFixnum), "new"); @@ -1437,6 +1465,9 @@ Init_Numeric() rb_undef_method(CLASS_OF(rb_cFloat), "new"); + rb_define_singleton_method(rb_cFloat, "induced_from", rb_flo_induced_from, 1); + rb_include_module(rb_cFloat, rb_mPrecision); + rb_define_method(rb_cFloat, "to_s", flo_to_s, 0); rb_define_method(rb_cFloat, "coerce", flo_coerce, 1); rb_define_method(rb_cFloat, "-@", flo_uminus, 0); |