Skip to content

Fix overflow when rounding#42033

Merged
simonbyrne merged 1 commit intoJuliaLang:masterfrom
petvana:petvana-fix-round-3
Sep 7, 2021
Merged

Fix overflow when rounding#42033
simonbyrne merged 1 commit intoJuliaLang:masterfrom
petvana:petvana-fix-round-3

Conversation

@petvana
Copy link
Copy Markdown
Member

@petvana petvana commented Aug 27, 2021

This PR aims to fix #37171. It simply uses a smaller step (square root of it) twice if it overflows. Otherwise, the computation remains exactly the same as before (and thus should not break any code).

Cc to the original author: @simonbyrne

The performance remains almost the same. Tested on i5-1035G1 by

for dig=1:7
    @btime round($(rand()), sigdigits = $dig)
end

Master:

  13.995 ns (0 allocations: 0 bytes)
  14.338 ns (0 allocations: 0 bytes)
  14.582 ns (0 allocations: 0 bytes)
  23.895 ns (0 allocations: 0 bytes)
  125.770 ns (0 allocations: 0 bytes)
  125.735 ns (0 allocations: 0 bytes)
  125.771 ns (0 allocations: 0 bytes)

This PR:

  14.233 ns (0 allocations: 0 bytes)
  14.536 ns (0 allocations: 0 bytes)
  15.044 ns (0 allocations: 0 bytes)
  24.183 ns (0 allocations: 0 bytes)
  126.037 ns (0 allocations: 0 bytes)
  126.018 ns (0 allocations: 0 bytes)
  126.134 ns (0 allocations: 0 bytes)

(The performance drop for 5+ digits is caused by the exponent while computing the step, see #42031)

@petvana petvana marked this pull request as ready for review August 27, 2021 17:13
@petvana
Copy link
Copy Markdown
Member Author

petvana commented Sep 3, 2021

@simonbyrne Would you mind checking this bugfix as the original author?

@simonbyrne simonbyrne merged commit 3aea11e into JuliaLang:master Sep 7, 2021
@simonbyrne
Copy link
Copy Markdown
Member

Thanks @petvana! Apologies for the delay.

LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
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.

Overflow when rounding

2 participants