jruby icon indicating copy to clipboard operation
jruby copied to clipboard

BigDecimal::limit has not effect

Open nmk opened this issue 12 years ago • 10 comments

Using BigDecimal::limit has not effect on following computations with BigDecimal instances. This works in MRI 1.9.3 and later.

Compare the following two sessions:

MRI (1.9.3-p545):

~ $ irb -r"bigdecimal" -r "bigdecimal/util"
1.9.3-p545 :001 > BigDecimal.limit(3)
 => 0
1.9.3-p545 :002 > 10.to_d / 3.to_d
 => #<BigDecimal:7fd034801d38,'0.333E1',18(36)>
1.9.3-p545 :003 > (10.to_d / 3.to_d).to_f
 => 3.33
1.9.3-p545 :004 > (10.to_d / 3.to_d).to_s("F")
 => "3.33"  

Latest JRuby (1.7.11):

~ $ irb -r"bigdecimal" -r"bigdecimal/util"
jruby-1.7.11 :001 > BigDecimal.limit(3)
 => 0
jruby-1.7.11 :002 > 10.to_d / 3.to_d
 => #<BigDecimal:136c2147,'0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333E1',200(204)>
jruby-1.7.11 :003 > (10.to_d / 3.to_d).to_f
 => 3.3333333333333335
jruby-1.7.11 :004 > (10.to_d / 3.to_d).to_s("F")
 => "3.3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333"

nmk avatar Apr 09 '14 13:04 nmk

I can reproduce this in 1.7.12.

masover avatar Apr 29 '14 16:04 masover

I can reproduce it as recently as 5ee515f6ae0dcf4f98a30ff1b95c4d7c81ab880d.

The spec that probably should catch this is in spec/ruby/library/bigdecimal/limit_spec.rb. It currently checks that rounding is applied correctly on addition and multiplication, but doesn't test division. (And addition and multiplication do follow the global limit setting.) And division works fine with a local limit:

$ irb -r"bigdecimal" -r"bigdecimal/util"
irb(main):001:0> 10.to_d.div(3.to_d, 3)
=> #<BigDecimal:1bb82408,'0.333E1',3(4)>
irb(main):002:0> 10.to_d.div(3.to_d, 3).to_f
=> 3.33

So I'm optimistic about this one.

masover avatar Apr 30 '14 09:04 masover

https://github.com/dummey/jruby/blob/cd1677eefe3fd79d9ea69660ab3f781457789311/spec/ruby/library/bigdecimal/limit_spec.rb

Adding some checks for subtraction and division for the fun of it.

Edit: https://github.com/dummey/jruby/blob/51a4ae12719a6b6491e403917f3976c061e38d23/spec/ruby/library/bigdecimal/limit_spec.rb

Divided up the spec to make it easier to see what is failing. It looks like subtraction is fine, it's just division that is not adhering to the limit.

dummey avatar May 03 '14 00:05 dummey

Interesting...

// regular division with some default precision
// TODO: proper algorithm to set the precision

From: jruby/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java:[861]

dummey avatar May 03 '14 01:05 dummey

I actually have something I could submit, but the spec (yours or mine) should probably go in RubySpec first?

And, that's exactly where I ended up. I still don't have a proper algorithm, but it's not difficult to at least enforce a limit (if one is provided).

diff --git a/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java b/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java
index 383747a..309aaae 100644
--- a/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java
+++ b/core/src/main/java/org/jruby/ext/bigdecimal/RubyBigDecimal.java
@@ -856,7 +856,12 @@ public class RubyBigDecimal extends RubyNumeric {
         RubyObject preciseOther = getVpValue19(context, other, true);
         // regular division with some default precision
         // TODO: proper algorithm to set the precision
-        return op_div(context, preciseOther, getRuntime().newFixnum(200));
+        Ruby runtime = getRuntime();
+        IRubyObject lim = bigDecimal(runtime).searchInternalModuleVariable("vpPrecLimit");
+        if (lim.equals(RubyFixnum.zero(runtime))) {
+            lim = getRuntime().newFixnum(200);
+        }
+        return op_div(context, preciseOther, lim);
     }

     public IRubyObject op_div(ThreadContext context, IRubyObject other) {

If you can wait, I can build a proper pull request. If you want to take my patch and run with it, I won't be offended.

masover avatar May 03 '14 01:05 masover

You should go for it =D

I'm exploring and tagging along largely to learn more about the inner workings of JRuby, so this would be a great chance for me to 'stalk' what you do.

dummey avatar May 03 '14 01:05 dummey

This bug has been open for 2 years. Surprised nobody else has felt the pain!

Since #limit is not working for division, I have to post process all my output data, and traverse every field in my data structures and manually round.

This is cumbersome. Can someone help fix this? Thanks

dymaxionuk avatar Jul 22 '16 06:07 dymaxionuk

@dymaxionuk you could have tried fixing it than or posting a bounty. it shouldn't be very hard and there's even some code in the comments to start with ...

kares avatar Jul 22 '16 10:07 kares

I made a pull request for this: https://github.com/jruby/jruby/pull/4830

MrBerg avatar Oct 27 '17 11:10 MrBerg

Fixed in 9.1.14 and 9.2. See #4830 for details.

headius avatar Oct 30 '17 19:10 headius