mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* numeric.c: Simplify [ruby-core:61106] [Bug #9570]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45205 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
debb5e4cb7
commit
e184e31c09
1 changed files with 9 additions and 13 deletions
22
numeric.c
22
numeric.c
|
@ -1805,26 +1805,22 @@ VALUE
|
||||||
ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
|
ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
|
||||||
{
|
{
|
||||||
if (FIXNUM_P(from) && FIXNUM_P(to) && FIXNUM_P(step)) {
|
if (FIXNUM_P(from) && FIXNUM_P(to) && FIXNUM_P(step)) {
|
||||||
long delta, diff, result;
|
long delta, diff;
|
||||||
|
|
||||||
diff = FIX2LONG(step);
|
diff = FIX2LONG(step);
|
||||||
if (!diff) rb_num_zerodiv();
|
if (!diff) rb_num_zerodiv();
|
||||||
delta = FIX2LONG(to) - FIX2LONG(from);
|
delta = FIX2LONG(to) - FIX2LONG(from);
|
||||||
|
if (diff < 0) {
|
||||||
|
diff = -diff;
|
||||||
|
delta = -delta;
|
||||||
|
}
|
||||||
if (excl) {
|
if (excl) {
|
||||||
delta += (diff > 0 ? -1 : +1);
|
delta--;
|
||||||
}
|
}
|
||||||
if (delta) {
|
if (delta < 0) {
|
||||||
if (diff < 0) {
|
return INT2FIX(0);
|
||||||
if (delta > 0) return INT2FIX(0);
|
|
||||||
diff = -diff;
|
|
||||||
delta = -delta;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (delta < 0) return INT2FIX(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
result = delta / diff;
|
return LONG2FIX(delta / diff + 1);
|
||||||
return LONG2FIX(result >= 0 ? result + 1 : 0);
|
|
||||||
}
|
}
|
||||||
else if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) {
|
else if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) {
|
||||||
double n = ruby_float_step_size(NUM2DBL(from), NUM2DBL(to), NUM2DBL(step), excl);
|
double n = ruby_float_step_size(NUM2DBL(from), NUM2DBL(to), NUM2DBL(step), excl);
|
||||||
|
|
Loading…
Add table
Reference in a new issue