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 and coerce failing on subtraction operation #5386

Closed
seandmccarthy opened this Issue Oct 29, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@seandmccarthy
Copy link
Contributor

seandmccarthy commented Oct 29, 2018

Environment

$ jruby -v jruby 9.2.0.0 (2.5.0) 2018-05-24 81156a8 Java HotSpot(TM) 64-Bit Server VM 25.161-b12 on 1.8.0_161-b12 +jit [darwin-x86_64]

Also can be replicated on 9.1.17.0 and 1.7.27

$ uname -a Darwin [redacted] 17.4.0 Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64 x86_64

Issue also present on Windows 7

C:\Users\Sean Mccarthy>jruby -v jruby 9.1.17.0 (2.3.3) 2018-04-20 d8b1ff9 Java HotSpot(TM) 64-Bit Server VM 25.181-b13 on 1.8.0_181-b13 +jit [mswin32-x86_64]

Expected Behavior

Here's a test case:

require 'bigdecimal'

class NullValue
  def nil?
    true
  end

  def coerce(other)
    case other
    when Numeric
      [other, BigDecimal(0)]
    end
  end

  def method_missing(method_name, *arguments, &block)
    BigDecimal(0).send(method_name, *arguments, &block)
  end
end

puts "Trying addition:"
puts BigDecimal(100) + NullValue.new
puts "Trying multiplication:"
puts BigDecimal(100) * NullValue.new
puts "Trying division:"
puts BigDecimal(100) / NullValue.new
puts "Trying subtraction:"
puts BigDecimal(100) - NullValue.new

Output from MRI/CRuby 2.3.x

$ ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [x86_64-darwin17]
$ ruby null_value.rb
Trying addition:
0.1E3
Trying multiplication:
0.0
Trying division:
Infinity
Trying subtraction:
0.1E3

Actual Behavior

$ ruby -v
jruby 9.2.0.0 (2.5.0) 2018-05-24 81156a8 Java HotSpot(TM) 64-Bit Server VM 25.161-b12 on 1.8.0_161-b12 +jit [darwin-x86_64]
$ ruby null_value.rb
Trying addition:
0.1e3
Trying multiplication:
0.0
Trying division:
Infinity
Trying subtraction:
TypeError: NullValue can't be coerced into BigDecimal
       - at org/jruby/ext/bigdecimal/RubyBigDecimal.java:1134
  <main> at null_value.rb:27

@kares kares added the beginner label Oct 30, 2018

@seandmccarthy

This comment has been minimized.

Copy link
Contributor Author

seandmccarthy commented Oct 31, 2018

I think the issue is here:

return subInternal(context, getVpValueWithPrec(context, b, true), b, 0);

The 3rd parameter to getVpValueWithPrec (the must value) is true, but in similar contexts, e.g.

return addInternal(context, getVpValueWithPrec(context, b, false), b, vpPrecLimit(context.runtime));

the must value is false.

This means that the cannotBeCoerced method immediately throws an exception rather than returning null and letting the coercion behaviour occur later.

private static RubyBigDecimal cannotBeCoerced(ThreadContext context, IRubyObject value, boolean must) {
if (must) {
throw context.runtime.newTypeError(
errMessageType(context, value) + " can't be coerced into BigDecimal"
);
}
return null;
}

seandmccarthy pushed a commit to seandmccarthy/jruby that referenced this issue Oct 31, 2018

@kares kares closed this in f7ef0fb Oct 31, 2018

@kares kares added this to the JRuby 9.2.1.0 milestone Oct 31, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.