?

Average Accuracy: 67.5% → 92.8%
Time: 9.7s
Precision: binary64
Cost: 8004

?

\[\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 := \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} \]
(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)) (+ (* x x) (* y y)))))
   (if (<= t_0 2.0) t_0 (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)) / ((x * x) + (y * y));
	double tmp;
	if (t_0 <= 2.0) {
		tmp = t_0;
	} 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(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y)))
	tmp = 0.0
	if (t_0 <= 2.0)
		tmp = t_0;
	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[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 2.0], t$95$0, 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 := \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}

Error?

Target

Original67.5%
Target99.9%
Herbie92.8%
\[\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 99.9%

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

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

    1. Initial program 0.0%

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Taylor expanded in x around 0 52.7%

      \[\leadsto \color{blue}{2 \cdot \frac{{x}^{2}}{{y}^{2}} - 1} \]
    3. Simplified77.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(2, \frac{x}{y} \cdot \frac{x}{y}, -1\right)} \]
      Proof

      [Start]52.7

      \[ 2 \cdot \frac{{x}^{2}}{{y}^{2}} - 1 \]

      fma-neg [=>]52.7

      \[ \color{blue}{\mathsf{fma}\left(2, \frac{{x}^{2}}{{y}^{2}}, -1\right)} \]

      unpow2 [=>]52.7

      \[ \mathsf{fma}\left(2, \frac{\color{blue}{x \cdot x}}{{y}^{2}}, -1\right) \]

      unpow2 [=>]52.7

      \[ \mathsf{fma}\left(2, \frac{x \cdot x}{\color{blue}{y \cdot y}}, -1\right) \]

      times-frac [=>]77.8

      \[ \mathsf{fma}\left(2, \color{blue}{\frac{x}{y} \cdot \frac{x}{y}}, -1\right) \]

      metadata-eval [=>]77.8

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

    \[\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)}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(2, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy92.6%
Cost1988
\[\begin{array}{l} t_0 := \frac{x + y}{y}\\ t_1 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{if}\;t_1 \leq 2:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{y} \cdot t_0 - t_0\\ \end{array} \]
Alternative 2
Accuracy91.2%
Cost1357
\[\begin{array}{l} \mathbf{if}\;y \leq -7 \cdot 10^{-41}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -1.35 \cdot 10^{-161} \lor \neg \left(y \leq 1.55 \cdot 10^{-162}\right):\\ \;\;\;\;\left(x - y\right) \cdot \frac{x + y}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{x} + \left(1 - \frac{y}{x}\right)\\ \end{array} \]
Alternative 3
Accuracy82.7%
Cost1028
\[\begin{array}{l} \mathbf{if}\;y \leq -2.8 \cdot 10^{-132}:\\ \;\;\;\;\frac{x + y}{\frac{-x}{\frac{y}{x}} - \left(x + y\right)}\\ \mathbf{elif}\;y \leq 8.4 \cdot 10^{-140}:\\ \;\;\;\;\frac{y}{x} + \left(1 - \frac{y}{x}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{x}{y} + 1}{\frac{y}{x - y}}\\ \end{array} \]
Alternative 4
Accuracy82.7%
Cost969
\[\begin{array}{l} \mathbf{if}\;y \leq -3.2 \cdot 10^{-133} \lor \neg \left(y \leq 2.05 \cdot 10^{-137}\right):\\ \;\;\;\;\frac{\frac{x}{y} + 1}{\frac{y}{x - y}}\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{x} + \left(1 - \frac{y}{x}\right)\\ \end{array} \]
Alternative 5
Accuracy82.7%
Cost968
\[\begin{array}{l} \mathbf{if}\;y \leq -3.3 \cdot 10^{-133}:\\ \;\;\;\;\frac{x}{y} \cdot \frac{x}{y} + -1\\ \mathbf{elif}\;y \leq 8.5 \cdot 10^{-138}:\\ \;\;\;\;\frac{y}{x} + \left(1 - \frac{y}{x}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{x + y}{y} \cdot \frac{x - y}{y}\\ \end{array} \]
Alternative 6
Accuracy82.5%
Cost841
\[\begin{array}{l} \mathbf{if}\;y \leq -2.4 \cdot 10^{-131} \lor \neg \left(y \leq 1.05 \cdot 10^{-137}\right):\\ \;\;\;\;\frac{x}{y} \cdot \frac{x}{y} + -1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 7
Accuracy82.6%
Cost841
\[\begin{array}{l} \mathbf{if}\;y \leq -5.2 \cdot 10^{-129} \lor \neg \left(y \leq 6.5 \cdot 10^{-138}\right):\\ \;\;\;\;\frac{x}{y} \cdot \frac{x}{y} + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{y}{x} + \left(1 - \frac{y}{x}\right)\\ \end{array} \]
Alternative 8
Accuracy82.2%
Cost328
\[\begin{array}{l} \mathbf{if}\;y \leq -2 \cdot 10^{-132}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 10^{-137}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 9
Accuracy66.7%
Cost64
\[-1 \]

Error

Reproduce?

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