?

Average Accuracy: 50.3% → 79.6%
Time: 3.1s
Precision: binary64
Cost: 7504

?

\[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
\[\begin{array}{l} t_0 := y \cdot \left(y \cdot 4\right)\\ t_1 := \mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\ t_2 := \frac{x \cdot x - t_0}{x \cdot x + t_0}\\ \mathbf{if}\;y \leq -2.1 \cdot 10^{+125}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq -1.05 \cdot 10^{-65}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;y \leq 1.35 \cdot 10^{-186}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 8.1 \cdot 10^{+96}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
(FPCore (x y)
 :precision binary64
 (/ (- (* x x) (* (* y 4.0) y)) (+ (* x x) (* (* y 4.0) y))))
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (* y (* y 4.0)))
        (t_1 (fma 0.5 (* (/ x y) (/ x y)) -1.0))
        (t_2 (/ (- (* x x) t_0) (+ (* x x) t_0))))
   (if (<= y -2.1e+125)
     t_1
     (if (<= y -1.05e-65)
       t_2
       (if (<= y 1.35e-186) 1.0 (if (<= y 8.1e+96) t_2 t_1))))))
double code(double x, double y) {
	return ((x * x) - ((y * 4.0) * y)) / ((x * x) + ((y * 4.0) * y));
}
double code(double x, double y) {
	double t_0 = y * (y * 4.0);
	double t_1 = fma(0.5, ((x / y) * (x / y)), -1.0);
	double t_2 = ((x * x) - t_0) / ((x * x) + t_0);
	double tmp;
	if (y <= -2.1e+125) {
		tmp = t_1;
	} else if (y <= -1.05e-65) {
		tmp = t_2;
	} else if (y <= 1.35e-186) {
		tmp = 1.0;
	} else if (y <= 8.1e+96) {
		tmp = t_2;
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(x, y)
	return Float64(Float64(Float64(x * x) - Float64(Float64(y * 4.0) * y)) / Float64(Float64(x * x) + Float64(Float64(y * 4.0) * y)))
end
function code(x, y)
	t_0 = Float64(y * Float64(y * 4.0))
	t_1 = fma(0.5, Float64(Float64(x / y) * Float64(x / y)), -1.0)
	t_2 = Float64(Float64(Float64(x * x) - t_0) / Float64(Float64(x * x) + t_0))
	tmp = 0.0
	if (y <= -2.1e+125)
		tmp = t_1;
	elseif (y <= -1.05e-65)
		tmp = t_2;
	elseif (y <= 1.35e-186)
		tmp = 1.0;
	elseif (y <= 8.1e+96)
		tmp = t_2;
	else
		tmp = t_1;
	end
	return tmp
end
code[x_, y_] := N[(N[(N[(x * x), $MachinePrecision] - N[(N[(y * 4.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(N[(y * 4.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_, y_] := Block[{t$95$0 = N[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]}, Block[{t$95$2 = N[(N[(N[(x * x), $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -2.1e+125], t$95$1, If[LessEqual[y, -1.05e-65], t$95$2, If[LessEqual[y, 1.35e-186], 1.0, If[LessEqual[y, 8.1e+96], t$95$2, t$95$1]]]]]]]
\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}
\begin{array}{l}
t_0 := y \cdot \left(y \cdot 4\right)\\
t_1 := \mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\
t_2 := \frac{x \cdot x - t_0}{x \cdot x + t_0}\\
\mathbf{if}\;y \leq -2.1 \cdot 10^{+125}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;y \leq -1.05 \cdot 10^{-65}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;y \leq 1.35 \cdot 10^{-186}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \leq 8.1 \cdot 10^{+96}:\\
\;\;\;\;t_2\\

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


\end{array}

Error?

Target

Original50.3%
Target50.8%
Herbie79.6%
\[\begin{array}{l} \mathbf{if}\;\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} < 0.9743233849626781:\\ \;\;\;\;\frac{x \cdot x}{x \cdot x + \left(y \cdot y\right) \cdot 4} - \frac{\left(y \cdot y\right) \cdot 4}{x \cdot x + \left(y \cdot y\right) \cdot 4}\\ \mathbf{else}:\\ \;\;\;\;{\left(\frac{x}{\sqrt{x \cdot x + \left(y \cdot y\right) \cdot 4}}\right)}^{2} - \frac{\left(y \cdot y\right) \cdot 4}{x \cdot x + \left(y \cdot y\right) \cdot 4}\\ \end{array} \]

Derivation?

  1. Split input into 3 regimes
  2. if y < -2.1000000000000001e125 or 8.1000000000000002e96 < y

    1. Initial program 17.0%

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

      \[\leadsto \color{blue}{0.5 \cdot \frac{{x}^{2}}{{y}^{2}} - 1} \]
    3. Simplified83.4%

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

      [Start]73.0

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

      fma-neg [=>]73.0

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

      unpow2 [=>]73.0

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

      unpow2 [=>]73.0

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

      times-frac [=>]83.4

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

      metadata-eval [=>]83.4

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

    if -2.1000000000000001e125 < y < -1.05000000000000001e-65 or 1.3499999999999999e-186 < y < 8.1000000000000002e96

    1. Initial program 74.1%

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

    if -1.05000000000000001e-65 < y < 1.3499999999999999e-186

    1. Initial program 55.3%

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

      \[\leadsto \color{blue}{1} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification79.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -2.1 \cdot 10^{+125}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\ \mathbf{elif}\;y \leq -1.05 \cdot 10^{-65}:\\ \;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{elif}\;y \leq 1.35 \cdot 10^{-186}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 8.1 \cdot 10^{+96}:\\ \;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy79.2%
Cost1744
\[\begin{array}{l} t_0 := y \cdot \left(y \cdot 4\right)\\ t_1 := \frac{x \cdot x - t_0}{x \cdot x + t_0}\\ \mathbf{if}\;y \leq -4.9 \cdot 10^{+126}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -1.55 \cdot 10^{-65}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 1.35 \cdot 10^{-186}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 2.85 \cdot 10^{+94}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 2
Accuracy72.8%
Cost328
\[\begin{array}{l} \mathbf{if}\;y \leq -1 \cdot 10^{-11}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 8 \cdot 10^{-90}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
Alternative 3
Accuracy49.9%
Cost64
\[-1 \]

Error

Reproduce?

herbie shell --seed 2023147 
(FPCore (x y)
  :name "Diagrams.TwoD.Arc:arcBetween from diagrams-lib-1.3.0.3"
  :precision binary64

  :herbie-target
  (if (< (/ (- (* x x) (* (* y 4.0) y)) (+ (* x x) (* (* y 4.0) y))) 0.9743233849626781) (- (/ (* x x) (+ (* x x) (* (* y y) 4.0))) (/ (* (* y y) 4.0) (+ (* x x) (* (* y y) 4.0)))) (- (pow (/ x (sqrt (+ (* x x) (* (* y y) 4.0)))) 2.0) (/ (* (* y y) 4.0) (+ (* x x) (* (* y y) 4.0)))))

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