summaryrefslogtreecommitdiff
path: root/rational.c
diff options
context:
space:
mode:
authortadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-07-12 14:57:42 +0000
committertadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-07-12 14:57:42 +0000
commit8d9896c3b98aba5eb86a4b9405bc661658cb0a80 (patch)
tree5a95f9005943b82aacfbf42bc4beeadf7ce2584a /rational.c
parentb1f618e9e511ee9fd16cb86153a48193652472db (diff)
* complex.c: added some shortcuts.
* rational.c: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24055 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'rational.c')
-rw-r--r--rational.c30
1 files changed, 28 insertions, 2 deletions
diff --git a/rational.c b/rational.c
index d5a87f6..a9e60eb 100644
--- a/rational.c
+++ b/rational.c
@@ -166,8 +166,18 @@ f_negative_p(VALUE x)
inline static VALUE
f_zero_p(VALUE x)
{
- if (FIXNUM_P(x))
+ switch (TYPE(x)) {
+ case T_FIXNUM:
return f_boolcast(FIX2LONG(x) == 0);
+ case T_BIGNUM:
+ return Qfalse;
+ case T_RATIONAL:
+ {
+ VALUE num = RRATIONAL(x)->num;
+
+ return f_boolcast(FIXNUM_P(num) && FIX2LONG(num) == 0);
+ }
+ }
return rb_funcall(x, id_eqeq_p, 1, ZERO);
}
@@ -176,8 +186,20 @@ f_zero_p(VALUE x)
inline static VALUE
f_one_p(VALUE x)
{
- if (FIXNUM_P(x))
+ switch (TYPE(x)) {
+ case T_FIXNUM:
return f_boolcast(FIX2LONG(x) == 1);
+ case T_BIGNUM:
+ return Qfalse;
+ case T_RATIONAL:
+ {
+ VALUE num = RRATIONAL(x)->num;
+ VALUE den = RRATIONAL(x)->den;
+
+ return f_boolcast(FIXNUM_P(num) && FIX2LONG(num) == 1 &&
+ FIXNUM_P(den) && FIX2LONG(den) == 1);
+ }
+ }
return rb_funcall(x, id_eqeq_p, 1, ONE);
}
@@ -846,6 +868,10 @@ nurat_div(VALUE self, VALUE other)
{
get_dat2(self, other);
+ if (f_one_p(self))
+ return f_rational_new_no_reduce2(CLASS_OF(self),
+ bdat->den, bdat->num);
+
return f_muldiv(self,
adat->num, adat->den,
bdat->num, bdat->den, '/');