Average Error: 31.0 → 11.9
Time: 2.9s
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 := \mathsf{fma}\left({\left(\frac{y}{x}\right)}^{2}, -8, 1\right)\\ t_2 := \mathsf{fma}\left(x, x, t_0\right)\\ t_3 := \frac{x}{\frac{t_2}{x}} - \frac{y}{\frac{t_2}{y \cdot 4}}\\ \mathbf{if}\;t_0 \leq 0:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 4 \cdot 10^{-222}:\\ \;\;\;\;\log \left(e^{\frac{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot -4\right)\right)}{t_2}}\right)\\ \mathbf{elif}\;t_0 \leq 10^{-147}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 4 \cdot 10^{-48}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;t_0 \leq 10^{+22}:\\ \;\;\;\;\log e\\ \mathbf{elif}\;t_0 \leq 2 \cdot 10^{+218}:\\ \;\;\;\;t_3\\ \mathbf{else}:\\ \;\;\;\;\log \left(\mathsf{fma}\left(0.5, \left(\frac{x}{y} \cdot \frac{x}{y}\right) \cdot e^{-1}, e^{-1}\right)\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 (pow (/ y x) 2.0) -8.0 1.0))
        (t_2 (fma x x t_0))
        (t_3 (- (/ x (/ t_2 x)) (/ y (/ t_2 (* y 4.0))))))
   (if (<= t_0 0.0)
     t_1
     (if (<= t_0 4e-222)
       (log (exp (/ (fma x x (* y (* y -4.0))) t_2)))
       (if (<= t_0 1e-147)
         t_1
         (if (<= t_0 4e-48)
           t_3
           (if (<= t_0 1e+22)
             (log E)
             (if (<= t_0 2e+218)
               t_3
               (log
                (fma
                 0.5
                 (* (* (/ x y) (/ x y)) (exp -1.0))
                 (exp -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(pow((y / x), 2.0), -8.0, 1.0);
	double t_2 = fma(x, x, t_0);
	double t_3 = (x / (t_2 / x)) - (y / (t_2 / (y * 4.0)));
	double tmp;
	if (t_0 <= 0.0) {
		tmp = t_1;
	} else if (t_0 <= 4e-222) {
		tmp = log(exp((fma(x, x, (y * (y * -4.0))) / t_2)));
	} else if (t_0 <= 1e-147) {
		tmp = t_1;
	} else if (t_0 <= 4e-48) {
		tmp = t_3;
	} else if (t_0 <= 1e+22) {
		tmp = log(((double) M_E));
	} else if (t_0 <= 2e+218) {
		tmp = t_3;
	} else {
		tmp = log(fma(0.5, (((x / y) * (x / y)) * exp(-1.0)), exp(-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 = fma((Float64(y / x) ^ 2.0), -8.0, 1.0)
	t_2 = fma(x, x, t_0)
	t_3 = Float64(Float64(x / Float64(t_2 / x)) - Float64(y / Float64(t_2 / Float64(y * 4.0))))
	tmp = 0.0
	if (t_0 <= 0.0)
		tmp = t_1;
	elseif (t_0 <= 4e-222)
		tmp = log(exp(Float64(fma(x, x, Float64(y * Float64(y * -4.0))) / t_2)));
	elseif (t_0 <= 1e-147)
		tmp = t_1;
	elseif (t_0 <= 4e-48)
		tmp = t_3;
	elseif (t_0 <= 1e+22)
		tmp = log(exp(1));
	elseif (t_0 <= 2e+218)
		tmp = t_3;
	else
		tmp = log(fma(0.5, Float64(Float64(Float64(x / y) * Float64(x / y)) * exp(-1.0)), exp(-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 + 1.0), $MachinePrecision]}, Block[{t$95$2 = N[(x * x + t$95$0), $MachinePrecision]}, Block[{t$95$3 = N[(N[(x / N[(t$95$2 / x), $MachinePrecision]), $MachinePrecision] - N[(y / N[(t$95$2 / N[(y * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 0.0], t$95$1, If[LessEqual[t$95$0, 4e-222], N[Log[N[Exp[N[(N[(x * x + N[(y * N[(y * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / t$95$2), $MachinePrecision]], $MachinePrecision]], $MachinePrecision], If[LessEqual[t$95$0, 1e-147], t$95$1, If[LessEqual[t$95$0, 4e-48], t$95$3, If[LessEqual[t$95$0, 1e+22], N[Log[E], $MachinePrecision], If[LessEqual[t$95$0, 2e+218], t$95$3, N[Log[N[(0.5 * N[(N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision] * N[Exp[-1.0], $MachinePrecision]), $MachinePrecision] + N[Exp[-1.0], $MachinePrecision]), $MachinePrecision]], $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 := \mathsf{fma}\left({\left(\frac{y}{x}\right)}^{2}, -8, 1\right)\\
t_2 := \mathsf{fma}\left(x, x, t_0\right)\\
t_3 := \frac{x}{\frac{t_2}{x}} - \frac{y}{\frac{t_2}{y \cdot 4}}\\
\mathbf{if}\;t_0 \leq 0:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_0 \leq 4 \cdot 10^{-222}:\\
\;\;\;\;\log \left(e^{\frac{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot -4\right)\right)}{t_2}}\right)\\

\mathbf{elif}\;t_0 \leq 10^{-147}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;t_0 \leq 4 \cdot 10^{-48}:\\
\;\;\;\;t_3\\

\mathbf{elif}\;t_0 \leq 10^{+22}:\\
\;\;\;\;\log e\\

\mathbf{elif}\;t_0 \leq 2 \cdot 10^{+218}:\\
\;\;\;\;t_3\\

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


\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original31.0
Target30.7
Herbie11.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 5 regimes
  2. if (*.f64 (*.f64 y 4) y) < 0.0 or 4.00000000000000019e-222 < (*.f64 (*.f64 y 4) y) < 9.9999999999999997e-148

    1. Initial program 28.1

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

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

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

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

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

    if 0.0 < (*.f64 (*.f64 y 4) y) < 4.00000000000000019e-222

    1. Initial program 12.9

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

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

    if 9.9999999999999997e-148 < (*.f64 (*.f64 y 4) y) < 3.9999999999999999e-48 or 1e22 < (*.f64 (*.f64 y 4) y) < 2.00000000000000017e218

    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. Applied egg-rr14.9

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

    if 3.9999999999999999e-48 < (*.f64 (*.f64 y 4) y) < 1e22

    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 egg-rr17.0

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

      \[\leadsto \log \color{blue}{\left(e^{1}\right)} \]
    4. Simplified31.7

      \[\leadsto \log \color{blue}{e} \]

    if 2.00000000000000017e218 < (*.f64 (*.f64 y 4) y)

    1. Initial program 53.1

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

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

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

      \[\leadsto \log \color{blue}{\left(\mathsf{fma}\left(0.5, \left(\frac{x}{y} \cdot \frac{x}{y}\right) \cdot e^{-1}, e^{-1}\right)\right)} \]
  3. Recombined 5 regimes into one program.
  4. Final simplification11.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot \left(y \cdot 4\right) \leq 0:\\ \;\;\;\;\mathsf{fma}\left({\left(\frac{y}{x}\right)}^{2}, -8, 1\right)\\ \mathbf{elif}\;y \cdot \left(y \cdot 4\right) \leq 4 \cdot 10^{-222}:\\ \;\;\;\;\log \left(e^{\frac{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot -4\right)\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}}\right)\\ \mathbf{elif}\;y \cdot \left(y \cdot 4\right) \leq 10^{-147}:\\ \;\;\;\;\mathsf{fma}\left({\left(\frac{y}{x}\right)}^{2}, -8, 1\right)\\ \mathbf{elif}\;y \cdot \left(y \cdot 4\right) \leq 4 \cdot 10^{-48}:\\ \;\;\;\;\frac{x}{\frac{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}{x}} - \frac{y}{\frac{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}{y \cdot 4}}\\ \mathbf{elif}\;y \cdot \left(y \cdot 4\right) \leq 10^{+22}:\\ \;\;\;\;\log e\\ \mathbf{elif}\;y \cdot \left(y \cdot 4\right) \leq 2 \cdot 10^{+218}:\\ \;\;\;\;\frac{x}{\frac{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}{x}} - \frac{y}{\frac{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}{y \cdot 4}}\\ \mathbf{else}:\\ \;\;\;\;\log \left(\mathsf{fma}\left(0.5, \left(\frac{x}{y} \cdot \frac{x}{y}\right) \cdot e^{-1}, e^{-1}\right)\right)\\ \end{array} \]

Reproduce

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