Average Error: 31.2 → 12.9
Time: 2.5s
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 := \frac{\mathsf{fma}\left(y, y \cdot -4, x \cdot x\right)}{\mathsf{fma}\left(x, x, t_0\right)}\\ \mathbf{if}\;t_0 \leq 5 \cdot 10^{-219}:\\ \;\;\;\;1\\ \mathbf{elif}\;t_0 \leq 5 \cdot 10^{-168}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 5 \cdot 10^{-80}:\\ \;\;\;\;\mathsf{fma}\left(-8, \frac{y}{\frac{x \cdot x}{y}}, 1\right)\\ \mathbf{elif}\;t_0 \leq 5 \cdot 10^{+161}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{y}, \frac{x \cdot 0.5}{y}, -1\right)\\ \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 y (* y -4.0) (* x x)) (fma x x t_0))))
   (if (<= t_0 5e-219)
     1.0
     (if (<= t_0 5e-168)
       t_1
       (if (<= t_0 5e-80)
         (fma -8.0 (/ y (/ (* x x) y)) 1.0)
         (if (<= t_0 5e+161) t_1 (fma (/ x y) (/ (* x 0.5) y) -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(y, (y * -4.0), (x * x)) / fma(x, x, t_0);
	double tmp;
	if (t_0 <= 5e-219) {
		tmp = 1.0;
	} else if (t_0 <= 5e-168) {
		tmp = t_1;
	} else if (t_0 <= 5e-80) {
		tmp = fma(-8.0, (y / ((x * x) / y)), 1.0);
	} else if (t_0 <= 5e+161) {
		tmp = t_1;
	} else {
		tmp = fma((x / y), ((x * 0.5) / y), -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(fma(y, Float64(y * -4.0), Float64(x * x)) / fma(x, x, t_0))
	tmp = 0.0
	if (t_0 <= 5e-219)
		tmp = 1.0;
	elseif (t_0 <= 5e-168)
		tmp = t_1;
	elseif (t_0 <= 5e-80)
		tmp = fma(-8.0, Float64(y / Float64(Float64(x * x) / y)), 1.0);
	elseif (t_0 <= 5e+161)
		tmp = t_1;
	else
		tmp = fma(Float64(x / y), Float64(Float64(x * 0.5) / y), -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[(y * N[(y * -4.0), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision] / N[(x * x + t$95$0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 5e-219], 1.0, If[LessEqual[t$95$0, 5e-168], t$95$1, If[LessEqual[t$95$0, 5e-80], N[(-8.0 * N[(y / N[(N[(x * x), $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision], If[LessEqual[t$95$0, 5e+161], t$95$1, N[(N[(x / y), $MachinePrecision] * N[(N[(x * 0.5), $MachinePrecision] / y), $MachinePrecision] + -1.0), $MachinePrecision]]]]]]]
\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 := \frac{\mathsf{fma}\left(y, y \cdot -4, x \cdot x\right)}{\mathsf{fma}\left(x, x, t_0\right)}\\
\mathbf{if}\;t_0 \leq 5 \cdot 10^{-219}:\\
\;\;\;\;1\\

\mathbf{elif}\;t_0 \leq 5 \cdot 10^{-168}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_0 \leq 5 \cdot 10^{-80}:\\
\;\;\;\;\mathsf{fma}\left(-8, \frac{y}{\frac{x \cdot x}{y}}, 1\right)\\

\mathbf{elif}\;t_0 \leq 5 \cdot 10^{+161}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{y}, \frac{x \cdot 0.5}{y}, -1\right)\\


\end{array}

Error

Target

Original31.2
Target30.9
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) < 5.0000000000000002e-219

    1. Initial program 26.6

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

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

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

    if 5.0000000000000002e-219 < (*.f64 (*.f64 y 4) y) < 5.00000000000000001e-168 or 5e-80 < (*.f64 (*.f64 y 4) y) < 4.9999999999999997e161

    1. Initial program 15.3

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

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

    if 5.00000000000000001e-168 < (*.f64 (*.f64 y 4) y) < 5e-80

    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. Simplified16.0

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

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

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

    if 4.9999999999999997e161 < (*.f64 (*.f64 y 4) y)

    1. Initial program 49.0

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

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{x}{y}, \frac{x \cdot 0.5}{y}, -1\right)} \]
  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 5 \cdot 10^{-219}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \cdot \left(y \cdot 4\right) \leq 5 \cdot 10^{-168}:\\ \;\;\;\;\frac{\mathsf{fma}\left(y, y \cdot -4, x \cdot x\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}\\ \mathbf{elif}\;y \cdot \left(y \cdot 4\right) \leq 5 \cdot 10^{-80}:\\ \;\;\;\;\mathsf{fma}\left(-8, \frac{y}{\frac{x \cdot x}{y}}, 1\right)\\ \mathbf{elif}\;y \cdot \left(y \cdot 4\right) \leq 5 \cdot 10^{+161}:\\ \;\;\;\;\frac{\mathsf{fma}\left(y, y \cdot -4, x \cdot x\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{x}{y}, \frac{x \cdot 0.5}{y}, -1\right)\\ \end{array} \]

Reproduce

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