Colin Law wrote in post #970447:
> On 24 December 2010 03:59, Marnen Laibow-Koser <[email protected]>
> wrote:
>>>
>>> x = BigDecimal.new('1')
>>> => #<BigDecimal:102c078,'0.1E1',4(8)>
>>> y = BigDecimal.new('3')
>>> => #<BigDecimal:10388f0,'0.3E1',4(8)>
>>> z = x / y
>>> => #<BigDecimal:103680c,'0.33333333E0',8(16)>
>>>
>>> In the case of float we have 16 decimal digits of precision, but in the
>>> case of BigDecimal we have only eight digits of precision.
>>
>> Are you sure? Or are only eight digits displayed?
>
> $ rails console
> Loading development environment (Rails 3.0.3)
> ruby-1.9.2-p0 > x=BigDecimal('1')/BigDecimal('3')
> => #<BigDecimal:ad823a4,'0.33333333E0',8(16)>
> ruby-1.9.2-p0 > x*BigDecimal('3')
> => #<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).
> Colin
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.