Average Error: 32.0 → 13.1
Time: 2.0s
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 := \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)\\ t_1 := x \cdot \frac{x}{y}\\ t_2 := \frac{t_1}{y \cdot 2}\\ \mathbf{if}\;y \leq -3.2 \cdot 10^{+160}:\\ \;\;\;\;\mathsf{fma}\left(\frac{0.5}{y}, t_1, -1\right)\\ \mathbf{elif}\;y \leq -4 \cdot 10^{-19}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 2.15 \cdot 10^{-163}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y}{x}, \frac{y}{x} \cdot -8, 1\right)\\ \mathbf{elif}\;y \leq 10^{+71}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{-1 + t_2 \cdot t_2}{1 + t_2}\\ \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
         (log (exp (/ (fma x x (* y (* y -4.0))) (fma x x (* y (* y 4.0)))))))
        (t_1 (* x (/ x y)))
        (t_2 (/ t_1 (* y 2.0))))
   (if (<= y -3.2e+160)
     (fma (/ 0.5 y) t_1 -1.0)
     (if (<= y -4e-19)
       t_0
       (if (<= y 2.15e-163)
         (fma (/ y x) (* (/ y x) -8.0) 1.0)
         (if (<= y 1e+71) t_0 (/ (+ -1.0 (* t_2 t_2)) (+ 1.0 t_2))))))))
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 = log(exp((fma(x, x, (y * (y * -4.0))) / fma(x, x, (y * (y * 4.0))))));
	double t_1 = x * (x / y);
	double t_2 = t_1 / (y * 2.0);
	double tmp;
	if (y <= -3.2e+160) {
		tmp = fma((0.5 / y), t_1, -1.0);
	} else if (y <= -4e-19) {
		tmp = t_0;
	} else if (y <= 2.15e-163) {
		tmp = fma((y / x), ((y / x) * -8.0), 1.0);
	} else if (y <= 1e+71) {
		tmp = t_0;
	} else {
		tmp = (-1.0 + (t_2 * t_2)) / (1.0 + t_2);
	}
	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 = log(exp(Float64(fma(x, x, Float64(y * Float64(y * -4.0))) / fma(x, x, Float64(y * Float64(y * 4.0))))))
	t_1 = Float64(x * Float64(x / y))
	t_2 = Float64(t_1 / Float64(y * 2.0))
	tmp = 0.0
	if (y <= -3.2e+160)
		tmp = fma(Float64(0.5 / y), t_1, -1.0);
	elseif (y <= -4e-19)
		tmp = t_0;
	elseif (y <= 2.15e-163)
		tmp = fma(Float64(y / x), Float64(Float64(y / x) * -8.0), 1.0);
	elseif (y <= 1e+71)
		tmp = t_0;
	else
		tmp = Float64(Float64(-1.0 + Float64(t_2 * t_2)) / Float64(1.0 + t_2));
	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[Log[N[Exp[N[(N[(x * x + N[(y * N[(y * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(x * x + N[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(x * N[(x / y), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(t$95$1 / N[(y * 2.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -3.2e+160], N[(N[(0.5 / y), $MachinePrecision] * t$95$1 + -1.0), $MachinePrecision], If[LessEqual[y, -4e-19], t$95$0, If[LessEqual[y, 2.15e-163], N[(N[(y / x), $MachinePrecision] * N[(N[(y / x), $MachinePrecision] * -8.0), $MachinePrecision] + 1.0), $MachinePrecision], If[LessEqual[y, 1e+71], t$95$0, N[(N[(-1.0 + N[(t$95$2 * t$95$2), $MachinePrecision]), $MachinePrecision] / N[(1.0 + t$95$2), $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 := \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)\\
t_1 := x \cdot \frac{x}{y}\\
t_2 := \frac{t_1}{y \cdot 2}\\
\mathbf{if}\;y \leq -3.2 \cdot 10^{+160}:\\
\;\;\;\;\mathsf{fma}\left(\frac{0.5}{y}, t_1, -1\right)\\

\mathbf{elif}\;y \leq -4 \cdot 10^{-19}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;y \leq 10^{+71}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\frac{-1 + t_2 \cdot t_2}{1 + t_2}\\


\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original32.0
Target31.7
Herbie13.1
\[\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 y < -3.1999999999999998e160

    1. Initial program 64.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.2

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

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

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

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

    if -3.1999999999999998e160 < y < -3.9999999999999999e-19 or 2.15000000000000004e-163 < y < 1e71

    1. Initial program 16.6

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

      \[\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 -3.9999999999999999e-19 < y < 2.15000000000000004e-163

    1. Initial program 26.3

      \[\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.5

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

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

    if 1e71 < y

    1. Initial program 48.3

      \[\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.3

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

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

      \[\leadsto \color{blue}{\frac{\frac{\frac{x}{y} \cdot x}{y \cdot 2} \cdot \frac{\frac{x}{y} \cdot x}{y \cdot 2} - 1}{\frac{\frac{x}{y} \cdot x}{y \cdot 2} - -1}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification13.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -3.2 \cdot 10^{+160}:\\ \;\;\;\;\mathsf{fma}\left(\frac{0.5}{y}, x \cdot \frac{x}{y}, -1\right)\\ \mathbf{elif}\;y \leq -4 \cdot 10^{-19}:\\ \;\;\;\;\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 \leq 2.15 \cdot 10^{-163}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y}{x}, \frac{y}{x} \cdot -8, 1\right)\\ \mathbf{elif}\;y \leq 10^{+71}:\\ \;\;\;\;\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{else}:\\ \;\;\;\;\frac{-1 + \frac{x \cdot \frac{x}{y}}{y \cdot 2} \cdot \frac{x \cdot \frac{x}{y}}{y \cdot 2}}{1 + \frac{x \cdot \frac{x}{y}}{y \cdot 2}}\\ \end{array} \]

Reproduce

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