Average Error: 31.5 → 12.6
Time: 2.4s
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 := {\left(\frac{x}{y}\right)}^{2} \cdot 0.5 + -1\\ \mathbf{if}\;x \cdot x \leq 3.274786957776617 \cdot 10^{-268}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \cdot x \leq 3.467948661196628 \cdot 10^{-114}:\\ \;\;\;\;\frac{x \cdot x + y \cdot \left(y \cdot -4\right)}{\mathsf{fma}\left(y \cdot y, 4, x \cdot x\right)}\\ \mathbf{elif}\;x \cdot x \leq 1.0681176063038099 \cdot 10^{-101}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \cdot x \leq 1.1080386546582645 \cdot 10^{+261}:\\ \;\;\;\;\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}\;x \cdot x \leq 1.3990540414211703 \cdot 10^{+302}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;{\left(\frac{y}{x}\right)}^{2} \cdot -8 + 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 (+ (* (pow (/ x y) 2.0) 0.5) -1.0)))
   (if (<= (* x x) 3.274786957776617e-268)
     t_0
     (if (<= (* x x) 3.467948661196628e-114)
       (/ (+ (* x x) (* y (* y -4.0))) (fma (* y y) 4.0 (* x x)))
       (if (<= (* x x) 1.0681176063038099e-101)
         t_0
         (if (<= (* x x) 1.1080386546582645e+261)
           (/ (fma y (* y -4.0) (* x x)) (fma x x (* y (* y 4.0))))
           (if (<= (* x x) 1.3990540414211703e+302)
             t_0
             (+ (* (pow (/ y x) 2.0) -8.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 = (pow((x / y), 2.0) * 0.5) + -1.0;
	double tmp;
	if ((x * x) <= 3.274786957776617e-268) {
		tmp = t_0;
	} else if ((x * x) <= 3.467948661196628e-114) {
		tmp = ((x * x) + (y * (y * -4.0))) / fma((y * y), 4.0, (x * x));
	} else if ((x * x) <= 1.0681176063038099e-101) {
		tmp = t_0;
	} else if ((x * x) <= 1.1080386546582645e+261) {
		tmp = fma(y, (y * -4.0), (x * x)) / fma(x, x, (y * (y * 4.0)));
	} else if ((x * x) <= 1.3990540414211703e+302) {
		tmp = t_0;
	} else {
		tmp = (pow((y / x), 2.0) * -8.0) + 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(Float64((Float64(x / y) ^ 2.0) * 0.5) + -1.0)
	tmp = 0.0
	if (Float64(x * x) <= 3.274786957776617e-268)
		tmp = t_0;
	elseif (Float64(x * x) <= 3.467948661196628e-114)
		tmp = Float64(Float64(Float64(x * x) + Float64(y * Float64(y * -4.0))) / fma(Float64(y * y), 4.0, Float64(x * x)));
	elseif (Float64(x * x) <= 1.0681176063038099e-101)
		tmp = t_0;
	elseif (Float64(x * x) <= 1.1080386546582645e+261)
		tmp = Float64(fma(y, Float64(y * -4.0), Float64(x * x)) / fma(x, x, Float64(y * Float64(y * 4.0))));
	elseif (Float64(x * x) <= 1.3990540414211703e+302)
		tmp = t_0;
	else
		tmp = Float64(Float64((Float64(y / x) ^ 2.0) * -8.0) + 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[(N[Power[N[(x / y), $MachinePrecision], 2.0], $MachinePrecision] * 0.5), $MachinePrecision] + -1.0), $MachinePrecision]}, If[LessEqual[N[(x * x), $MachinePrecision], 3.274786957776617e-268], t$95$0, If[LessEqual[N[(x * x), $MachinePrecision], 3.467948661196628e-114], N[(N[(N[(x * x), $MachinePrecision] + N[(y * N[(y * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[(y * y), $MachinePrecision] * 4.0 + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * x), $MachinePrecision], 1.0681176063038099e-101], t$95$0, If[LessEqual[N[(x * x), $MachinePrecision], 1.1080386546582645e+261], N[(N[(y * N[(y * -4.0), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision] / N[(x * x + N[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * x), $MachinePrecision], 1.3990540414211703e+302], t$95$0, N[(N[(N[Power[N[(y / x), $MachinePrecision], 2.0], $MachinePrecision] * -8.0), $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 := {\left(\frac{x}{y}\right)}^{2} \cdot 0.5 + -1\\
\mathbf{if}\;x \cdot x \leq 3.274786957776617 \cdot 10^{-268}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;x \cdot x \leq 1.0681176063038099 \cdot 10^{-101}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \cdot x \leq 1.1080386546582645 \cdot 10^{+261}:\\
\;\;\;\;\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}\;x \cdot x \leq 1.3990540414211703 \cdot 10^{+302}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;{\left(\frac{y}{x}\right)}^{2} \cdot -8 + 1\\


\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original31.5
Target31.1
Herbie12.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 4 regimes
  2. if (*.f64 x x) < 3.2747869577766172e-268 or 3.467948661196628e-114 < (*.f64 x x) < 1.06811760630380989e-101 or 1.1080386546582645e261 < (*.f64 x x) < 1.39905404142117032e302

    1. Initial program 27.2

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

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

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

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

    if 3.2747869577766172e-268 < (*.f64 x x) < 3.467948661196628e-114

    1. Initial program 14.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-rr14.9

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

    if 1.06811760630380989e-101 < (*.f64 x x) < 1.1080386546582645e261

    1. Initial program 15.4

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

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

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

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

    if 1.39905404142117032e302 < (*.f64 x x)

    1. Initial program 62.9

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

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

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

      \[\leadsto \color{blue}{{\left(\frac{y}{x}\right)}^{2} \cdot -8 + 1} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification12.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 3.274786957776617 \cdot 10^{-268}:\\ \;\;\;\;{\left(\frac{x}{y}\right)}^{2} \cdot 0.5 + -1\\ \mathbf{elif}\;x \cdot x \leq 3.467948661196628 \cdot 10^{-114}:\\ \;\;\;\;\frac{x \cdot x + y \cdot \left(y \cdot -4\right)}{\mathsf{fma}\left(y \cdot y, 4, x \cdot x\right)}\\ \mathbf{elif}\;x \cdot x \leq 1.0681176063038099 \cdot 10^{-101}:\\ \;\;\;\;{\left(\frac{x}{y}\right)}^{2} \cdot 0.5 + -1\\ \mathbf{elif}\;x \cdot x \leq 1.1080386546582645 \cdot 10^{+261}:\\ \;\;\;\;\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}\;x \cdot x \leq 1.3990540414211703 \cdot 10^{+302}:\\ \;\;\;\;{\left(\frac{x}{y}\right)}^{2} \cdot 0.5 + -1\\ \mathbf{else}:\\ \;\;\;\;{\left(\frac{y}{x}\right)}^{2} \cdot -8 + 1\\ \end{array} \]

Reproduce

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