← Back to Pull requests

Make `round`/`floor`/`ceil`/`trunc` with `digits` keyword more accurate. #63105

Open
unknown opened this pull request with 451 lines diff across 5 files
View on GitHub
unknown unknown commented on Sep 10, 2026 22:07 UTC
Author

This fixes https://github.com/JuliaLang/julia/issues/63040, and also fixes a subtle case mentioned in the docstring of round:

  │ Note
  │
  │  Rounding to specified digits in bases other than 2 can be inexact when
  │  operating on binary floating point numbers. For example, the Float64 value
  │  represented by 1.15 is actually less than 1.15, yet will be rounded to 1.2.
  │  For example:
  │
  │  julia> x = 1.15
  │  1.15
  │  
  │  julia> big(1.15)
  │  1.149999999999999911182158029987476766109466552734375
  │  
  │  julia> x < 115//100
  │  true
  │  
  │  julia> round(x, digits=1)
  │  1.2

The idea is to basically use the TwicePrecision machinery and some other stuff to always faithfully round in the right direction (provided base^digits is a number we can represent). Following @adienes's suggestion, I added _mul_round and _div_round functions which implement "multiply and round" and "divide and round" more accurately. If people think there's a case for making these public, I can do so.

__________

Measured on my machine, this change has a small but real impact on performance. For example:

using BenchmarkTools
@benchmark round(x; digits=1) setup=x=rand(Int)/rand(Int)

Before:
BenchmarkTools.Trial: 10000 samples with 1000 evaluations per sample.
 Range (min … max):  4.588 ns … 39.294 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     4.769 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   5.012 ns ±  0.961 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▄▆▅▆█▁                                            ▂ ▁▁▂  ▂ ▁
  ██████▄▄▃▄▃▂▂▂▂▄▃▂▂▃▄▄▄▂▅▄▄▃▅▅▄▅▃▄▅▅▄▅▆▅▆▆▆▇▆▆▆▅▅▅█▆███▅▅█ █
  4.59 ns      Histogram: log(frequency) by time      7.2 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

After:
BenchmarkTools.Trial: 10000 samples with 1000 evaluations per sample.
 Range (min … max):  6.402 ns … 15.078 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     6.613 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   6.630 ns ±  0.346 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▁ ▃ ▁         ▁  ▂     ▂     ▂▁ ▃   ▃ ▆ ▃   ▇▇ █ ▁   ▂     ▂
  █▁█▁█▁▃▁▁▁▁▁▁▁█▆▁█▁▆▁█▁█▁█▁▁▁██▁█▁▅▁█▁█▁█▁▆▆██▁█▁█▁█▁█▁█▁▃ █
  6.4 ns       Histogram: log(frequency) by time     6.67 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

and

using BenchmarkTools
@benchmark round(x; digits=1) setup=(x=rand(Int))

Before:
BenchmarkTools.Trial: 10000 samples with 1000 evaluations per sample.
 Range (min … max):  4.718 ns … 36.599 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     4.879 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   4.912 ns ±  0.593 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

       █▄                                                     
  ▃▃▂▃▃██▂▂▂▁▁▁▁▁▂▁▂▂▂▂▂▂▁▁▂▁▁▁▁▁▁▂▂▂▁▂▂▂▂▂▂▁▂▁▂▂▁▂▂▂▁▁▁▂▂▂▂ ▂
  4.72 ns        Histogram: frequency by time        6.33 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

After:
BenchmarkTools.Trial: 10000 samples with 1000 evaluations per sample.
 Range (min … max):  5.750 ns … 12.814 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     5.941 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   5.965 ns ±  0.369 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

       █                                                      
  ▃▂▂▃▅█▂▂▂▁▁▂▂▁▁▂▁▁▂▂▁▂▁▂▁▁▁▁▂▁▁▁▁▁▂▁▂▁▂▂▂▁▁▂▂▂▂▁▂▂▂▂▂▂▁▁▁▂ ▂
  5.75 ns        Histogram: frequency by time        7.92 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

Assisted by Claude Code (Fable 5.1)

Diff: 451 lines (+338 / -113) across 5 files Reviewer: mikmoore Review duration: 116778m
All Vouch review confidence checks passed
Residual risk 0.29 = Change Risk (0.52) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 451 lines diff across 5 files
0.52
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
44%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.366
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #63105"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 44.7% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 52.4% (MEDIUM) · Higher = larger blast radius
52.4%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 44.7% · Lower = rushed or shallow review
44.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 29.0% · Safe to merge
29.0%