Skip to content

Fix Time subsecond precision loss from floating point arithmetic#9261

Merged
headius merged 2 commits intojruby:jruby-10.0from
evaniainbrooks:test-time-precision-error
Mar 4, 2026
Merged

Fix Time subsecond precision loss from floating point arithmetic#9261
headius merged 2 commits intojruby:jruby-10.0from
evaniainbrooks:test-time-precision-error

Conversation

@evaniainbrooks
Copy link
Copy Markdown
Contributor

Use integer arithmetic when computing millis/nanos from Rational

Fixes Psych::Visitors::TestToRuby#test_time

https://github.com/jruby/jruby/actions/runs/22045652593/job/63693879405

Use integer arithmetic when computing millis/nanos from Rational

Fixes Psych::Visitors::TestToRuby#test_time

https://github.com/jruby/jruby/actions/runs/22045652593/job/63693879405
@evaniainbrooks evaniainbrooks force-pushed the test-time-precision-error branch from 30ca982 to 539085c Compare February 25, 2026 02:15
@headius
Copy link
Copy Markdown
Member

headius commented Feb 25, 2026

Those failures are not your fault. Master merged 10.1 and something needs to be tweaked.

Thank you! I have been running into this occasionally on our CI but never pinned it down.

@headius headius changed the base branch from master to jruby-10.0 February 25, 2026 04:12
@headius headius added this to the JRuby 10.0.4.0 milestone Feb 25, 2026
@headius
Copy link
Copy Markdown
Member

headius commented Feb 25, 2026

Suggested change to eliminate one BigInteger:

diff --git a/core/src/main/java/org/jruby/RubyTime.java b/core/src/main/java/org/jruby/RubyTime.java
index d6a99b6450..5add2109db 100644
--- a/core/src/main/java/org/jruby/RubyTime.java
+++ b/core/src/main/java/org/jruby/RubyTime.java
@@ -113,6 +113,7 @@ public class RubyTime extends RubyObject {
     private static final BigDecimal ONE_MILLION_BD = BigDecimal.valueOf(1000000);
     private static final BigDecimal ONE_BILLION_BD = BigDecimal.valueOf(1000000000);
     public static final int TIME_SCALE = 1_000_000_000;
+    public static final BigInteger TIME_SCALE_BI = BigInteger.valueOf(RubyTime.TIME_SCALE);
     public static final int TIME_SCALE_DIGITS = 9;
 
     private DateTime dt;
diff --git a/core/src/main/java/org/jruby/util/time/TimeArgs.java b/core/src/main/java/org/jruby/util/time/TimeArgs.java
index eaed31c7e5..fb445f231f 100644
--- a/core/src/main/java/org/jruby/util/time/TimeArgs.java
+++ b/core/src/main/java/org/jruby/util/time/TimeArgs.java
@@ -136,7 +136,7 @@ public class TimeArgs {
                     }
 
                     long subSeconds = BigInteger.valueOf(numerator)
-                            .multiply(BigInteger.valueOf(TIME_SCALE))
+                            .multiply(RubyTime.TIME_SCALE_BI)
                             .divide(BigInteger.valueOf(denominator))
                             .longValue();
 

There's still a lot of narrowing conversions in this code... is there a reason why we shouldn't just use BigInteger throughout? Maybe there's a precision threshold we've already met with your logic, but going back and forth between longs and BigInteger makes me wonder. 🤔

@evaniainbrooks evaniainbrooks force-pushed the test-time-precision-error branch from 96d3c41 to 0ff29d1 Compare February 25, 2026 13:41
@headius
Copy link
Copy Markdown
Member

headius commented Mar 3, 2026

@evaniainbrooks You still have this marked as draft. Is there any more?

@headius headius modified the milestones: JRuby 10.0.4.0, JRuby 10.0.5.0 Mar 3, 2026
@evaniainbrooks
Copy link
Copy Markdown
Contributor Author

I had another commit prepared to use BigInteger throughout, but, after reasoning through it, it does seem that this is enough.

@evaniainbrooks evaniainbrooks marked this pull request as ready for review March 3, 2026 21:26
@evaniainbrooks
Copy link
Copy Markdown
Contributor Author

@headius marked as ready

@headius headius merged commit 148f885 into jruby:jruby-10.0 Mar 4, 2026
79 checks passed
@headius
Copy link
Copy Markdown
Member

headius commented Mar 4, 2026

Thank you! I have some refactoring ideas that I'll push as a separate PR.

@headius
Copy link
Copy Markdown
Member

headius commented Mar 4, 2026

#9292

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