Average Error: 14.9 → 0.6
Time: 1.5s
Precision: binary64
\[\frac{\left(x \cdot 2\right) \cdot y}{x - y} \]
\[\begin{array}{l} t_0 := \frac{\left(x \cdot 2\right) \cdot y}{x - y}\\ t_1 := \frac{x}{\mathsf{fma}\left(0.5, \frac{x}{y}, -0.5\right)}\\ \mathbf{if}\;t_0 \leq -208120357.65945:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq -1.0684883610804224 \cdot 10^{-303}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 0:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 5.448508932801663 \cdot 10^{-60}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
(FPCore (x y) :precision binary64 (/ (* (* x 2.0) y) (- x y)))
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ (* (* x 2.0) y) (- x y))) (t_1 (/ x (fma 0.5 (/ x y) -0.5))))
   (if (<= t_0 -208120357.65945)
     t_1
     (if (<= t_0 -1.0684883610804224e-303)
       t_0
       (if (<= t_0 0.0) t_1 (if (<= t_0 5.448508932801663e-60) t_0 t_1))))))
double code(double x, double y) {
	return ((x * 2.0) * y) / (x - y);
}
double code(double x, double y) {
	double t_0 = ((x * 2.0) * y) / (x - y);
	double t_1 = x / fma(0.5, (x / y), -0.5);
	double tmp;
	if (t_0 <= -208120357.65945) {
		tmp = t_1;
	} else if (t_0 <= -1.0684883610804224e-303) {
		tmp = t_0;
	} else if (t_0 <= 0.0) {
		tmp = t_1;
	} else if (t_0 <= 5.448508932801663e-60) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(x, y)
	return Float64(Float64(Float64(x * 2.0) * y) / Float64(x - y))
end
function code(x, y)
	t_0 = Float64(Float64(Float64(x * 2.0) * y) / Float64(x - y))
	t_1 = Float64(x / fma(0.5, Float64(x / y), -0.5))
	tmp = 0.0
	if (t_0 <= -208120357.65945)
		tmp = t_1;
	elseif (t_0 <= -1.0684883610804224e-303)
		tmp = t_0;
	elseif (t_0 <= 0.0)
		tmp = t_1;
	elseif (t_0 <= 5.448508932801663e-60)
		tmp = t_0;
	else
		tmp = t_1;
	end
	return tmp
end
code[x_, y_] := N[(N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
code[x_, y_] := Block[{t$95$0 = N[(N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(x / N[(0.5 * N[(x / y), $MachinePrecision] + -0.5), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -208120357.65945], t$95$1, If[LessEqual[t$95$0, -1.0684883610804224e-303], t$95$0, If[LessEqual[t$95$0, 0.0], t$95$1, If[LessEqual[t$95$0, 5.448508932801663e-60], t$95$0, t$95$1]]]]]]
\frac{\left(x \cdot 2\right) \cdot y}{x - y}
\begin{array}{l}
t_0 := \frac{\left(x \cdot 2\right) \cdot y}{x - y}\\
t_1 := \frac{x}{\mathsf{fma}\left(0.5, \frac{x}{y}, -0.5\right)}\\
\mathbf{if}\;t_0 \leq -208120357.65945:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_0 \leq -1.0684883610804224 \cdot 10^{-303}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_0 \leq 5.448508932801663 \cdot 10^{-60}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original14.9
Target0.3
Herbie0.6
\[\begin{array}{l} \mathbf{if}\;x < -1.7210442634149447 \cdot 10^{+81}:\\ \;\;\;\;\frac{2 \cdot x}{x - y} \cdot y\\ \mathbf{elif}\;x < 83645045635564430:\\ \;\;\;\;\frac{x \cdot 2}{\frac{x - y}{y}}\\ \mathbf{else}:\\ \;\;\;\;\frac{2 \cdot x}{x - y} \cdot y\\ \end{array} \]

Derivation

  1. Split input into 2 regimes
  2. if (/.f64 (*.f64 (*.f64 x 2) y) (-.f64 x y)) < -208120357.65944999 or -1.06848836108042239e-303 < (/.f64 (*.f64 (*.f64 x 2) y) (-.f64 x y)) < -0.0 or 5.4485089328016631e-60 < (/.f64 (*.f64 (*.f64 x 2) y) (-.f64 x y))

    1. Initial program 35.9

      \[\frac{\left(x \cdot 2\right) \cdot y}{x - y} \]
    2. Simplified0.6

      \[\leadsto \color{blue}{\frac{x}{\mathsf{fma}\left(0.5, \frac{x}{y}, -0.5\right)}} \]

    if -208120357.65944999 < (/.f64 (*.f64 (*.f64 x 2) y) (-.f64 x y)) < -1.06848836108042239e-303 or -0.0 < (/.f64 (*.f64 (*.f64 x 2) y) (-.f64 x y)) < 5.4485089328016631e-60

    1. Initial program 0.6

      \[\frac{\left(x \cdot 2\right) \cdot y}{x - y} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{\left(x \cdot 2\right) \cdot y}{x - y} \leq -208120357.65945:\\ \;\;\;\;\frac{x}{\mathsf{fma}\left(0.5, \frac{x}{y}, -0.5\right)}\\ \mathbf{elif}\;\frac{\left(x \cdot 2\right) \cdot y}{x - y} \leq -1.0684883610804224 \cdot 10^{-303}:\\ \;\;\;\;\frac{\left(x \cdot 2\right) \cdot y}{x - y}\\ \mathbf{elif}\;\frac{\left(x \cdot 2\right) \cdot y}{x - y} \leq 0:\\ \;\;\;\;\frac{x}{\mathsf{fma}\left(0.5, \frac{x}{y}, -0.5\right)}\\ \mathbf{elif}\;\frac{\left(x \cdot 2\right) \cdot y}{x - y} \leq 5.448508932801663 \cdot 10^{-60}:\\ \;\;\;\;\frac{\left(x \cdot 2\right) \cdot y}{x - y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{\mathsf{fma}\left(0.5, \frac{x}{y}, -0.5\right)}\\ \end{array} \]

Reproduce

herbie shell --seed 2022150 
(FPCore (x y)
  :name "Linear.Projection:perspective from linear-1.19.1.3, B"
  :precision binary64

  :herbie-target
  (if (< x -1.7210442634149447e+81) (* (/ (* 2.0 x) (- x y)) y) (if (< x 83645045635564430.0) (/ (* x 2.0) (/ (- x y) y)) (* (/ (* 2.0 x) (- x y)) y)))

  (/ (* (* x 2.0) y) (- x y)))