Project

General

Profile

Bug #13331

Updated by msatkiewicz (Maciej Satkiewicz) about 7 years ago

One would reasonably expect the equality 1.to_d * f == f.t_d to hold for every Float value f, i.e. multiplying by DigDecimal's unit can be viewed as an alternative way of casting Float into Decimal; or simply one could expect that BigDecimal#* should always invoke Float#to_d BigDecimal#to_d on it's argument. Yet for some Floats (e.g. 64.4) the results differ, as showed in the provided script. This can cause a bug in some application contexts. 

 Originally I asked about that on Stackoverflow: 
 http://stackoverflow.com/questions/40472933/inconsistent-conversion-of-float-into-decimal-in-ruby/40473007 

 See the comprehensive answer by Stefan who points out that it is due to different precisions being used in Float#to_d BigDecimal#to_d and BigDecimal#*.

Back