Average Error: 32.0 → 13.0
Time: 2.7s
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 -1.0371372005582628 \cdot 10^{+70}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -2.029692625314952 \cdot 10^{-98}:\\ \;\;\;\;\frac{x \cdot x}{t_1} - \frac{t_0}{t_1}\\ \mathbf{elif}\;y \leq 4.1575865434538223 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 7.048940344096351 \cdot 10^{+96}:\\ \;\;\;\;\frac{1}{\frac{t_1}{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot -4\right)}}\\ \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 -1.0371372005582628e+70)
     -1.0
     (if (<= y -2.029692625314952e-98)
       (- (/ (* x x) t_1) (/ t_0 t_1))
       (if (<= y 4.1575865434538223e-162)
         1.0
         (if (<= y 7.048940344096351e+96)
           (/ 1.0 (/ t_1 (fma x x (* (* y y) -4.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 <= -1.0371372005582628e+70) {
		tmp = -1.0;
	} else if (y <= -2.029692625314952e-98) {
		tmp = ((x * x) / t_1) - (t_0 / t_1);
	} else if (y <= 4.1575865434538223e-162) {
		tmp = 1.0;
	} else if (y <= 7.048940344096351e+96) {
		tmp = 1.0 / (t_1 / fma(x, x, ((y * y) * -4.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 <= -1.0371372005582628e+70)
		tmp = -1.0;
	elseif (y <= -2.029692625314952e-98)
		tmp = Float64(Float64(Float64(x * x) / t_1) - Float64(t_0 / t_1));
	elseif (y <= 4.1575865434538223e-162)
		tmp = 1.0;
	elseif (y <= 7.048940344096351e+96)
		tmp = Float64(1.0 / Float64(t_1 / fma(x, x, Float64(Float64(y * y) * -4.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, -1.0371372005582628e+70], -1.0, If[LessEqual[y, -2.029692625314952e-98], N[(N[(N[(x * x), $MachinePrecision] / t$95$1), $MachinePrecision] - N[(t$95$0 / t$95$1), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 4.1575865434538223e-162], 1.0, If[LessEqual[y, 7.048940344096351e+96], N[(1.0 / N[(t$95$1 / N[(x * x + N[(N[(y * y), $MachinePrecision] * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -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 -1.0371372005582628 \cdot 10^{+70}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \leq 4.1575865434538223 \cdot 10^{-162}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \leq 7.048940344096351 \cdot 10^{+96}:\\
\;\;\;\;\frac{1}{\frac{t_1}{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot -4\right)}}\\

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


\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original32.0
Target31.7
Herbie13.0
\[\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 < -1.0371372005582628e70 or 7.04894034409635117e96 < y

    1. Initial program 49.5

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

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

    if -1.0371372005582628e70 < y < -2.029692625314952e-98

    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. Applied div-sub_binary6417.0

      \[\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. Simplified17.0

      \[\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. Simplified17.0

      \[\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 -2.029692625314952e-98 < y < 4.1575865434538223e-162

    1. Initial program 28.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 inf 9.9

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

    if 4.1575865434538223e-162 < y < 7.04894034409635117e96

    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 clear-num_binary6416.8

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.0371372005582628 \cdot 10^{+70}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -2.029692625314952 \cdot 10^{-98}:\\ \;\;\;\;\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 4.1575865434538223 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 7.048940344096351 \cdot 10^{+96}:\\ \;\;\;\;\frac{1}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot 4\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot -4\right)}}\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]

Reproduce

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