views:

69

answers:

1

I just switched my Ruby version to 1.9.2, and the BigDecimal code works in Ruby 1.8 doesn't working anymore. Here is test code show what happened

irb(main):001:0> require 'bigdecimal'
=> true
irb(main):002:0> (BigDecimal.new("1")/BigDecimal.new("3")).to_s("F")
=> "0.33333333"
irb(main):003:0> (BigDecimal.new("1", 20)/BigDecimal.new("3", 20)).to_s("F")
=> "0.33333333"

Problem with my Ruby installation? Otherwise I think even in Ruby 1.9, above testing code still should work, what's going on here?

A: 

Seems changes in Ruby 1.9 make '/' will not get it significant digits specified from two operand, which works in Ruby 1.8.

Above code wouldn't work because two operand for '/' will only have on significant digitals, and make it float num, and float num will always generate float result by using '/' method.

Instead, in that situation, I should use div(value, digits)

(BigDecimal.new("1", 20).div(BigDecimal.new("3", 20), 50)).to_s("F")
=> "0.33333333333333333333333333333333333333333333333333"

Hope that make sense.

pstar
so is this a bug or not?
rogerdpack