?

Average Error: 20.2 → 5.0
Time: 2.0min
Precision: binary64
Cost: 7628

?

\[\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} \mathbf{if}\;y \leq -2 \cdot 10^{+155}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -3.3 \cdot 10^{-152}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{elif}\;y \leq 6.4 \cdot 10^{-165}:\\ \;\;\;\;1 + \frac{\frac{-2 \cdot y}{x} \cdot y}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot x - y \cdot y}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \end{array} \]
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
(FPCore (x y)
 :precision binary64
 (if (<= y -2e+155)
   -1.0
   (if (<= y -3.3e-152)
     (/ (* (- x y) (+ x y)) (+ (* x x) (* y y)))
     (if (<= y 6.4e-165)
       (+ 1.0 (/ (* (/ (* -2.0 y) x) y) x))
       (/ (- (* x x) (* y y)) (fma x x (* y y)))))))
double code(double x, double y) {
	return ((x - y) * (x + y)) / ((x * x) + (y * y));
}
double code(double x, double y) {
	double tmp;
	if (y <= -2e+155) {
		tmp = -1.0;
	} else if (y <= -3.3e-152) {
		tmp = ((x - y) * (x + y)) / ((x * x) + (y * y));
	} else if (y <= 6.4e-165) {
		tmp = 1.0 + ((((-2.0 * y) / x) * y) / x);
	} else {
		tmp = ((x * x) - (y * y)) / fma(x, x, (y * y));
	}
	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)
	tmp = 0.0
	if (y <= -2e+155)
		tmp = -1.0;
	elseif (y <= -3.3e-152)
		tmp = Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y)));
	elseif (y <= 6.4e-165)
		tmp = Float64(1.0 + Float64(Float64(Float64(Float64(-2.0 * y) / x) * y) / x));
	else
		tmp = Float64(Float64(Float64(x * x) - Float64(y * y)) / fma(x, x, Float64(y * y)));
	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_] := If[LessEqual[y, -2e+155], -1.0, If[LessEqual[y, -3.3e-152], N[(N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 6.4e-165], N[(1.0 + N[(N[(N[(N[(-2.0 * y), $MachinePrecision] / x), $MachinePrecision] * y), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision], N[(N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision] / N[(x * x + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\begin{array}{l}
\mathbf{if}\;y \leq -2 \cdot 10^{+155}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \leq -3.3 \cdot 10^{-152}:\\
\;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\

\mathbf{elif}\;y \leq 6.4 \cdot 10^{-165}:\\
\;\;\;\;1 + \frac{\frac{-2 \cdot y}{x} \cdot y}{x}\\

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


\end{array}

Error?

Target

Original20.2
Target0.0
Herbie5.0
\[\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 4 regimes
  2. if y < -2.00000000000000001e155

    1. Initial program 64.0

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

      \[\leadsto \color{blue}{-1} \]

    if -2.00000000000000001e155 < y < -3.29999999999999998e-152

    1. Initial program 0.2

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

    if -3.29999999999999998e-152 < y < 6.40000000000000026e-165

    1. Initial program 29.4

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

      \[\leadsto \color{blue}{1 + \left(-2 \cdot \frac{{y}^{2}}{{x}^{2}} + \left(-1 \cdot \frac{x}{{x}^{2}} + \frac{x}{{x}^{2}}\right) \cdot y\right)} \]
    3. Simplified30.4

      \[\leadsto \color{blue}{1 + \frac{-2 \cdot {y}^{2}}{{x}^{2}}} \]
      Proof
    4. Taylor expanded in y around 0 30.4

      \[\leadsto 1 + \color{blue}{-2 \cdot \frac{{y}^{2}}{{x}^{2}}} \]
    5. Simplified16.2

      \[\leadsto 1 + \color{blue}{\frac{\frac{\left(-2 \cdot y\right) \cdot y}{x}}{x}} \]
      Proof
    6. Applied egg-rr15.1

      \[\leadsto 1 + \frac{\color{blue}{\frac{-2 \cdot y}{x} \cdot y}}{x} \]

    if 6.40000000000000026e-165 < y

    1. Initial program 0.3

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \]
    2. Applied egg-rr0.3

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

      \[\leadsto \color{blue}{\sqrt[3]{\frac{{x}^{2} - {y}^{2}}{\mathsf{fma}\left(x, x, {y}^{2}\right)}} \cdot \sqrt[3]{{\left(\frac{{y}^{2} - {x}^{2}}{\mathsf{fma}\left(x, x, {y}^{2}\right)}\right)}^{2}}} \]
    4. Simplified0.3

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

Alternatives

Alternative 1
Error5.0
Cost1356
\[\begin{array}{l} t_0 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{if}\;y \leq -2 \cdot 10^{+155}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -3.3 \cdot 10^{-152}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 6.4 \cdot 10^{-165}:\\ \;\;\;\;1 + \frac{\frac{-2 \cdot y}{x} \cdot y}{x}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error11.3
Cost1232
\[\begin{array}{l} t_0 := 1 + \frac{\frac{-2 \cdot y}{x} \cdot y}{x}\\ \mathbf{if}\;y \leq -7.8 \cdot 10^{-87}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -4.8 \cdot 10^{-137}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq -4.8 \cdot 10^{-151}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 6.8 \cdot 10^{-170}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 3
Error10.4
Cost968
\[\begin{array}{l} \mathbf{if}\;y \leq -1.6 \cdot 10^{-151}:\\ \;\;\;\;\frac{2 \cdot \left(x \cdot x\right)}{y \cdot y} - 1\\ \mathbf{elif}\;y \leq 3.1 \cdot 10^{-170}:\\ \;\;\;\;1 + \frac{\frac{-2 \cdot y}{x} \cdot y}{x}\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 4
Error10.2
Cost968
\[\begin{array}{l} \mathbf{if}\;y \leq -1.55 \cdot 10^{-151}:\\ \;\;\;\;\frac{2 \cdot \left(x \cdot x\right)}{y \cdot y} - 1\\ \mathbf{elif}\;y \leq 5.5 \cdot 10^{-170}:\\ \;\;\;\;1 + \frac{\frac{-2 \cdot y}{x} \cdot y}{x}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{x}{y \cdot 0.5} \cdot x}{y} - 1\\ \end{array} \]
Alternative 5
Error10.9
Cost840
\[\begin{array}{l} \mathbf{if}\;y \leq -5.6 \cdot 10^{-152}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 6.5 \cdot 10^{-170}:\\ \;\;\;\;\frac{x + y}{x} - \frac{y}{x}\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 6
Error11.0
Cost328
\[\begin{array}{l} \mathbf{if}\;y \leq -3.5 \cdot 10^{-151}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 2.5 \cdot 10^{-170}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 7
Error21.4
Cost64
\[-1 \]

Error

Reproduce?

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