Skip to content

Commit 1493303

Browse files
authored
Replace round with div (#298)
Use `div` with appropriate `RoundingMode` in `center` instead of `round`. This improves accuracy for large lengths.
1 parent 83ab52e commit 1493303

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "OffsetArrays"
22
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
3-
version = "1.12.2"
3+
version = "1.12.3"
44

55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/OffsetArrays.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,13 @@ _no_offset_view(::Any, A::AbstractUnitRange) = UnitRange(A)
705705
# These two helpers are deliberately not exported; their meaning can be very different in
706706
# other scenarios and will be very likely to cause name conflicts if exported.
707707
#####
708+
709+
if VERSION < v"1.4"
710+
_halfroundInt(v, r::RoundingMode) = round(Int, v/2, r)
711+
else
712+
_halfroundInt(v, r::RoundingMode) = div(v, 2, r)
713+
end
714+
708715
"""
709716
center(A, [r::RoundingMode=RoundDown])::Dims
710717
@@ -743,7 +750,7 @@ can use [`centered`](@ref OffsetArrays.centered).
743750
"""
744751
function center(A::AbstractArray, r::RoundingMode=RoundDown)
745752
map(axes(A)) do inds
746-
round(Int, (length(inds)-1)/2, r) + first(inds)
753+
_halfroundInt(length(inds)-1, r) + first(inds)
747754
end
748755
end
749756

0 commit comments

Comments
 (0)