Average Error: 31.5 → 13.6
Time: 1.6s
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 := \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)}\\ t_1 := \mathsf{fma}\left(\frac{0.5}{y}, \frac{x \cdot x}{y}, -1\right)\\ \mathbf{if}\;x \cdot x \leq 7.594980201689878 \cdot 10^{-293}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \cdot x \leq 2.499038807843666 \cdot 10^{-253}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \cdot x \leq 1.1087674357060143 \cdot 10^{-172}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \cdot x \leq 3.256700432886185 \cdot 10^{-112}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \cdot x \leq 22106031556944.254:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \cdot x \leq 9.636435449841436 \cdot 10^{+56}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \cdot x \leq 6.606821305716693 \cdot 10^{+68}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \cdot x \leq 3.9401376541324113 \cdot 10^{+307}:\\ \;\;\;\;t_0\\ \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 (/ (fma x x (* -4.0 (* y y))) (fma x x (* y (* y 4.0)))))
        (t_1 (fma (/ 0.5 y) (/ (* x x) y) -1.0)))
   (if (<= (* x x) 7.594980201689878e-293)
     t_1
     (if (<= (* x x) 2.499038807843666e-253)
       t_0
       (if (<= (* x x) 1.1087674357060143e-172)
         t_1
         (if (<= (* x x) 3.256700432886185e-112)
           t_0
           (if (<= (* x x) 22106031556944.254)
             t_1
             (if (<= (* x x) 9.636435449841436e+56)
               t_0
               (if (<= (* x x) 6.606821305716693e+68)
                 t_1
                 (if (<= (* x x) 3.9401376541324113e+307) t_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 = fma(x, x, (-4.0 * (y * y))) / fma(x, x, (y * (y * 4.0)));
	double t_1 = fma((0.5 / y), ((x * x) / y), -1.0);
	double tmp;
	if ((x * x) <= 7.594980201689878e-293) {
		tmp = t_1;
	} else if ((x * x) <= 2.499038807843666e-253) {
		tmp = t_0;
	} else if ((x * x) <= 1.1087674357060143e-172) {
		tmp = t_1;
	} else if ((x * x) <= 3.256700432886185e-112) {
		tmp = t_0;
	} else if ((x * x) <= 22106031556944.254) {
		tmp = t_1;
	} else if ((x * x) <= 9.636435449841436e+56) {
		tmp = t_0;
	} else if ((x * x) <= 6.606821305716693e+68) {
		tmp = t_1;
	} else if ((x * x) <= 3.9401376541324113e+307) {
		tmp = t_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(fma(x, x, Float64(-4.0 * Float64(y * y))) / fma(x, x, Float64(y * Float64(y * 4.0))))
	t_1 = fma(Float64(0.5 / y), Float64(Float64(x * x) / y), -1.0)
	tmp = 0.0
	if (Float64(x * x) <= 7.594980201689878e-293)
		tmp = t_1;
	elseif (Float64(x * x) <= 2.499038807843666e-253)
		tmp = t_0;
	elseif (Float64(x * x) <= 1.1087674357060143e-172)
		tmp = t_1;
	elseif (Float64(x * x) <= 3.256700432886185e-112)
		tmp = t_0;
	elseif (Float64(x * x) <= 22106031556944.254)
		tmp = t_1;
	elseif (Float64(x * x) <= 9.636435449841436e+56)
		tmp = t_0;
	elseif (Float64(x * x) <= 6.606821305716693e+68)
		tmp = t_1;
	elseif (Float64(x * x) <= 3.9401376541324113e+307)
		tmp = t_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[(x * x + N[(-4.0 * N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(x * x + N[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(0.5 / y), $MachinePrecision] * N[(N[(x * x), $MachinePrecision] / y), $MachinePrecision] + -1.0), $MachinePrecision]}, If[LessEqual[N[(x * x), $MachinePrecision], 7.594980201689878e-293], t$95$1, If[LessEqual[N[(x * x), $MachinePrecision], 2.499038807843666e-253], t$95$0, If[LessEqual[N[(x * x), $MachinePrecision], 1.1087674357060143e-172], t$95$1, If[LessEqual[N[(x * x), $MachinePrecision], 3.256700432886185e-112], t$95$0, If[LessEqual[N[(x * x), $MachinePrecision], 22106031556944.254], t$95$1, If[LessEqual[N[(x * x), $MachinePrecision], 9.636435449841436e+56], t$95$0, If[LessEqual[N[(x * x), $MachinePrecision], 6.606821305716693e+68], t$95$1, If[LessEqual[N[(x * x), $MachinePrecision], 3.9401376541324113e+307], t$95$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 := \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)}\\
t_1 := \mathsf{fma}\left(\frac{0.5}{y}, \frac{x \cdot x}{y}, -1\right)\\
\mathbf{if}\;x \cdot x \leq 7.594980201689878 \cdot 10^{-293}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;x \cdot x \leq 2.499038807843666 \cdot 10^{-253}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \cdot x \leq 1.1087674357060143 \cdot 10^{-172}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;x \cdot x \leq 3.256700432886185 \cdot 10^{-112}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \cdot x \leq 22106031556944.254:\\
\;\;\;\;t_1\\

\mathbf{elif}\;x \cdot x \leq 9.636435449841436 \cdot 10^{+56}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \cdot x \leq 6.606821305716693 \cdot 10^{+68}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;x \cdot x \leq 3.9401376541324113 \cdot 10^{+307}:\\
\;\;\;\;t_0\\

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


\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original31.5
Target31.2
Herbie13.6
\[\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 (*.f64 x x) < 7.59498020168987849e-293 or 2.49903880784366614e-253 < (*.f64 x x) < 1.10876743570601427e-172 or 3.25670043288618503e-112 < (*.f64 x x) < 22106031556944.2539 or 9.6364354498414361e56 < (*.f64 x x) < 6.6068213057166928e68

    1. Initial program 23.9

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

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

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

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

    if 7.59498020168987849e-293 < (*.f64 x x) < 2.49903880784366614e-253 or 1.10876743570601427e-172 < (*.f64 x x) < 3.25670043288618503e-112 or 22106031556944.2539 < (*.f64 x x) < 9.6364354498414361e56 or 6.6068213057166928e68 < (*.f64 x x) < 3.9401376541324113e307

    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. Simplified15.8

      \[\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 3.9401376541324113e307 < (*.f64 x x)

    1. Initial program 63.9

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Simplified63.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 8.0

      \[\leadsto \color{blue}{1} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification13.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 7.594980201689878 \cdot 10^{-293}:\\ \;\;\;\;\mathsf{fma}\left(\frac{0.5}{y}, \frac{x \cdot x}{y}, -1\right)\\ \mathbf{elif}\;x \cdot x \leq 2.499038807843666 \cdot 10^{-253}:\\ \;\;\;\;\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}\;x \cdot x \leq 1.1087674357060143 \cdot 10^{-172}:\\ \;\;\;\;\mathsf{fma}\left(\frac{0.5}{y}, \frac{x \cdot x}{y}, -1\right)\\ \mathbf{elif}\;x \cdot x \leq 3.256700432886185 \cdot 10^{-112}:\\ \;\;\;\;\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}\;x \cdot x \leq 22106031556944.254:\\ \;\;\;\;\mathsf{fma}\left(\frac{0.5}{y}, \frac{x \cdot x}{y}, -1\right)\\ \mathbf{elif}\;x \cdot x \leq 9.636435449841436 \cdot 10^{+56}:\\ \;\;\;\;\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}\;x \cdot x \leq 6.606821305716693 \cdot 10^{+68}:\\ \;\;\;\;\mathsf{fma}\left(\frac{0.5}{y}, \frac{x \cdot x}{y}, -1\right)\\ \mathbf{elif}\;x \cdot x \leq 3.9401376541324113 \cdot 10^{+307}:\\ \;\;\;\;\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{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))))