Average Error: 31.3 → 13.3
Time: 2.4s
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(\frac{x}{y}\right)}^{2} \cdot 0.5 + -1\\ t_1 := \mathsf{fma}\left({\left(\frac{y}{x}\right)}^{2}, -8, 1\right)\\ t_2 := \frac{\mathsf{fma}\left(y, y \cdot -4, x \cdot x\right)}{\mathsf{fma}\left(y, y \cdot 4, x \cdot x\right)}\\ \mathbf{if}\;x \leq -1 \cdot 10^{+63}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq -2 \cdot 10^{-30}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -1.9 \cdot 10^{-152}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \leq 7.5 \cdot 10^{-142}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 4.4 \cdot 10^{+98}:\\ \;\;\;\;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 (+ (* (pow (/ x y) 2.0) 0.5) -1.0))
        (t_1 (fma (pow (/ y x) 2.0) -8.0 1.0))
        (t_2 (/ (fma y (* y -4.0) (* x x)) (fma y (* y 4.0) (* x x)))))
   (if (<= x -1e+63)
     t_1
     (if (<= x -2e-30)
       t_0
       (if (<= x -1.9e-152)
         t_2
         (if (<= x 7.5e-142) t_0 (if (<= x 4.4e+98) 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 = (pow((x / y), 2.0) * 0.5) + -1.0;
	double t_1 = fma(pow((y / x), 2.0), -8.0, 1.0);
	double t_2 = fma(y, (y * -4.0), (x * x)) / fma(y, (y * 4.0), (x * x));
	double tmp;
	if (x <= -1e+63) {
		tmp = t_1;
	} else if (x <= -2e-30) {
		tmp = t_0;
	} else if (x <= -1.9e-152) {
		tmp = t_2;
	} else if (x <= 7.5e-142) {
		tmp = t_0;
	} else if (x <= 4.4e+98) {
		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(Float64((Float64(x / y) ^ 2.0) * 0.5) + -1.0)
	t_1 = fma((Float64(y / x) ^ 2.0), -8.0, 1.0)
	t_2 = Float64(fma(y, Float64(y * -4.0), Float64(x * x)) / fma(y, Float64(y * 4.0), Float64(x * x)))
	tmp = 0.0
	if (x <= -1e+63)
		tmp = t_1;
	elseif (x <= -2e-30)
		tmp = t_0;
	elseif (x <= -1.9e-152)
		tmp = t_2;
	elseif (x <= 7.5e-142)
		tmp = t_0;
	elseif (x <= 4.4e+98)
		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[(N[(N[Power[N[(x / y), $MachinePrecision], 2.0], $MachinePrecision] * 0.5), $MachinePrecision] + -1.0), $MachinePrecision]}, Block[{t$95$1 = N[(N[Power[N[(y / x), $MachinePrecision], 2.0], $MachinePrecision] * -8.0 + 1.0), $MachinePrecision]}, Block[{t$95$2 = N[(N[(y * N[(y * -4.0), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision] / N[(y * N[(y * 4.0), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -1e+63], t$95$1, If[LessEqual[x, -2e-30], t$95$0, If[LessEqual[x, -1.9e-152], t$95$2, If[LessEqual[x, 7.5e-142], t$95$0, If[LessEqual[x, 4.4e+98], 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 := {\left(\frac{x}{y}\right)}^{2} \cdot 0.5 + -1\\
t_1 := \mathsf{fma}\left({\left(\frac{y}{x}\right)}^{2}, -8, 1\right)\\
t_2 := \frac{\mathsf{fma}\left(y, y \cdot -4, x \cdot x\right)}{\mathsf{fma}\left(y, y \cdot 4, x \cdot x\right)}\\
\mathbf{if}\;x \leq -1 \cdot 10^{+63}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;x \leq -2 \cdot 10^{-30}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq -1.9 \cdot 10^{-152}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;x \leq 7.5 \cdot 10^{-142}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq 4.4 \cdot 10^{+98}:\\
\;\;\;\;t_2\\

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


\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original31.3
Target31.0
Herbie13.3
\[\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 x < -1.00000000000000006e63 or 4.40000000000000017e98 < x

    1. Initial program 47.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 inf 17.2

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{y}{x} \cdot \frac{y}{x}, -8, 1\right)} \]
    4. Applied egg-rr11.4

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

    if -1.00000000000000006e63 < x < -2e-30 or -1.90000000000000006e-152 < x < 7.49999999999999958e-142

    1. Initial program 26.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 18.7

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)} \]
    4. Applied egg-rr13.1

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

    if -2e-30 < x < -1.90000000000000006e-152 or 7.49999999999999958e-142 < x < 4.40000000000000017e98

    1. Initial program 16.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 16.0

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

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(y, y \cdot -4, x \cdot x\right)}}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    4. Taylor expanded in x around 0 16.0

      \[\leadsto \frac{\mathsf{fma}\left(y, y \cdot -4, x \cdot x\right)}{\color{blue}{4 \cdot {y}^{2} + {x}^{2}}} \]
    5. Simplified16.0

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1 \cdot 10^{+63}:\\ \;\;\;\;\mathsf{fma}\left({\left(\frac{y}{x}\right)}^{2}, -8, 1\right)\\ \mathbf{elif}\;x \leq -2 \cdot 10^{-30}:\\ \;\;\;\;{\left(\frac{x}{y}\right)}^{2} \cdot 0.5 + -1\\ \mathbf{elif}\;x \leq -1.9 \cdot 10^{-152}:\\ \;\;\;\;\frac{\mathsf{fma}\left(y, y \cdot -4, x \cdot x\right)}{\mathsf{fma}\left(y, y \cdot 4, x \cdot x\right)}\\ \mathbf{elif}\;x \leq 7.5 \cdot 10^{-142}:\\ \;\;\;\;{\left(\frac{x}{y}\right)}^{2} \cdot 0.5 + -1\\ \mathbf{elif}\;x \leq 4.4 \cdot 10^{+98}:\\ \;\;\;\;\frac{\mathsf{fma}\left(y, y \cdot -4, x \cdot x\right)}{\mathsf{fma}\left(y, y \cdot 4, x \cdot x\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left({\left(\frac{y}{x}\right)}^{2}, -8, 1\right)\\ \end{array} \]

Reproduce

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