October 2006
Intermediate to advanced
888 pages
16h 55m
English
The bigdecimal standard library enables us to work with large numbers of significant digits in fractional numbers. In effect, it stores numbers as arrays of digits rather than converting to a binary floating point representation. This allows arbitrary precision, though of course at the cost of speed.
To motivate ourselves, look at the following simple piece of code using floating point numbers:
if (3.2 - 2.0) == 1.2 puts "equal" else puts "not equal" # prints "not equal"! end
This is the sort of situation that BigDecimal helps with. However, note that with infinitely repeating decimals, we still will have problems. For yet another approach, see the upcoming section 5.9 “Working with Rational Values.”
A BigDecimal is initialized ...
Read now
Unlock full access