# Why does rounding behave so weird?

**URL:** <https://discourse.julialang.org/t/why-does-rounding-behave-so-weird/121422>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 17, 2024, 12:59pm UTC](https://discourse.julialang.org/t/why-does-rounding-behave-so-weird/121422 "2024-10-17T12:59:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SunnyUser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunnyuser/32/206001_2.png) [@SunnyUser](https://discourse.julialang.org/u/SunnyUser)\
**Post date:** [October 17, 2024, 12:59pm UTC](https://discourse.julialang.org/t/why-does-rounding-behave-so-weird/121422/1 "2024-10-17T12:59:06Z")

</div>

What Julia does:

```julia
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:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 17, 2024, 1:05pm UTC](https://discourse.julialang.org/t/why-does-rounding-behave-so-weird/121422/2 "2024-10-17T13:05:45Z")

</div>

See the [documentation of the `round` function](https://docs.julialang.org/en/v1/base/math/#Base.round). In particular:

> The [`RoundingMode`](https://docs.julialang.org/en/v1/base/math/#Base.Rounding.RoundingMode) `r` controls the direction of the rounding; the default is [`RoundNearest`](https://docs.julialang.org/en/v1/base/math/#Base.Rounding.RoundNearest), which rounds to the nearest integer, with ties (fractional values of 0.5) being rounded to the nearest even integer.

---

<div class="post-metadata">

**Author:** ![cshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshen/32/217287_2.png) [@cshen](https://discourse.julialang.org/u/cshen)\
**Post date:** [October 17, 2024, 1:09pm UTC](https://discourse.julialang.org/t/why-does-rounding-behave-so-weird/121422/3 "2024-10-17T13:09:57Z")

</div>

To obtain what you expect use

```julia
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
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

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 17, 2024, 1:26pm UTC](https://discourse.julialang.org/t/why-does-rounding-behave-so-weird/121422/4 "2024-10-17T13:26:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SunnyUser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunnyuser/32/206001_2.png) [@SunnyUser](https://discourse.julialang.org/u/SunnyUser)\
**Post date:** [October 17, 2024, 1:29pm UTC](https://discourse.julialang.org/t/why-does-rounding-behave-so-weird/121422/5 "2024-10-17T13:29:07Z")

</div>

I came to the conclusion: Rounding is wired^^  
Thanks for all your inside
