Question

I thought that the purpose of Ruby's BigDecimal class is that it is infinitely arbitrarily precise at the cost of speed. Wrong:

> BigDecimal(4).sqrt(4)
=> #<BigDecimal:906602c,'0.1999999999 9999999998 66602351E1',36(36)>
> BigDecimal(4).sqrt(11)
=> #<BigDecimal:9e36850,'0.2E1',9(45)>

First, sqrt should work without a parameter, at least by the documentation. Second, if that parameter is the precision, 4 should suffice for this operation (or at least I shouldn't know that 11 digit precision is OK).

The last thing I want is to reinvent the wheel, and create some 'arbitrarily precise squaring library' of my own.

Questions:

  • Is it possible to set parameters to a BigDecimal instance so the above does not happen? (meaning: It gives back proper result at low precision)
  • Is there any detailed info on the BigDecimal object (what those numbers are after the value)?

System: Debian, 32-bit, ruby 1.9.3p125

Was it helpful?

Solution

The big decimal docs for inspect describe what that output means.

Regarding the documentation for sqrt that seems to be a documentation bug - see http://bugs.ruby-lang.org/issues/5267

As for the output you're getting, I'm not sure - I get 0.2E1 in both cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top