From: "duerst (Martin Dürst)" Date: 2013-12-04T19:58:20+09:00 Subject: [ruby-core:58849] [ruby-trunk - Bug #9192] Inconsistent comparison between Float and BigDecimal Issue #9192 has been updated by duerst (Martin D��rst). marcandre (Marc-Andre Lafortune) wrote: > I never thought about comparison of float with other numeric types, but should we convert everything to float and compare this way? This way we insure that: > > any_big_decimal.to_f == any_big_decimal > > Here, the float 706.06 stands for a small range of real numbers that includes BigDecimal('706.06'). Thus shouldn't we have 706.06 == BigDecimal('706.06')? This definitely would be a good solution for this specific example. But we have to (try to) make sure it's going to work consistently for other numeric types, too. For example, I tested with Rational(70606, 100). That compares equal with 706.06 (both ways). So it would indeed make sense to have BigDecimal('706.06') also compare equal with 706.06. It currently does if written 706.06 op BigDecimal('706.06') (op is any of <=>, <=, ==, >=, !=, <, >), but not the other way round. But simply converting BigDecimal to Float may not make sense in general. Imagine a BigDecimal with much higher precision, like BigDecimal('706.06059999999999'), and some corresponding floats. We may want to actually say that the BigDecimal is bigger or smaller than the float created from the same string, because the float looses precision. Currently, I get 706.06059999999999 <=> BigDecimal('706.06059999999999') #=> 0 BigDecimal('706.06059999999999') <=> 706.0605999999999 #=> -1 which is of course inconsistent. We should in any case make sure that (a <=> b) + (b <=> a) is always 0 wherever the comparison makes sense. ---------------------------------------- Bug #9192: Inconsistent comparison between Float and BigDecimal https://bugs.ruby-lang.org/issues/9192#change-43407 Author: vatsu (Gustavo Sales) Status: Assigned Priority: Normal Assignee: mrkn (Kenta Murata) Category: ext Target version: ruby -v: 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin11.4.2] Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN I was checking a possible inconsistency on ActiveModel and I found situations where comparing BigDecimal to Float differs to comparing Float to BigDecimal. I have create a simple ruby script to exemplify the problem. The script is attach to this issue and output is as follows: Instance number 706.05 ########### Instance number 706.06 Inconsistence: 0.70606E3 > 706.06 706.06 < 0.70606E3 ========= Inconsistence: 0.70606E3 <= 706.06 706.06 >= 0.70606E3 ========= ########### Instance number 706.07 Inconsistence: 0.70607E3 < 706.07 706.07 > 0.70607E3 ========= Inconsistence: 0.70607E3 >= 706.07 706.07 <= 0.70607E3 ========= ########### Instance number 706.08 ########### -- http://bugs.ruby-lang.org/