Skip to content

In JRuby, don't add sqrt, exp, log, power implemented in ruby#417

Merged
tompng merged 1 commit intoruby:masterfrom
tompng:jruby_skip
Sep 3, 2025
Merged

In JRuby, don't add sqrt, exp, log, power implemented in ruby#417
tompng merged 1 commit intoruby:masterfrom
tompng:jruby_skip

Conversation

@tompng
Copy link
Copy Markdown
Member

@tompng tompng commented Sep 2, 2025

Stub out ruby-implemented methods (sqrt exp log power) in JRuby for now.
These methods already exist in JRuby.
It depends on new feature only implemented in bigdecimal.c: BigDecimal(float_without_prec) and BigDecimal#_decimal_shift

There are some utility functions in lib/bigdecimal/bigdecimal.rb: BigDecimal::Internal.coerce_to_bigdecimal, BigDecimal::Internal.validate_prec, etc.
I want to use them in BigMath in the future. At that time, I think we need to remove this return and fix the ruby code.

Ruby-implemented sqrt, exp, log, power depends on new feature only implemented in bigdecimal.c:
`BigDecimal(float_without_prec)` and `BigDecimal#_decimal_shift`
@headius
Copy link
Copy Markdown
Contributor

headius commented Sep 2, 2025

@tompng Thanks for looking into this.

I believe we could implement _decimal_shift using the following Java methods, but it would not be in a JRuby release for some time:

https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#movePointLeft-int-

https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#movePointRight-int-

It could be implemented in Ruby right now via the following code:

class BigDecimal
  def _decimal_shift(i)
    java_bigdecimal = to_java
    if i < 0
      java_bigdecimal.move_point_right(-i).to_d
    else
      java_bigdecimal.move_point_left(i).to_d
    end
  end
end
BigDecimal("123.456")._decimal_shift(2) # => 0.123456e1
BigDecimal("123.456")._decimal_shift(-2) # => 0.123456e5

BigDecimal(float_without_prec) seems like a public API change that we should also implement in the JRuby extension.

Over time, we want to reduce the amount of custom code we have to maintain in our BigDecimal, but making two completely different implementations line up is not easy. 😭

@tompng
Copy link
Copy Markdown
Member Author

tompng commented Sep 3, 2025

Thank you 👍
Since _decimal_shift is currently an internal API (name may change), I'd like to implement _decimal_shift in ruby/bigdecimal using move_point_left and move_point_right soon.
But doing it, I think we need a CI for JRuby for at least methods defined in bigdecimal/bigdecimal.rb and bigdecimal/math.rb.

@tompng tompng merged commit 6177f46 into ruby:master Sep 3, 2025
79 checks passed
@tompng tompng deleted the jruby_skip branch September 3, 2025 07:14
@headius
Copy link
Copy Markdown
Contributor

headius commented Sep 4, 2025

@tompng CI on JRuby will be a big job but I agree that needs to happen. I think the JRuby BigDecimal implementation should move out of JRuby's repository (into this repo or a separate jruby/bigdecimal repo) so that we can fix issues more quickly and start reusing all of the .rb code.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants