Average Error: 20.9 → 4.9
Time: 9.5s
Precision: binary64
Cost: 8260
\[\left(0 < x \land x < 1\right) \land y < 1\]
\[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
\[\begin{array}{l} t_0 := \left(x - y\right) \cdot \left(x + y\right)\\ \mathbf{if}\;\frac{t_0}{x \cdot x + y \cdot y} \leq 2:\\ \;\;\;\;\frac{t_0}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(2, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\ \end{array} \]
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (* (- x y) (+ x y))))
   (if (<= (/ t_0 (+ (* x x) (* y y))) 2.0)
     (/ t_0 (fma x x (* y y)))
     (fma 2.0 (* (/ x y) (/ x y)) -1.0))))
double code(double x, double y) {
	return ((x - y) * (x + y)) / ((x * x) + (y * y));
}
double code(double x, double y) {
	double t_0 = (x - y) * (x + y);
	double tmp;
	if ((t_0 / ((x * x) + (y * y))) <= 2.0) {
		tmp = t_0 / fma(x, x, (y * y));
	} else {
		tmp = fma(2.0, ((x / y) * (x / y)), -1.0);
	}
	return tmp;
}
function code(x, y)
	return Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y)))
end
function code(x, y)
	t_0 = Float64(Float64(x - y) * Float64(x + y))
	tmp = 0.0
	if (Float64(t_0 / Float64(Float64(x * x) + Float64(y * y))) <= 2.0)
		tmp = Float64(t_0 / fma(x, x, Float64(y * y)));
	else
		tmp = fma(2.0, Float64(Float64(x / y) * Float64(x / y)), -1.0);
	end
	return tmp
