Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigDecimal initializer does not mirror YARV behavior #3242

Open
garysweaver opened this issue Aug 10, 2015 · 0 comments
Open

BigDecimal initializer does not mirror YARV behavior #3242

garysweaver opened this issue Aug 10, 2015 · 0 comments

Comments

@garysweaver
Copy link

The BigDecimal API doc says the second argument (digits) for BigDecimal's initializer should be: "The number of significant digits, as a Fixnum. If omitted or 0, the number of significant digits is determined from the initial value." But, in reality, Ruby 2.2.2 seems to ignore the second argument unless it is higher than 18, at least on my system, e.g.

2.2.2 :001 > require 'bigdecimal'
 => true 
2.2.2 :002 > BigDecimal.new '31.0344830', 0
 => #<BigDecimal:7fbc4c1ee5c8,'0.31034483E2',18(18)> 
2.2.2 :003 > BigDecimal.new '31.0344830', 3
 => #<BigDecimal:7ffc0114ca80,'0.31034483E2',18(18)> 
2.2.2 :004 > BigDecimal.new '31.0344830', 18
 => #<BigDecimal:7ffc01083dd8,'0.31034483E2',18(18)> 
2.2.2 :005 > BigDecimal.new '31.0344830', 19
 => #<BigDecimal:7ffc01073e38,'0.31034483E2',18(27)> 
2.2.2 :006 > BigDecimal.new '31.0344830', 199
 => #<BigDecimal:7ffc01870c58,'0.31034483E2',18(207)>
2.2.2 :007 > BigDecimal.new '31.03', 0
 => #<BigDecimal:7ffc01031060,'0.3103E2',18(18)> 
2.2.2 :008 > BigDecimal.new '31.03', 1
 => #<BigDecimal:7ffc030935d8,'0.3103E2',18(18)> 
2.2.2 :009 > BigDecimal.new '31.03', 18
 => #<BigDecimal:7ffc03088e80,'0.3103E2',18(18)> 
2.2.2 :010 > BigDecimal.new '31.03', 19
 => #<BigDecimal:7ffc0307aba0,'0.3103E2',18(27)> 
2.2.2 :011 > BigDecimal.new '31.03', 199
 => #<BigDecimal:7ffc0306a250,'0.3103E2',18(207)> 

JRuby 9.0.0.0 on the other hand seems to try to set scale using some other mechanism that appears to be based on the value of the string used for initialization and from what I can tell, and seems to be ignoring the second argument:

jruby-9.0.0.0 :001 > require 'bigdecimal'
 => true 
jruby-9.0.0.0 :002 > BigDecimal.new '31.0344830', 0
 => #<BigDecimal:6ff65192,'0.31034483E2',8(12)> 
jruby-9.0.0.0 :003 > BigDecimal.new '31.0344830', 3
 => #<BigDecimal:2dd80673,'0.31034483E2',8(12)> 
jruby-9.0.0.0 :004 > BigDecimal.new '31.0344830', 18
 => #<BigDecimal:4af0df05,'0.31034483E2',8(12)> 
jruby-9.0.0.0 :005 > BigDecimal.new '31.0344830', 19
 => #<BigDecimal:57ea113a,'0.31034483E2',8(12)> 
jruby-9.0.0.0 :006 > BigDecimal.new '31.0344830', 199
 => #<BigDecimal:acdb094,'0.31034483E2',8(12)> 
jruby-9.0.0.0 :007 > BigDecimal.new '31.03', 0
 => #<BigDecimal:674bd420,'0.3103E2',4(8)> 
jruby-9.0.0.0 :008 > BigDecimal.new '31.03', 1
 => #<BigDecimal:2ceb80a1,'0.3103E2',4(8)> 
jruby-9.0.0.0 :009 > BigDecimal.new '31.03', 18
 => #<BigDecimal:4b45dcb8,'0.3103E2',4(8)> 
jruby-9.0.0.0 :010 > BigDecimal.new '31.03', 19
 => #<BigDecimal:7216fb24,'0.3103E2',4(8)> 
jruby-9.0.0.0 :011 > BigDecimal.new '31.03', 199
 => #<BigDecimal:2072acb2,'0.3103E2',4(8)> 

I've not looked into it fully yet, but I'm not sure what the workaround should be to ensure both YARV and JRuby's BigDecimal work the same in this regard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants