What Julia does:
julia> hcat(5.5:-1:0.5, round.(5.5:-1:0.5))
6Γ2 Matrix{Float64}:
5.5 6.0
4.5 4.0
3.5 4.0
2.5 2.0
1.5 2.0
0.5 0.0
What I expect:
6Γ2 Matrix{Float64}:
5.5 6.0
4.5 5.0
3.5 4.0
2.5 3.0
1.5 2.0
0.5 1.0
What Julia does:
julia> hcat(5.5:-1:0.5, round.(5.5:-1:0.5))
6Γ2 Matrix{Float64}:
5.5 6.0
4.5 4.0
3.5 4.0
2.5 2.0
1.5 2.0
0.5 0.0
What I expect:
6Γ2 Matrix{Float64}:
5.5 6.0
4.5 5.0
3.5 4.0
2.5 3.0
1.5 2.0
0.5 1.0
See the documentation of the round
function. In particular:
The
RoundingMode
r
controls the direction of the rounding; the default isRoundNearest
, which rounds to the nearest integer, with ties (fractional values of 0.5) being rounded to the nearest even integer.
To obtain what you expect use
julia> hcat(5.5:-1:0.5, round.(5.5:-1:0.5, RoundUp))
6Γ2 Matrix{Float64}:
5.5 6.0
4.5 5.0
3.5 4.0
2.5 3.0
1.5 2.0
0.5 1.0
or
julia> hcat(5.5:-1:0.5, ceil.(5.5:-1:0.5))
6Γ2 Matrix{Float64}:
5.5 6.0
4.5 5.0
3.5 4.0
2.5 3.0
1.5 2.0
0.5 1.0
Specifically this rounding mode is the default because if you always round 0.5 up (or down), round
will on average slightly increase your numbers. By alternating whether 0.5
rounds up or down, you make it so that your mean isnβt changed.
I came to the conclusion: Rounding is wired^^
Thanks for all your inside