summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-05-08 03:56:12 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-05-08 03:56:12 +0000
commit698a24674eb0707fdc8d934084932e845db955cd (patch)
tree14332a4426614ee3b43f69a5c51a1117ec8d1ab2 /numeric.c
parent908300d44f0d072d4ef2e6813794aa9170b9f618 (diff)
downloadruby-698a24674eb0707fdc8d934084932e845db955cd.tar.gz
* compar.c (rb_cmperr): raise comparison failure.
* intern.h: prototype; rb_cmperr * numeric.c (flo_gt, flo_ge, flo_lt, flo_le, fix_gt, fix_ge, fix_lt, fix_le): should fail unless the argument is comparable. (ruby-bugs-ja:PR#456) git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3768 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c62
1 files changed, 28 insertions, 34 deletions
diff --git a/numeric.c b/numeric.c
index edc42ef6bd..186a2db901 100644
--- a/numeric.c
+++ b/numeric.c
@@ -144,6 +144,20 @@ rb_num_coerce_cmp(x, y)
}
static VALUE
+num_coerce_relop(x, y)
+ VALUE x, y;
+{
+ VALUE c, x0 = x, y0 = y;
+
+ if (!do_coerce(&x, &y, Qfalse) ||
+ NIL_P(c = rb_funcall(x, rb_frame_last_func(), 1, y))) {
+ rb_cmperr(x0, y0);
+ return Qnil; /* not reached */
+ }
+ return c;
+}
+
+static VALUE
num_copy_object(x, y)
VALUE x, y;
{
@@ -604,7 +618,7 @@ flo_gt(x, y)
break;
default:
- return rb_num_coerce_cmp(x, y);
+ return num_coerce_relop(x, y);
}
if (isnan(a) || isnan(b)) return Qfalse;
return (a > b)?Qtrue:Qfalse;
@@ -631,7 +645,7 @@ flo_ge(x, y)
break;
default:
- return rb_num_coerce_cmp(x, y);
+ return num_coerce_relop(x, y);
}
if (isnan(a) || isnan(b)) return Qfalse;
return (a >= b)?Qtrue:Qfalse;
@@ -658,7 +672,7 @@ flo_lt(x, y)
break;
default:
- return rb_num_coerce_cmp(x, y);
+ return num_coerce_relop(x, y);
}
if (isnan(a) || isnan(b)) return Qfalse;
return (a < b)?Qtrue:Qfalse;
@@ -685,7 +699,7 @@ flo_le(x, y)
break;
default:
- return rb_num_coerce_cmp(x, y);
+ return num_coerce_relop(x, y);
}
if (isnan(a) || isnan(b)) return Qfalse;
return (a <= b)?Qtrue:Qfalse;
@@ -1439,7 +1453,7 @@ fix_gt(x, y)
return Qfalse;
}
else {
- return rb_num_coerce_cmp(x, y);
+ return num_coerce_relop(x, y);
}
}
@@ -1454,7 +1468,7 @@ fix_ge(x, y)
return Qfalse;
}
else {
- return rb_num_coerce_cmp(x, y);
+ return num_coerce_relop(x, y);
}
}
@@ -1469,7 +1483,7 @@ fix_lt(x, y)
return Qfalse;
}
else {
- return rb_num_coerce_cmp(x, y);
+ return num_coerce_relop(x, y);
}
}
@@ -1484,7 +1498,7 @@ fix_le(x, y)
return Qfalse;
}
else {
- return rb_num_coerce_cmp(x, y);
+ return num_coerce_relop(x, y);
}
}
@@ -1654,28 +1668,6 @@ fix_size(fix)
}
static VALUE
-int_compare(i, to, id)
- VALUE i, to;
- ID id;
-{
- VALUE cmp = rb_funcall(i, id, 1, to);
- if (NIL_P(cmp)) {
- char *toclass;
-
- if (SPECIAL_CONST_P(to)) {
- to = rb_inspect(to);
- toclass = StringValuePtr(to);
- }
- else {
- toclass = rb_obj_classname(to);
- }
- rb_raise(rb_eArgError, "cannot compare %s with %s",
- rb_obj_classname(i), toclass);
- }
- return cmp;
-}
-
-static VALUE
int_upto(from, to)
VALUE from, to;
{
@@ -1688,12 +1680,13 @@ int_upto(from, to)
}
}
else {
- VALUE i = from;
+ VALUE i = from, c;
- while (!int_compare(i, to, '>')) {
+ while (!(c = rb_funcall(i, '>', 1, to))) {
rb_yield(i);
i = rb_funcall(i, '+', 1, INT2FIX(1));
}
+ if (NIL_P(c)) rb_cmperr(i, to);
}
return from;
}
@@ -1711,12 +1704,13 @@ int_downto(from, to)
}
}
else {
- VALUE i = from;
+ VALUE i = from, c;
- while (!int_compare(i, to, '<')) {
+ while (!(c = rb_funcall(i, '<', 1, to))) {
rb_yield(i);
i = rb_funcall(i, '-', 1, INT2FIX(1));
}
+ if (NIL_P(c)) rb_cmperr(i, to);
}
return from;
}