Average Error: 31.8 → 13.7
Time: 4.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} \mathbf{if}\;x \leq -2.329334044066695 \cdot 10^{+153}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_0 := y \cdot \left(y \cdot 4\right)\\ t_1 := x \cdot x + t_0\\ t_2 := x \cdot x - t_0\\ \mathbf{if}\;x \leq -1.8134981297322904 \cdot 10^{-53}:\\ \;\;\;\;\begin{array}{l} t_3 := \frac{t_2}{t_1}\\ \sqrt[3]{t_3 \cdot \left(t_3 \cdot t_3\right)} \end{array}\\ \mathbf{elif}\;x \leq -3.9297601949890104 \cdot 10^{-88}:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(x \cdot x, \frac{-0.5}{y \cdot y}, -1\right)}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_4 := \frac{1}{\frac{t_1}{t_2}}\\ \mathbf{if}\;x \leq -3.889732912922227 \cdot 10^{-141}:\\ \;\;\;\;t_4\\ \mathbf{elif}\;x \leq 4.844477666462499 \cdot 10^{-218}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 6.7124149431424 \cdot 10^{+28}:\\ \;\;\;\;t_4\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array}\\ \end{array}\\ \end{array} \]
\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}
\begin{array}{l}
\mathbf{if}\;x \leq -2.329334044066695 \cdot 10^{+153}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_0 := y \cdot \left(y \cdot 4\right)\\
t_1 := x \cdot x + t_0\\
t_2 := x \cdot x - t_0\\
\mathbf{if}\;x \leq -1.8134981297322904 \cdot 10^{-53}:\\
\;\;\;\;\begin{array}{l}
t_3 := \frac{t_2}{t_1}\\
\sqrt[3]{t_3 \cdot \left(t_3 \cdot t_3\right)}
\end{array}\\

\mathbf{elif}\;x \leq -3.9297601949890104 \cdot 10^{-88}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(x \cdot x, \frac{-0.5}{y \cdot y}, -1\right)}\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_4 := \frac{1}{\frac{t_1}{t_2}}\\
\mathbf{if}\;x \leq -3.889732912922227 \cdot 10^{-141}:\\
\;\;\;\;t_4\\

\mathbf{elif}\;x \leq 4.844477666462499 \cdot 10^{-218}:\\
\;\;\;\;-1\\

\mathbf{elif}\;x \leq 6.7124149431424 \cdot 10^{+28}:\\
\;\;\;\;t_4\\

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


\end{array}\\


\end{array}\\


\end{array}
(FPCore (x y)
 :precision binary64
 (/ (- (* x x) (* (* y 4.0) y)) (+ (* x x) (* (* y 4.0) y))))
(FPCore (x y)
 :precision binary64
 (if (<= x -2.329334044066695e+153)
   1.0
   (let* ((t_0 (* y (* y 4.0))) (t_1 (+ (* x x) t_0)) (t_2 (- (* x x) t_0)))
     (if (<= x -1.8134981297322904e-53)
       (let* ((t_3 (/ t_2 t_1))) (cbrt (* t_3 (* t_3 t_3))))
       (if (<= x -3.9297601949890104e-88)
         (/ 1.0 (fma (* x x) (/ -0.5 (* y y)) -1.0))
         (let* ((t_4 (/ 1.0 (/ t_1 t_2))))
           (if (<= x -3.889732912922227e-141)
             t_4
             (if (<= x 4.844477666462499e-218)
               -1.0
               (if (<= x 6.7124149431424e+28) t_4 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 tmp;
	if (x <= -2.329334044066695e+153) {
		tmp = 1.0;
	} else {
		double t_0 = y * (y * 4.0);
		double t_1 = (x * x) + t_0;
		double t_2 = (x * x) - t_0;
		double tmp_1;
		if (x <= -1.8134981297322904e-53) {
			double t_3_2 = t_2 / t_1;
			tmp_1 = cbrt(t_3_2 * (t_3_2 * t_3_2));
		} else if (x <= -3.9297601949890104e-88) {
			tmp_1 = 1.0 / fma((x * x), (-0.5 / (y * y)), -1.0);
		} else {
			double t_4 = 1.0 / (t_1 / t_2);
			double tmp_3;
			if (x <= -3.889732912922227e-141) {
				tmp_3 = t_4;
			} else if (x <= 4.844477666462499e-218) {
				tmp_3 = -1.0;
			} else if (x <= 6.7124149431424e+28) {
				tmp_3 = t_4;
			} else {
				tmp_3 = 1.0;
			}
			tmp_1 = tmp_3;
		}
		tmp = tmp_1;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original31.8
Target31.5
Herbie13.7
\[\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 x < -2.3293340440666951e153 or 6.71241494314239984e28 < x

    1. Initial program 49.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 12.7

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

    if -2.3293340440666951e153 < x < -1.8134981297322904e-53

    1. Initial program 15.1

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

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

    if -1.8134981297322904e-53 < x < -3.92976019498901042e-88

    1. Initial program 22.1

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

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

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

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

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

    if -3.92976019498901042e-88 < x < -3.88973291292222692e-141 or 4.8444776664624986e-218 < x < 6.71241494314239984e28

    1. Initial program 19.2

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Applied clear-num_binary6419.2

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

    if -3.88973291292222692e-141 < x < 4.8444776664624986e-218

    1. Initial program 30.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 7.6

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -2.329334044066695 \cdot 10^{+153}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -1.8134981297322904 \cdot 10^{-53}:\\ \;\;\;\;\sqrt[3]{\frac{x \cdot x - y \cdot \left(y \cdot 4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)} \cdot \left(\frac{x \cdot x - y \cdot \left(y \cdot 4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)} \cdot \frac{x \cdot x - y \cdot \left(y \cdot 4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\right)}\\ \mathbf{elif}\;x \leq -3.9297601949890104 \cdot 10^{-88}:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(x \cdot x, \frac{-0.5}{y \cdot y}, -1\right)}\\ \mathbf{elif}\;x \leq -3.889732912922227 \cdot 10^{-141}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot \left(y \cdot 4\right)}{x \cdot x - y \cdot \left(y \cdot 4\right)}}\\ \mathbf{elif}\;x \leq 4.844477666462499 \cdot 10^{-218}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 6.7124149431424 \cdot 10^{+28}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot \left(y \cdot 4\right)}{x \cdot x - y \cdot \left(y \cdot 4\right)}}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]

Reproduce

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