Average Error: 31.4 → 12.9
Time: 1.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 := y \cdot \left(y \cdot 4\right)\\ t_1 := {\left(\frac{y}{x}\right)}^{2} \cdot -8\\ \mathbf{if}\;t_0 \leq 2.4518680323519546 \cdot 10^{-249}:\\ \;\;\;\;t_1 + 1\\ \mathbf{elif}\;t_0 \leq 1.5309129005238457 \cdot 10^{+197}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, x, -4 \cdot \left(y \cdot y\right)\right)}{\mathsf{fma}\left(x, x, t_0\right)}\\ \mathbf{elif}\;t_0 \leq 8.243350513607583 \cdot 10^{+226}:\\ \;\;\;\;e^{\mathsf{log1p}\left(t_1\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 (* (pow (/ y x) 2.0) -8.0)))
   (if (<= t_0 2.4518680323519546e-249)
     (+ t_1 1.0)
     (if (<= t_0 1.5309129005238457e+197)
       (/ (fma x x (* -4.0 (* y y))) (fma x x t_0))
       (if (<= t_0 8.243350513607583e+226) (exp (log1p t_1)) -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 = pow((y / x), 2.0) * -8.0;
	double tmp;
	if (t_0 <= 2.4518680323519546e-249) {
		tmp = t_1 + 1.0;
	} else if (t_0 <= 1.5309129005238457e+197) {
		tmp = fma(x, x, (-4.0 * (y * y))) / fma(x, x, t_0);
	} else if (t_0 <= 8.243350513607583e+226) {
		tmp = exp(log1p(t_1));
	} 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(y * Float64(y * 4.0))
	t_1 = Float64((Float64(y / x) ^ 2.0) * -8.0)
	tmp = 0.0
	if (t_0 <= 2.4518680323519546e-249)
		tmp = Float64(t_1 + 1.0);
	elseif (t_0 <= 1.5309129005238457e+197)
		tmp = Float64(fma(x, x, Float64(-4.0 * Float64(y * y))) / fma(x, x, t_0));
	elseif (t_0 <= 8.243350513607583e+226)
		tmp = exp(log1p(t_1));
	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[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Power[N[(y / x), $MachinePrecision], 2.0], $MachinePrecision] * -8.0), $MachinePrecision]}, If[LessEqual[t$95$0, 2.4518680323519546e-249], N[(t$95$1 + 1.0), $MachinePrecision], If[LessEqual[t$95$0, 1.5309129005238457e+197], N[(N[(x * x + N[(-4.0 * N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(x * x + t$95$0), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 8.243350513607583e+226], N[Exp[N[Log[1 + t$95$1], $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 := y \cdot \left(y \cdot 4\right)\\
t_1 := {\left(\frac{y}{x}\right)}^{2} \cdot -8\\
\mathbf{if}\;t_0 \leq 2.4518680323519546 \cdot 10^{-249}:\\
\;\;\;\;t_1 + 1\\

\mathbf{elif}\;t_0 \leq 1.5309129005238457 \cdot 10^{+197}:\\
\;\;\;\;\frac{\mathsf{fma}\left(x, x, -4 \cdot \left(y \cdot y\right)\right)}{\mathsf{fma}\left(x, x, t_0\right)}\\

\mathbf{elif}\;t_0 \leq 8.243350513607583 \cdot 10^{+226}:\\
\;\;\;\;e^{\mathsf{log1p}\left(t_1\right)}\\

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


\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original31.4
Target31.1
Herbie12.9
\[\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 (*.f64 (*.f64 y 4) y) < 2.4518680323519546e-249

    1. Initial program 27.8

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

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

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

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

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

    if 2.4518680323519546e-249 < (*.f64 (*.f64 y 4) y) < 1.5309129005238457e197

    1. Initial program 15.6

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

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

    if 1.5309129005238457e197 < (*.f64 (*.f64 y 4) y) < 8.24335051360758292e226

    1. Initial program 17.4

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

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

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

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

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left({\left(\frac{y}{x}\right)}^{2} \cdot -8\right)}} \]

    if 8.24335051360758292e226 < (*.f64 (*.f64 y 4) y)

    1. Initial program 54.0

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

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

      \[\leadsto \color{blue}{-1} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification12.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot \left(y \cdot 4\right) \leq 2.4518680323519546 \cdot 10^{-249}:\\ \;\;\;\;{\left(\frac{y}{x}\right)}^{2} \cdot -8 + 1\\ \mathbf{elif}\;y \cdot \left(y \cdot 4\right) \leq 1.5309129005238457 \cdot 10^{+197}:\\ \;\;\;\;\frac{\mathsf{fma}\left(x, x, -4 \cdot \left(y \cdot y\right)\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}\\ \mathbf{elif}\;y \cdot \left(y \cdot 4\right) \leq 8.243350513607583 \cdot 10^{+226}:\\ \;\;\;\;e^{\mathsf{log1p}\left({\left(\frac{y}{x}\right)}^{2} \cdot -8\right)}\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]

Reproduce

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