\[\begin{array}{l}
t_1 := x \cdot y - z \cdot t\\
\mathbf{if}\;t_1 \leq -\infty:\\
\;\;\;\;\mathsf{fma}\left(x, \frac{y}{a}, \frac{-z}{\frac{a}{t}}\right)\\
\mathbf{elif}\;t_1 \leq 2 \cdot 10^{+285}:\\
\;\;\;\;\frac{t_1}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{\frac{a}{y}} - \frac{z}{\frac{a}{t}}\\
\end{array}
\]
(FPCore (x y z t a) :precision binary64 (/ (- (* x y) (* z t)) a))
↓
(FPCore (x y z t a)
:precision binary64
(let* ((t_1 (- (* x y) (* z t))))
(if (<= t_1 (- INFINITY))
(fma x (/ y a) (/ (- z) (/ a t)))
(if (<= t_1 2e+285) (/ t_1 a) (- (/ x (/ a y)) (/ z (/ a t)))))))
double code(double x, double y, double z, double t, double a) {
return ((x * y) - (z * t)) / a;
}
↓
double code(double x, double y, double z, double t, double a) {
double t_1 = (x * y) - (z * t);
double tmp;
if (t_1 <= -((double) INFINITY)) {
tmp = fma(x, (y / a), (-z / (a / t)));
} else if (t_1 <= 2e+285) {
tmp = t_1 / a;
} else {
tmp = (x / (a / y)) - (z / (a / t));
}
return tmp;
}
function code(x, y, z, t, a)
return Float64(Float64(Float64(x * y) - Float64(z * t)) / a)
end
↓
function code(x, y, z, t, a)
t_1 = Float64(Float64(x * y) - Float64(z * t))
tmp = 0.0
if (t_1 <= Float64(-Inf))
tmp = fma(x, Float64(y / a), Float64(Float64(-z) / Float64(a / t)));
elseif (t_1 <= 2e+285)
tmp = Float64(t_1 / a);
else
tmp = Float64(Float64(x / Float64(a / y)) - Float64(z / Float64(a / t)));
end
return tmp
end
The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.
Herbie found 10 alternatives:
Alternative
Accuracy
Speedup
Accuracy vs Speed
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.
\[\leadsto \begin{array}{l}
\mathbf{if}\;x \cdot y - z \cdot t \leq -\infty:\\
\;\;\;\;\mathsf{fma}\left(x, \frac{y}{a}, \frac{-z}{\frac{a}{t}}\right)\\
\mathbf{elif}\;x \cdot y - z \cdot t \leq 2 \cdot 10^{+285}:\\
\;\;\;\;\frac{x \cdot y - z \cdot t}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{\frac{a}{y}} - \frac{z}{\frac{a}{t}}\\
\end{array}
\]
Alternatives
Alternative 1
Accuracy
97.3%
Cost
7556
\[\begin{array}{l}
t_1 := x \cdot y - z \cdot t\\
\mathbf{if}\;t_1 \leq -\infty:\\
\;\;\;\;\mathsf{fma}\left(x, \frac{y}{a}, \frac{-z}{\frac{a}{t}}\right)\\
\mathbf{elif}\;t_1 \leq 2 \cdot 10^{+285}:\\
\;\;\;\;\frac{t_1}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{\frac{a}{y}} - \frac{z}{\frac{a}{t}}\\
\end{array}
\]
Alternative 2
Accuracy
97.2%
Cost
7620
\[\begin{array}{l}
t_1 := x \cdot y - z \cdot t\\
\mathbf{if}\;t_1 \leq -\infty:\\
\;\;\;\;\mathsf{fma}\left(-1, \frac{t}{\frac{a}{z}}, \frac{y}{\frac{a}{x}}\right)\\
\mathbf{elif}\;t_1 \leq 2 \cdot 10^{+285}:\\
\;\;\;\;\frac{t_1}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{\frac{a}{y}} - \frac{z}{\frac{a}{t}}\\
\end{array}
\]
Alternative 3
Accuracy
97.2%
Cost
1737
\[\begin{array}{l}
t_1 := x \cdot y - z \cdot t\\
\mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 2 \cdot 10^{+285}\right):\\
\;\;\;\;\frac{x}{\frac{a}{y}} - \frac{z}{\frac{a}{t}}\\
\mathbf{else}:\\
\;\;\;\;\frac{t_1}{a}\\
\end{array}
\]
herbie shell --seed 2023263
(FPCore (x y z t a)
:name "Data.Colour.Matrix:inverse from colour-2.3.3, B"
:precision binary64
:herbie-target
(if (< z -2.468684968699548e+170) (- (* (/ y a) x) (* (/ t a) z)) (if (< z 6.309831121978371e-71) (/ (- (* x y) (* z t)) a) (- (* (/ y a) x) (* (/ t a) z))))
(/ (- (* x y) (* z t)) a))