# Inplace modification of Float64

**URL:** <https://discourse.julialang.org/t/inplace-modification-of-float64/118995>\
**Category:** New to Julia\
**Tags:** inplace, immutable\
**Created:** [September 3, 2024, 2:09pm UTC](https://discourse.julialang.org/t/inplace-modification-of-float64/118995 "2024-09-03T14:09:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)\
**Post date:** [September 3, 2024, 2:09pm UTC](https://discourse.julialang.org/t/inplace-modification-of-float64/118995/1 "2024-09-03T14:09:58Z")

</div>

Hi, I know that things like Float64 are immutable, so I can’t do inplace modification (in my specific situation: I can’t call `clamp!` on them).

I’m writing some code in which I either want to apply a routine on an entire vector simultaneously (in which case `clamp!` works fine) or by looping over Float64’s (in which case `clamp!` doesn’t work).

I can do two things: either forgo inplace modification in the Float64 case, or “make” the Float64 mutable: see the lines for `u` and `w` in my example. Which approach would typically be preferred performance-wise? Intuitively I’d think that not doing inplace modification is probably quicker here than all the extra referencing that you’d need to do by using `Ref`? I’m curious to hear your thoughts!

```julia
function my_clamp!(x, a, b)
    if x < a
        x = a
    elseif x > b
        x = b
    end
    return x
end

function my_clamp_ref!(x, a, b)
    if x[] < a
        x[] = a
    elseif x[] > b
        x[] = b
    end
    return x
end

a = -1.0
b = 1.0

u = 2.0
display("works") # prints 2.0, then 1.0 
display(u)
u = my_clamp!(u, a, b)
display(u)

v = 2.0
display("doesn't work") # prints 2.0, then 2.0
display(v)
my_clamp!(v, a, b)
display(v)

w = Ref(2.0)
display("works") # prints Base.RefValue{Float64}(2.0) then Base.RefValue{Float64}(1.0)
display(w)
my_clamp_ref!(w, a, b)
display(w)

```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [September 3, 2024, 2:34pm UTC](https://discourse.julialang.org/t/inplace-modification-of-float64/118995/2 "2024-09-03T14:34:42Z")

</div>

> [@JADekker](#):
>
> I either want to apply a routine on an entire vector simultaneously

Could you give a more complete example of what you want?

> [@JADekker](#):
>
> I can do two things: either forgo inplace modification in the Float64 case, or “make” the Float64 mutable

There could be a third option (inspired by MutableArithmetics.jl), design an optionally mutating API.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 3, 2024, 2:44pm UTC](https://discourse.julialang.org/t/inplace-modification-of-float64/118995/3 "2024-09-03T14:44:14Z")

</div>

`clamp!` is something of an outlier in being defined to work on arrays. If you replace it with

```julia
u .= clamp.(u, a, b)

```

you have the usual duality between broadcasting and looping:

```julia
x .= f.(x)

```

vs

```julia
for i in eachindex(x)
    x[i] = f(x[i])
end

```

(This observation may or may not be helpful. It’s difficult to guess how your real problem generalizes outside of clamping.)

---

<div class="post-metadata">

**Author:** ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)\
**Post date:** [September 3, 2024, 3:45pm UTC](https://discourse.julialang.org/t/inplace-modification-of-float64/118995/4 "2024-09-03T15:45:41Z")

</div>

I’m somehow failing to reproduce this in my original setting at present, so I’ll close this question for now, returning to it if the issue resurfaces.

---

<div class="post-metadata">

**Author:** ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)\
**Post date:** [September 3, 2024, 3:45pm UTC](https://discourse.julialang.org/t/inplace-modification-of-float64/118995/5 "2024-09-03T15:45:55Z")

</div>

Thank you for your help nonetheless!
