Average Error: 32.0 → 13.2
Time: 2.2s
Precision: binary64
\[\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 := \left(y \cdot y\right) \cdot 4\\ t_1 := \mathsf{fma}\left(x, x, t_0\right)\\ \mathbf{if}\;y \leq -2.90709829270765 \cdot 10^{+109}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -3.738119627219816 \cdot 10^{-161}:\\ \;\;\;\;\frac{x \cdot x}{t_1} - \frac{t_0}{t_1}\\ \mathbf{elif}\;y \leq 3.0001361978417216 \cdot 10^{-145}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 174160338219.57495:\\ \;\;\;\;\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 4.8959736638846305 \cdot 10^{+33}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-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 x x t_0)))
   (if (<= y -2.90709829270765e+109)
     -1.0
     (if (<= y -3.738119627219816e-161)
       (- (/ (* x x) t_1) (/ t_0 t_1))
       (if (<= y 3.0001361978417216e-145)
         1.0
         (if (<= y 174160338219.57495)
           (/ (+ (* x x) (* y (* y -4.0))) (+ (* x x) (* y (* y 4.0))))
           (if (<= y 4.8959736638846305e+33) 1.0 -1.0)))))))
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(x, x, t_0);
	double tmp;
	if (y <= -2.90709829270765e+109) {
		tmp = -1.0;
	} else if (y <= -3.738119627219816e-161) {
		tmp = ((x * x) / t_1) - (t_0 / t_1);
	} else if (y <= 3.0001361978417216e-145) {
		tmp = 1.0;
	} else if (y <= 174160338219.57495) {
		tmp = ((x * x) + (y * (y * -4.0))) / ((x * x) + (y * (y * 4.0)));
	} else if (y <= 4.8959736638846305e+33) {
		tmp = 1.0;
	} else {
		tmp = -1.0;
	}
	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(Float64(y * y) * 4.0)
	t_1 = fma(x, x, t_0)
	tmp = 0.0
	if (y <= -2.90709829270765e+109)
		tmp = -1.0;
	elseif (y <= -3.738119627219816e-161)
		tmp = Float64(Float64(Float64(x * x) / t_1) - Float64(t_0 / t_1));
	elseif (y <= 3.0001361978417216e-145)
		tmp = 1.0;
	elseif (y <= 174160338219.57495)
		tmp = Float64(Float64(Float64(x * x) + Float64(y * Float64(y * -4.0))) / Float64(Float64(x * x) + Float64(y * Float64(y * 4.0))));
	elseif (y <= 4.8959736638846305e+33)
		tmp = 1.0;
	else
		tmp = -1.0;
	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[(N[(y * y), $MachinePrecision] * 4.0), $MachinePrecision]}, Block[{t$95$1 = N[(x * x + t$95$0), $MachinePrecision]}, If[LessEqual[y, -2.90709829270765e+109], -1.0, If[LessEqual[y, -3.738119627219816e-161], N[(N[(N[(x * x), $MachinePrecision] / t$95$1), $MachinePrecision] - N[(t$95$0 / t$95$1), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 3.0001361978417216e-145], 1.0, If[LessEqual[y, 174160338219.57495], N[(N[(N[(x * x), $MachinePrecision] + N[(y * N[(y * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 4.8959736638846305e+33], 1.0, -1.0]]]]]]]
\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 := \left(y \cdot y\right) \cdot 4\\
t_1 := \mathsf{fma}\left(x, x, t_0\right)\\
\mathbf{if}\;y \leq -2.90709829270765 \cdot 10^{+109}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \leq -3.738119627219816 \cdot 10^{-161}:\\
\;\;\;\;\frac{x \cdot x}{t_1} - \frac{t_0}{t_1}\\

\mathbf{elif}\;y \leq 3.0001361978417216 \cdot 10^{-145}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \leq 174160338219.57495:\\
\;\;\;\;\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 4.8959736638846305 \cdot 10^{+33}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;-1\\


\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original32.0
Target31.7
Herbie13.2
\[\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 4 regimes
  2. if y < -2.9070982927076501e109 or 4.8959736638846305e33 < y

    1. Initial program 47.4

      \[\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 12.8

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

    if -2.9070982927076501e109 < y < -3.73811962721981598e-161

    1. Initial program 15.8

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Applied div-sub_binary6415.8

      \[\leadsto \color{blue}{\frac{x \cdot x}{x \cdot x + \left(y \cdot 4\right) \cdot y} - \frac{\left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}} \]
    3. Simplified15.8

      \[\leadsto \color{blue}{\frac{x \cdot x}{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot 4\right)}} - \frac{\left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    4. Simplified15.8

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

    if -3.73811962721981598e-161 < y < 3.00013619784172163e-145 or 174160338219.57495 < y < 4.8959736638846305e33

    1. Initial program 28.9

      \[\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 9.9

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

    if 3.00013619784172163e-145 < y < 174160338219.57495

    1. Initial program 16.8

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Applied cancel-sign-sub-inv_binary6416.8

      \[\leadsto \frac{\color{blue}{x \cdot x + \left(-y \cdot 4\right) \cdot y}}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification13.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -2.90709829270765 \cdot 10^{+109}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -3.738119627219816 \cdot 10^{-161}:\\ \;\;\;\;\frac{x \cdot x}{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot 4\right)} - \frac{\left(y \cdot y\right) \cdot 4}{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot 4\right)}\\ \mathbf{elif}\;y \leq 3.0001361978417216 \cdot 10^{-145}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 174160338219.57495:\\ \;\;\;\;\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 4.8959736638846305 \cdot 10^{+33}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]

Reproduce

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