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

Numeric#round(n, mode) - round modes are not supported #1509

Closed
deepj opened this issue Dec 7, 2018 · 11 comments
Closed

Numeric#round(n, mode) - round modes are not supported #1509

deepj opened this issue Dec 7, 2018 · 11 comments

Comments

@deepj
Copy link

deepj commented Dec 7, 2018

At least Ruby 2.4 supports several round modes. See https://ruby-doc.org/stdlib-2.4.0/libdoc/bigdecimal/rdoc/BigDecimal.html#method-c-mode

Unfortunately, TruffleRuby triggers TypeError: Truffle doesn't have a case for the org.truffleruby.stdlib.bigdecimal.BigDecimalNodesFactory$RoundNodeFactory$RoundNodeGen node with values of type exception with any used round mode.

To reproduce:

require 'bigdecimal'

BigDecimal('1.516').round(2, :half_even)

Error:

TypeError: Truffle doesn't have a case for the org.truffleruby.stdlib.bigdecimal.BigDecimalNodesFactory$RoundNodeFactory$RoundNodeGen node with values of type  Truffle::BigDecimal(com.oracle.truffle.object.basic.DynamicObjectBasic) java.lang.Integer=2 Symbol(com.oracle.truffle.object.basic.DynamicObjectBasic)
	from (irb):5:in `round'
	from (irb):5
	from ~/.rubies/truffleruby-1.0.0-rc10/bin/irb:29:in `<main>'
@deepj deepj changed the title BigDecimal#round(n, mode) - round modes are not support BigDecimal#round(n, mode) - round modes are not supported Dec 7, 2018
@nirvdrum
Copy link
Collaborator

nirvdrum commented Dec 7, 2018

Thanks for the report. TruffleRuby does work with some round modes. It passes all the BigDecimal#round specs. It looks like the symbol-based modes are problematic. We probably need a lookup table to translate them to integer values. It shouldn't be too difficult.

It looks like we should also improve the Ruby Spec Suite by adding more cases to test.

@nirvdrum nirvdrum added the bug label Dec 7, 2018
@nirvdrum
Copy link
Collaborator

nirvdrum commented Dec 7, 2018

Are you sure that's supposed to work though? In MRI 2.4.x and 2.5.x, I get an ArgumentError:

> BigDecimal('1.516').round(2, :round_half_even)
ArgumentError: invalid rounding mode
	from (irb):2:in `round'
	from (irb):2
	from /home/kjmenard/.rbenv/versions/2.4.4/bin/irb:11:in `<main>'

So it looks like we might just need raise an ArgumentError with a better message.

@deepj
Copy link
Author

deepj commented Dec 7, 2018

@nirvdrum You have a typo. It's half_even, not round_half_even :)

@deepj
Copy link
Author

deepj commented Dec 7, 2018

screenshot 2018-12-07 at 23 43 21

@nirvdrum
Copy link
Collaborator

nirvdrum commented Dec 7, 2018

Right you are. I messed up translating BigDecimal::ROUND_HALF_EVEN to a symbol. Thanks for pointing that out.

@chrisseaton
Copy link
Collaborator

The three rounding issues #1509, #1510, #1514.

@deepj
Copy link
Author

deepj commented Feb 23, 2019

@eregon @chrisseaton Could this be a priority for RC14, please? In fact, this is one of issues why I started to report bugs in TruffleRuby. I'd like to run a quite trivial script importing exchange rates on TruffleRuby one day :)

@chrisseaton
Copy link
Collaborator

Sorry this hasn't managed to go into RC15, but we have a lot of time scheduled this month to look at this issue backlog.

@chrisseaton chrisseaton changed the title BigDecimal#round(n, mode) - round modes are not supported Numeric#round(n, mode) - round modes are not supported Apr 8, 2019
@chrisseaton
Copy link
Collaborator

Working on this now.

@chrisseaton chrisseaton self-assigned this Apr 8, 2019
@chrisseaton
Copy link
Collaborator

Fix for this done and on the way.

@chrisseaton
Copy link
Collaborator

Fixed in 0beec8a.

@eregon eregon added this to the 1.0.0-rc16 milestone Apr 10, 2019
jcmfernandes added a commit to jcmfernandes/truffleruby that referenced this issue Jan 13, 2023
jcmfernandes added a commit to jcmfernandes/truffleruby that referenced this issue Jan 13, 2023
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

4 participants