Colin Law wrote in post #970496: > On 24 December 2010 14:25, Marnen Laibow-Koser <li...@ruby-forum.com> > wrote: >>>>> >>> => #<BigDecimal:ad2b270,'0.99999999E0',8(20)> >>> ruby-1.9.2-p0 > x*BigDecimal('3') == 1 >>> => false >>> ruby-1.9.2-p0 > x*BigDecimal('3') - 1 >>> => #<BigDecimal:aa4fdf8,'-0.1E-7',4(20)> >>> >> >> >> Interesting. However, the BigDecimal constructor allows specification >> of the minimum number of significant figures, so in this case we could >> have done BigDecimal.new('1', 16). > > ruby-1.9.2-p0 > x = BigDecimal.new('1', 16)/BigDecimal.new('3',16) > => #<BigDecimal:a146bbc,'0.33333333E0',8(16)> > ruby-1.9.2-p0 > x*BigDecimal('3',16) - 1 > => #<BigDecimal:a114acc,'-0.1E-7',4(20)> > > It doesn't make any difference how many digits you have, 1/3 as a > decimal number multiplied by 3 will never be equal to 1.
I'm aware of that, of course. In this particular case, we could use a Rational to fix that. But that won't necessarily work with arbitrary floating-point numbers... ...unless we recast them as Rationals and do all math that way. Hmm. > > I am not sure why the above still gives only 8 digits accuracy > however, I would have thought it aught to be 16. Yeah, that's peculiar. I'll have to run some tests. (I can almost guarantee that Rubinius would get that one right, since I wrote most of the BigDecimal library at one time. Don't know if they're still using it.) > > Colin Best, -- Marnen Laibow-Koser http://www.marnen.org mar...@marnen.org Sent from my iPhone -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.