Average Error: 14.4 → 1.4
Time: 2.6s
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}\\ \mathbf{if}\;t_0 \leq -2 \cdot 10^{-40}:\\ \;\;\;\;\frac{-x}{0.5 + \frac{x}{y} \cdot -0.5}\\ \mathbf{elif}\;t_0 \leq -5 \cdot 10^{-301}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_0 \leq 1.1 \cdot 10^{-265}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{x}{0.5 \cdot \frac{x}{y} + -0.5}\right)\right)\\ \mathbf{elif}\;t_0 \leq 2 \cdot 10^{-12}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;x \cdot \frac{1}{\mathsf{fma}\left(0.5, \frac{x}{y}, -0.5\right)}\\ \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))))
   (if (<= t_0 -2e-40)
     (/ (- x) (+ 0.5 (* (/ x y) -0.5)))
     (if (<= t_0 -5e-301)
       t_0
       (if (<= t_0 1.1e-265)
         (expm1 (log1p (/ x (+ (* 0.5 (/ x y)) -0.5))))
         (if (<= t_0 2e-12) t_0 (* x (/ 1.0 (fma 0.5 (/ x y) -0.5)))))))))
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 tmp;
	if (t_0 <= -2e-40) {
		tmp = -x / (0.5 + ((x / y) * -0.5));
	} else if (t_0 <= -5e-301) {
		tmp = t_0;
	} else if (t_0 <= 1.1e-265) {
		tmp = expm1(log1p((x / ((0.5 * (x / y)) + -0.5))));
	} else if (t_0 <= 2e-12) {
		tmp = t_0;
	} else {
		tmp = x * (1.0 / fma(0.5, (x / y), -0.5));
	}
	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))
	tmp = 0.0
	if (t_0 <= -2e-40)
		tmp = Float64(Float64(-x) / Float64(0.5 + Float64(Float64(x / y) * -0.5)));
	elseif (t_0 <= -5e-301)
		tmp = t_0;
	elseif (t_0 <= 1.1e-265)
		tmp = expm1(log1p(Float64(x / Float64(Float64(0.5 * Float64(x / y)) + -0.5))));
	elseif (t_0 <= 2e-12)
		tmp = t_0;
	else
		tmp = Float64(x * Float64(1.0 / fma(0.5, Float64(x / y), -0.5)));
	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]}, If[LessEqual[t$95$0, -2e-40], N[((-x) / N[(0.5 + N[(N[(x / y), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, -5e-301], t$95$0, If[LessEqual[t$95$0, 1.1e-265], N[(Exp[N[Log[1 + N[(x / N[(N[(0.5 * N[(x / y), $MachinePrecision]), $MachinePrecision] + -0.5), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]] - 1), $MachinePrecision], If[LessEqual[t$95$0, 2e-12], t$95$0, N[(x * N[(1.0 / N[(0.5 * N[(x / y), $MachinePrecision] + -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]
\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}\\
\mathbf{if}\;t_0 \leq -2 \cdot 10^{-40}:\\
\;\;\;\;\frac{-x}{0.5 + \frac{x}{y} \cdot -0.5}\\

\mathbf{elif}\;t_0 \leq -5 \cdot 10^{-301}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;t_0 \leq 1.1 \cdot 10^{-265}:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{x}{0.5 \cdot \frac{x}{y} + -0.5}\right)\right)\\

\mathbf{elif}\;t_0 \leq 2 \cdot 10^{-12}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;x \cdot \frac{1}{\mathsf{fma}\left(0.5, \frac{x}{y}, -0.5\right)}\\


\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original14.4
Target0.4
Herbie1.4
\[\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 4 regimes
  2. if (/.f64 (*.f64 (*.f64 x 2) y) (-.f64 x y)) < -1.9999999999999999e-40

    1. Initial program 24.0

      \[\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)}} \]
    3. Applied egg-rr10.0

      \[\leadsto \frac{x}{\color{blue}{\frac{\frac{0.5 \cdot x}{y} \cdot \frac{0.5 \cdot x}{y} - 0.25}{\frac{0.5 \cdot x}{y} - -0.5}}} \]
    4. Applied egg-rr0.6

      \[\leadsto \color{blue}{-\frac{x}{-\left(0.5 \cdot \frac{x}{y} + -0.5\right)}} \]

    if -1.9999999999999999e-40 < (/.f64 (*.f64 (*.f64 x 2) y) (-.f64 x y)) < -5.00000000000000013e-301 or 1.10000000000000005e-265 < (/.f64 (*.f64 (*.f64 x 2) y) (-.f64 x y)) < 1.99999999999999996e-12

    1. Initial program 0.6

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

    if -5.00000000000000013e-301 < (/.f64 (*.f64 (*.f64 x 2) y) (-.f64 x y)) < 1.10000000000000005e-265

    1. Initial program 42.6

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

      \[\leadsto \color{blue}{\frac{x}{\mathsf{fma}\left(0.5, \frac{x}{y}, -0.5\right)}} \]
    3. Applied egg-rr12.6

      \[\leadsto \frac{x}{\color{blue}{\frac{\frac{0.5 \cdot x}{y} \cdot \frac{0.5 \cdot x}{y} - 0.25}{\frac{0.5 \cdot x}{y} - -0.5}}} \]
    4. Applied egg-rr6.4

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{x}{0.5 \cdot \frac{x}{y} + -0.5}\right)\right)} \]

    if 1.99999999999999996e-12 < (/.f64 (*.f64 (*.f64 x 2) y) (-.f64 x y))

    1. Initial program 28.3

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

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

      \[\leadsto \color{blue}{x \cdot \frac{1}{\mathsf{fma}\left(0.5, \frac{x}{y}, -0.5\right)}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification1.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{\left(x \cdot 2\right) \cdot y}{x - y} \leq -2 \cdot 10^{-40}:\\ \;\;\;\;\frac{-x}{0.5 + \frac{x}{y} \cdot -0.5}\\ \mathbf{elif}\;\frac{\left(x \cdot 2\right) \cdot y}{x - y} \leq -5 \cdot 10^{-301}:\\ \;\;\;\;\frac{\left(x \cdot 2\right) \cdot y}{x - y}\\ \mathbf{elif}\;\frac{\left(x \cdot 2\right) \cdot y}{x - y} \leq 1.1 \cdot 10^{-265}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{x}{0.5 \cdot \frac{x}{y} + -0.5}\right)\right)\\ \mathbf{elif}\;\frac{\left(x \cdot 2\right) \cdot y}{x - y} \leq 2 \cdot 10^{-12}:\\ \;\;\;\;\frac{\left(x \cdot 2\right) \cdot y}{x - y}\\ \mathbf{else}:\\ \;\;\;\;x \cdot \frac{1}{\mathsf{fma}\left(0.5, \frac{x}{y}, -0.5\right)}\\ \end{array} \]

Reproduce

herbie shell --seed 2022162 
(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)))