@@ -3535,6 +3535,88 @@ arith_seq_first(int argc, VALUE *argv, VALUE self)
35353535 return rb_call_super (argc , argv );
35363536}
35373537
3538+ static inline VALUE
3539+ num_plus (VALUE a , VALUE b )
3540+ {
3541+ if (RB_INTEGER_TYPE_P (a )) {
3542+ return rb_int_plus (a , b );
3543+ }
3544+ else if (RB_FLOAT_TYPE_P (a )) {
3545+ return rb_float_plus (a , b );
3546+ }
3547+ else if (RB_TYPE_P (a , T_RATIONAL )) {
3548+ return rb_rational_plus (a , b );
3549+ }
3550+ else {
3551+ return rb_funcallv (a , '+' , 1 , & b );
3552+ }
3553+ }
3554+
3555+ static inline VALUE
3556+ num_minus (VALUE a , VALUE b )
3557+ {
3558+ if (RB_INTEGER_TYPE_P (a )) {
3559+ return rb_int_minus (a , b );
3560+ }
3561+ else if (RB_FLOAT_TYPE_P (a )) {
3562+ return rb_float_minus (a , b );
3563+ }
3564+ else if (RB_TYPE_P (a , T_RATIONAL )) {
3565+ return rb_rational_minus (a , b );
3566+ }
3567+ else {
3568+ return rb_funcallv (a , '-' , 1 , & b );
3569+ }
3570+ }
3571+
3572+ static inline VALUE
3573+ num_mul (VALUE a , VALUE b )
3574+ {
3575+ if (RB_INTEGER_TYPE_P (a )) {
3576+ return rb_int_mul (a , b );
3577+ }
3578+ else if (RB_FLOAT_TYPE_P (a )) {
3579+ return rb_float_mul (a , b );
3580+ }
3581+ else if (RB_TYPE_P (a , T_RATIONAL )) {
3582+ return rb_rational_mul (a , b );
3583+ }
3584+ else {
3585+ return rb_funcallv (a , '*' , 1 , & b );
3586+ }
3587+ }
3588+
3589+ static inline VALUE
3590+ num_idiv (VALUE a , VALUE b )
3591+ {
3592+ VALUE q ;
3593+ if (RB_INTEGER_TYPE_P (a )) {
3594+ q = rb_int_idiv (a , b );
3595+ }
3596+ else if (RB_FLOAT_TYPE_P (a )) {
3597+ q = rb_float_div (a , b );
3598+ }
3599+ else if (RB_TYPE_P (a , T_RATIONAL )) {
3600+ q = rb_rational_div (a , b );
3601+ }
3602+ else {
3603+ q = rb_funcallv (a , idDiv , 1 , & b );
3604+ }
3605+
3606+ if (RB_INTEGER_TYPE_P (q )) {
3607+ return q ;
3608+ }
3609+ else if (RB_FLOAT_TYPE_P (q )) {
3610+ return rb_float_floor (q , 0 );
3611+ }
3612+ else if (RB_TYPE_P (q , T_RATIONAL )) {
3613+ return rb_rational_floor (q , 0 );
3614+ }
3615+ else {
3616+ return rb_funcall (q , rb_intern ("floor" ), 0 );
3617+ }
3618+ }
3619+
35383620/*
35393621 * call-seq:
35403622 * aseq.last -> num or nil
@@ -3559,17 +3641,17 @@ arith_seq_last(int argc, VALUE *argv, VALUE self)
35593641 b = arith_seq_begin (self );
35603642 s = arith_seq_step (self );
35613643
3562- len_1 = rb_int_idiv ( rb_int_minus (e , b ), s );
3644+ len_1 = num_idiv ( num_minus (e , b ), s );
35633645 if (rb_num_negative_int_p (len_1 )) {
35643646 if (argc == 0 ) {
35653647 return Qnil ;
35663648 }
35673649 return rb_ary_new_capa (0 );
35683650 }
35693651
3570- last = rb_int_plus (b , rb_int_mul (s , len_1 ));
3652+ last = num_plus (b , num_mul (s , len_1 ));
35713653 if ((last_is_adjusted = arith_seq_exclude_end_p (self ) && rb_equal (last , e ))) {
3572- last = rb_int_minus (last , s );
3654+ last = num_minus (last , s );
35733655 }
35743656
35753657 if (argc == 0 ) {
@@ -3778,22 +3860,22 @@ arith_seq_each(VALUE self)
37783860 return self ;
37793861 }
37803862
3781- len_1 = rb_int_idiv ( rb_int_minus (e , c ), s );
3782- last = rb_int_plus (c , rb_int_mul (s , len_1 ));
3863+ len_1 = num_idiv ( num_minus (e , c ), s );
3864+ last = num_plus (c , num_mul (s , len_1 ));
37833865 if (x && rb_equal (last , e )) {
3784- last = rb_int_minus (last , s );
3866+ last = num_minus (last , s );
37853867 }
37863868
37873869 if (rb_num_negative_int_p (s )) {
37883870 while (NUM_GE (c , last )) {
37893871 rb_yield (c );
3790- c = rb_int_plus (c , s );
3872+ c = num_plus (c , s );
37913873 }
37923874 }
37933875 else {
37943876 while (NUM_GE (last , c )) {
37953877 rb_yield (c );
3796- c = rb_int_plus (c , s );
3878+ c = num_plus (c , s );
37973879 }
37983880 }
37993881
0 commit comments