# Round inplace?

**URL:** <https://discourse.julialang.org/t/round-inplace/43298>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 18, 2020, 5:44pm UTC](https://discourse.julialang.org/t/round-inplace/43298 "2020-07-18T17:44:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [July 18, 2020, 5:44pm UTC](https://discourse.julialang.org/t/round-inplace/43298/1 "2020-07-18T17:44:03Z")

</div>

What is the reason that there are not `round!` functions? Is it not possible to do that type of operation in place? It seems like it is no different than `replace!` (which exists) and the thing you are replacing with is simply a rounded version of the original value (without the need to search for it).

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [July 18, 2020, 5:53pm UTC](https://discourse.julialang.org/t/round-inplace/43298/2 "2020-07-18T17:53:46Z")

</div>

No operations on floating-point numbers are in-place since they are immutable.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [July 18, 2020, 5:54pm UTC](https://discourse.julialang.org/t/round-inplace/43298/3 "2020-07-18T17:54:06Z")

</div>

Sorry, I mean for vectors

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [July 18, 2020, 5:55pm UTC](https://discourse.julialang.org/t/round-inplace/43298/4 "2020-07-18T17:55:46Z")

</div>

Because its not common to need it and it’s a one-liner to write it yourself using broadcasting, I guess.

`v .= round.(v)`

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [July 18, 2020, 6:04pm UTC](https://discourse.julialang.org/t/round-inplace/43298/5 "2020-07-18T18:04:59Z")

</div>

Fair enough. I am rounding a vector with \>100M values so it takes a bit to finish and so this came to mind.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 18, 2020, 6:32pm UTC](https://discourse.julialang.org/t/round-inplace/43298/6 "2020-07-18T18:32:43Z")

</div>

`replace!` isn’t so easy to replace with a dotted version, while in-place `round` is trivial to write with dots.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [July 18, 2020, 7:20pm UTC](https://discourse.julialang.org/t/round-inplace/43298/7 "2020-07-18T19:20:43Z")

</div>

I guess the real question is why you need this – i.e. what you will do with the 100M results.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 18, 2020, 8:34pm UTC](https://discourse.julialang.org/t/round-inplace/43298/8 "2020-07-18T20:34:11Z")

</div>

> [@dpsanders](#):
>
> v .= round.(v)

It is worth noting that the @dpsanders answer use dot fusion to not create an intermediary array so, in that aspect, it is as efficient as a `round!` could get.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [July 18, 2020, 8:51pm UTC](https://discourse.julialang.org/t/round-inplace/43298/9 "2020-07-18T20:51:07Z")

</div>

Almost:

```julia
julia> function round1!(v)
           v .= round.(v)
       end
round1! (generic function with 1 method)

julia> function round2!(v)
       for i in eachindex(v)
           v[i] = round(v[i])
       end
       end
round2! (generic function with 1 method)

julia> v = 10 .* rand(1000);

julia> function round2!(v)
       for i in eachindex(v)
           @inbounds v[i] = round(v[i])
       end
       end
round2! (generic function with 1 method)

julia> @btime round1!($v);
  69.519 ns (0 allocations: 0 bytes)

julia> @btime round2!($v);
  66.269 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 18, 2020, 9:07pm UTC](https://discourse.julialang.org/t/round-inplace/43298/10 "2020-07-18T21:07:39Z")

</div>

Ok, but:

```julia
julia> using BenchmarkTools
julia> function round1!(v)
           @inbounds v .= round.(v)
       end
julia> function round2!(v)
           for i in eachindex(v)
               @inbounds v[i] = round(v[i])
           end
       end
julia> import Random
julia> Random.seed!(0);
julia> v = 1000 .* rand(100000);
julia> @btime round1!($v)
  20.245 μs (0 allocations: 0 bytes)
julia> @btime round2!($v)
  19.232 μs (0 allocations: 0 bytes)
julia> @btime round1!($v)
  19.132 μs (0 allocations: 0 bytes)
julia> @btime round2!($v)
  19.166 μs (0 allocations: 0 bytes)
julia> @btime round1!($v)
  19.512 μs (0 allocations: 0 bytes)
julia> @btime round2!($v)
  19.248 μs (0 allocations: 0 bytes

```

Seems negligible to me.
