Bug #2129
Rational can't be coerced into BigDecimal (TypeError)
| Status: | Closed | Start date: | 09/21/2009 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | - | % Done: | 100% |
|
| Category: | lib | |||
| Target version: | 1.9.2 | |||
| ruby -v: | ruby 1.9.2dev (2009-09-20 trunk 25012) [x86_64-linux] |
Description
Rational + BigDecimal makes a Rational as follows:
$ ruby -rbigdecimal -e "puts Rational(1) + BigDecimal.new('1')"
2/1
But BigDecimal + Rationl raises a TypeError as follows:
$ ruby -rbigdecimal -e "puts BigDecimal.new('1') + Rational(1)"
-e:1:in `+': Rational can't be coerced into BigDecimal (TypeError)
from -e:1:in `<main>'
Associated revisions
* ext/bigdecimal/bigdecimal.c (GetVpValue): support conversion from
Rational. [ruby-core:25697]
History
Updated by nobu (Nobuyoshi Nakada) over 2 years ago
- Status changed from Open to Closed
- % Done changed from 0 to 100
Applied in changeset r25025.
Updated by kubo (Takehiro Kubo) over 2 years ago
Thanks for the fix not to raise a TypeError. Well, ruby svn trunk 25031 calculate Rational and BigDecimal as follows: Rational + BigDecimal -> Rational BigDecimal + Rational -> BigDecimal I think that it should be Rational + BigDecimal -> Rational BigDecimal + Rational -> Rational or Rational + BigDecimal -> BigDecimal BigDecimal + Rational -> BigDecimal I prefer the former.
Updated by tadf (tadayoshi funaba) over 2 years ago
BigDecimal can represent Infinity and NaN.
main@192-20090925> Rational(1,3) * BigDecimal('NaN')
TypeError: BigDecimal can't be coerced into Rational
Updated by marcandre (Marc-Andre Lafortune) over 2 years ago
- Category set to lib
- Status changed from Closed to Open
- Target version set to 1.9.2
Updated by mame (Yusuke Endoh) about 2 years ago
Hi,
- #1454 was registerd
- r23389 was committed to fix #1454, but it was incomplete
- #2129 (this ticket) was registered
- r25025 was committed to fix #2129 correctly
Now, r23389 not only is unnecessary but also causes the
inconsistency of return type.
Thus, I'll fix this issue by reverting r23389.
By this revert, both will return BigDecimal.
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 9930e63..2dd3213 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -602,14 +602,9 @@ BigDecimal_coerce(VALUE self, VALUE other)
ENTER(2);
VALUE obj;
Real *b;
- switch (TYPE(other)) {
- case T_FLOAT:
+ if (TYPE(other) == T_FLOAT) {
obj = rb_assoc_new(other, BigDecimal_to_f(self));
- break;
- case T_RATIONAL:
- obj = rb_assoc_new(other, BigDecimal_to_r(self));
- break;
- default:
+ } else {
GUARD_OBJ(b,GetVpValue(other,1));
obj = rb_assoc_new(b->obj, self);
}
--
Yusuke Endoh <mame@tsg.ne.jp>
Updated by mame (Yusuke Endoh) about 2 years ago
- Status changed from Open to Closed
This issue was solved with changeset r27358. Takehiro, thank you for reporting this issue. Your contribution to Ruby is greatly appreciated. May Ruby be with you.