end
code[x_, y_] := N[(N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_, y_] := Block[{t$95$0 = N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(t$95$0 / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2.0], N[(t$95$0 / N[(x * x + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(2.0 * N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]]
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\begin{array}{l}
t_0 := \left(x - y\right) \cdot \left(x + y\right)\\
\mathbf{if}\;\frac{t_0}{x \cdot x + y \cdot y} \leq 2:\\
\;\;\;\;\frac{t_0}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\

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


\end{array}

Error

Target

Original20.9
Target0.1
Herbie4.9
\[\begin{array}{l} \mathbf{if}\;0.5 < \left|\frac{x}{y}\right| \land \left|\frac{x}{y}\right| < 2:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;1 - \frac{2}{1 + \frac{x}{y} \cdot \frac{x}{y}}\\ \end{array} \]

Derivation

  1. Split input into 2 regimes
  2. if (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < 2

    1. Initial program 0.0

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

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(x + y\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \]
      Proof
      (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (fma.f64 x x (*.f64 y y))): 0 points increase in error, 0 points decrease in error
      (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (Rewrite<= fma-def_binary64 (+.f64 (*.f64 x x) (*.f64 y y)))): 2 points increase in error, 0 points decrease in error

    if 2 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y)))

    1. Initial program 64.0

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

      \[\leadsto \color{blue}{\left(x - y\right) \cdot \frac{x + y}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \]
      Proof
      (*.f64 (-.f64 x y) (/.f64 (+.f64 x y) (fma.f64 x x (*.f64 y y)))): 0 points increase in error, 0 points decrease in error
      (*.f64 (-.f64 x y) (/.f64 (+.f64 x y) (Rewrite<= fma-def_binary64 (+.f64 (*.f64 x x) (*.f64 y y))))): 0 points increase in error, 0 points decrease in error
      (Rewrite=> associate-*r/_binary64 (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y)))): 38 points increase in error, 64 points decrease in error
    3. Taylor expanded in x around 0 31.2

      \[\leadsto \color{blue}{2 \cdot \frac{{x}^{2}}{{y}^{2}} - 1} \]
    4. Simplified14.9

      \[\leadsto \color{blue}{\mathsf{fma}\left(2, \frac{x}{y} \cdot \frac{x}{y}, -1\right)} \]
      Proof
      (fma.f64 2 (*.f64 (/.f64 x y) (/.f64 x y)) -1): 0 points increase in error, 0 points decrease in error
      (fma.f64 2 (Rewrite<= times-frac_binary64 (/.f64 (*.f64 x x) (*.f64 y y))) -1): 64 points increase in error, 5 points decrease in error
      (fma.f64 2 (/.f64 (Rewrite<= unpow2_binary64 (pow.f64 x 2)) (*.f64 y y)) -1): 0 points increase in error, 0 points decrease in error
      (fma.f64 2 (/.f64 (pow.f64 x 2) (Rewrite<= unpow2_binary64 (pow.f64 y 2))) -1): 0 points increase in error, 0 points decrease in error
      (fma.f64 2 (/.f64 (pow.f64 x 2) (pow.f64 y 2)) (Rewrite<= metadata-eval (neg.f64 1))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-neg_binary64 (-.f64 (*.f64 2 (/.f64 (pow.f64 x 2) (pow.f64 y 2))) 1)): 0 points increase in error, 0 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification4.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \leq 2:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(2, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\ \end{array} \]

Alternatives

Alternative 1
Error4.9
Cost8004
\[\begin{array}{l} t_0 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{if}\;t_0 \leq 2:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(2, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\ \end{array} \]
Alternative 2
Error5.0
Cost1988
\[\begin{array}{l} t_0 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{if}\;t_0 \leq 2:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{x + y}{y}}{\frac{y}{x - y}}\\ \end{array} \]
Alternative 3
Error11.1
Cost1096
\[\begin{array}{l} \mathbf{if}\;y \leq -1.8 \cdot 10^{-122}:\\ \;\;\;\;\frac{x}{y} \cdot \frac{x}{y} + -1\\ \mathbf{elif}\;y \leq 2.8 \cdot 10^{-137}:\\ \;\;\;\;1 + \frac{y \cdot -2}{x} \cdot \frac{y}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{y \cdot \frac{y}{x - y}}{x + y}}\\ \end{array} \]
Alternative 4
Error11.1
Cost968
\[\begin{array}{l} t_0 := \frac{x}{y} \cdot \frac{x}{y} + -1\\ \mathbf{if}\;y \leq -2.6 \cdot 10^{-123}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 10^{-136}:\\ \;\;\;\;1 + \frac{y \cdot -2}{x} \cdot \frac{y}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error11.6
Cost840
\[\begin{array}{l} t_0 := \frac{x}{y} \cdot \frac{x}{y} + -1\\ \mathbf{if}\;y \leq -2.4 \cdot 10^{-123}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 4.2 \cdot 10^{-137}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error11.5
Cost840
\[\begin{array}{l} t_0 := \frac{x}{y} \cdot \frac{x}{y} + -1\\ \mathbf{if}\;y \leq -2.3 \cdot 10^{-123}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 5.5 \cdot 10^{-137}:\\ \;\;\;\;\frac{y}{x} + \left(1 - \frac{y}{x}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error11.3
Cost840
\[\begin{array}{l} t_0 := \frac{x}{y} \cdot \frac{x}{y} + -1\\ \mathbf{if}\;y \leq -1.6 \cdot 10^{-122}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 7.5 \cdot 10^{-137}:\\ \;\;\;\;1 - \frac{y \cdot \frac{y}{x}}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error11.8
Cost328
\[\begin{array}{l} \mathbf{if}\;y \leq -3.6 \cdot 10^{-123}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 6.6 \cdot 10^{-134}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 9
Error22.0
Cost64
\[-1 \]

Error

Reproduce

herbie shell --seed 2022330 
(FPCore (x y)
  :name "Kahan p9 Example"
  :precision binary64
  :pre (and (and (< 0.0 x) (< x 1.0)) (< y 1.0))

  :herbie-target
  (if (and (< 0.5 (fabs (/ x y))) (< (fabs (/ x y)) 2.0)) (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))) (- 1.0 (/ 2.0 (+ 1.0 (* (/ x y) (/ x y))))))

  (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))