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

seandmccarthy opened this Issue Oct 29, 2018 · 1 comment


None yet
2 participants
Copy link

seandmccarthy commented Oct 29, 2018


$ jruby -v jruby (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 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 (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?

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

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

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

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:
Trying multiplication:
Trying division:
Trying subtraction:

Actual Behavior

$ ruby -v
jruby (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:
Trying multiplication:
Trying division:
Trying subtraction:
TypeError: NullValue can't be coerced into BigDecimal
       - at org/jruby/ext/bigdecimal/
  <main> at null_value.rb:27

@kares kares added the beginner label Oct 30, 2018


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 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.