# Should inv(::Rational) make copies of the numerator and denominator?

**URL:** <https://discourse.julialang.org/t/should-inv-rational-make-copies-of-the-numerator-and-denominator/88314>\
**Category:** Internals & Design\
**Created:** [October 5, 2022, 10:37pm UTC](https://discourse.julialang.org/t/should-inv-rational-make-copies-of-the-numerator-and-denominator/88314 "2022-10-05T22:37:48Z")\
**Posts on this page:** 4\
**Page:** 1

<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:** [October 5, 2022, 10:37pm UTC](https://discourse.julialang.org/t/should-inv-rational-make-copies-of-the-numerator-and-denominator/88314/1 "2022-10-05T22:37:48Z")

</div>

Usually operations on Rational numbers don’t return references to their arguments:

```julia
julia> d = Rational{BigInt}(2)
2//1

julia> d.num === (1*d).num
false

julia> d.num === (0+d).num
false

```

I assumed that this holds in general, but I was wrong:

```julia
julia> d.num === inv(d).den
true

```

It turns out that `inv(::Rational)` returns the exact same numerator and denominator objects, just switched around in the Rational object: [julia/rational.jl at master · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/master/base/rational.jl#L362)

Sometimes this distinctions is irrelevant, but this behavior was quite a head-scratcher for sure while writing some code making use of MutableArithmetics.jl. An example of unexpected behavior:

```julia
julia> a = Rational{BigInt}(2)
2//1

julia> b = inv(a)
1//2

julia> using MutableArithmetics

julia> zero!!(a)
0//1

julia> b # mutating a also mutates b!
1//0

```

Perhaps `inv` should be changed so as to copy the numerator and denominator, instead of returning references to the input value, for consistency with the other operations? I know this would cause some pain for more basic use-cases, though…

Maybe it would be best to keep the current behavior, but document it somehow?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 6, 2022, 12:28am UTC](https://discourse.julialang.org/t/should-inv-rational-make-copies-of-the-numerator-and-denominator/88314/2 "2022-10-06T00:28:21Z")

</div>

Numbers should behave immutably. If you’re going to mutate them then that’s on you and you should make sure no one can observe you doing it.

---

<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:** [October 6, 2022, 8:28am UTC](https://discourse.julialang.org/t/should-inv-rational-make-copies-of-the-numerator-and-denominator/88314/3 "2022-10-06T08:28:20Z")

</div>

If I understand you correctly, you’re saying that users of MutableArithmetics should be aware of the implementation of Base and the standard library?

This doesn’t sound so bad until one needs to upgrade to a new Julia release and some of the implementation of Julia changes, requiring changes to my code… If I got this right, it basically means that MutableArithmetics is only good for one-off experimentation, which would be quite a shame for applications that need the performance offered by MutableArithmetics.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [October 6, 2022, 11:30am UTC](https://discourse.julialang.org/t/should-inv-rational-make-copies-of-the-numerator-and-denominator/88314/4 "2022-10-06T11:30:02Z")

</div>

Someone who really wants the performance of MutableArithmetics should be grateful `inv(::Rational)` doesn’t allocate by default.
