# Inverse fma

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

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

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)`.

1 Like

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.

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

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

1 Like