Skip to content

Commit

Permalink
Replace round with div (#298)
Browse files Browse the repository at this point in the history
Use `div` with appropriate `RoundingMode` in `center` instead of `round`. This improves accuracy for large lengths.
  • Loading branch information
FedericoStra authored Jun 6, 2022
1 parent 83ab52e commit 1493303
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "OffsetArrays"
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
version = "1.12.2"
version = "1.12.3"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
9 changes: 8 additions & 1 deletion src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,13 @@ _no_offset_view(::Any, A::AbstractUnitRange) = UnitRange(A)
# These two helpers are deliberately not exported; their meaning can be very different in
# other scenarios and will be very likely to cause name conflicts if exported.
#####

if VERSION < v"1.4"
_halfroundInt(v, r::RoundingMode) = round(Int, v/2, r)
else
_halfroundInt(v, r::RoundingMode) = div(v, 2, r)
end

"""
center(A, [r::RoundingMode=RoundDown])::Dims
Expand Down Expand Up @@ -743,7 +750,7 @@ can use [`centered`](@ref OffsetArrays.centered).
"""
function center(A::AbstractArray, r::RoundingMode=RoundDown)
map(axes(A)) do inds
round(Int, (length(inds)-1)/2, r) + first(inds)
_halfroundInt(length(inds)-1, r) + first(inds)
end
end

Expand Down

2 comments on commit 1493303

@jishnub
Copy link
Member

@jishnub jishnub commented on 1493303 Jun 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/61826

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.12.3 -m "<description of version>" 1493303a94f4877ee5147ed0c3a1e9b7f20696b6
git push origin v1.12.3

Please sign in to comment.