# Inverse fma

**URL:** https://discourse.julialang.org/t/inverse-fma/11812
**Category:** General Usage
**Tags:** question
**Created:** [June 20, 2018, 9:10am UTC](https://discourse.julialang.org/t/inverse-fma/11812 "2018-06-20T09:10:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 20, 2018, 9:10am UTC](https://discourse.julialang.org/t/inverse-fma/11812/1 "2018-06-20T09:10:20Z")

</div>

Is there an inverse of `fma`, in the sense of

```julia
y = fma(x, A, B)
x2 = ???(y, A, B) # this is what I am looking for
x ≈ x2 # "reasonably" close

```

I found that sometimes either `(y - B)/A` or `fma(y, inv(A), -B/A)` is more accurate, but it really depends on the magnitude of `B` vs `A`.

---

<div class="post-metadata">

### Author: ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)
#### Post date: [June 20, 2018, 9:57am UTC](https://discourse.julialang.org/t/inverse-fma/11812/2 "2018-06-20T09:57:32Z")

</div>

You should not define “accuracy” based on the difference between `x` and `x2`. If `x*A` is smaller than `B`, then there will be many values of `x` that map to the same `y` and your inverse function cannot possibly know which one is your original `x`. Better to compare `fma(x, A, B)` to `fma(x2, A, B)`.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 20, 2018, 10:53am UTC](https://discourse.julialang.org/t/inverse-fma/11812/3 "2018-06-20T10:53:56Z")

</div>

Good point, but I would still like a `(y - B)/A` that is as accurate as it can be, given `B` and `A` and recognizing that it can’t be a bijection.

Of course I can always go to `BigFloat` and back, but that is rather slow.

---

<div class="post-metadata">

### Author: ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)
#### Post date: [June 20, 2018, 11:20am UTC](https://discourse.julialang.org/t/inverse-fma/11812/4 "2018-06-20T11:20:47Z")

</div>

```julia
function invfma(y, A, B) 
       x2 = (y-B)/A
       y2 = fma(x2, A, B)
       x2 + (y-y2)/A
end

```

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [June 20, 2018, 11:25am UTC](https://discourse.julialang.org/t/inverse-fma/11812/5 "2018-06-20T11:25:18Z")

</div>

Another alternative to `BigFloat` is `Base.TwicePrecision`.
