Average Error: 31.9 → 12.2
Time: 1.7s
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 -4.9056520310025977 \cdot 10^{+114}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_0 := y \cdot \left(y \cdot 4\right)\\ t_1 := \frac{x \cdot x - t_0}{\mathsf{fma}\left(x, x, t_0\right)}\\ \mathbf{if}\;x \leq -4.496742760871901 \cdot 10^{-109}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 1.8162755885967747 \cdot 10^{-154}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \sqrt[3]{{\left(\frac{x}{y}\right)}^{6}}, -1\right)\\ \mathbf{elif}\;x \leq 6.904674213912628 \cdot 10^{+122}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;1\\ \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 -4.9056520310025977 \cdot 10^{+114}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_0 := y \cdot \left(y \cdot 4\right)\\
t_1 := \frac{x \cdot x - t_0}{\mathsf{fma}\left(x, x, t_0\right)}\\
\mathbf{if}\;x \leq -4.496742760871901 \cdot 10^{-109}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;x \leq 1.8162755885967747 \cdot 10^{-154}:\\
\;\;\;\;\mathsf{fma}\left(0.5, \sqrt[3]{{\left(\frac{x}{y}\right)}^{6}}, -1\right)\\

\mathbf{elif}\;x \leq 6.904674213912628 \cdot 10^{+122}:\\
\;\;\;\;t_1\\

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


\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 -4.9056520310025977e+114)
   1.0
   (let* ((t_0 (* y (* y 4.0))) (t_1 (/ (- (* x x) t_0) (fma x x t_0))))
     (if (<= x -4.496742760871901e-109)
       t_1
       (if (<= x 1.8162755885967747e-154)
         (fma 0.5 (cbrt (pow (/ x y) 6.0)) -1.0)
         (if (<= x 6.904674213912628e+122) t_1 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 <= -4.9056520310025977e+114) {
		tmp = 1.0;
	} else {
		double t_0 = y * (y * 4.0);
		double t_1 = ((x * x) - t_0) / fma(x, x, t_0);
		double tmp_1;
		if (x <= -4.496742760871901e-109) {
			tmp_1 = t_1;
		} else if (x <= 1.8162755885967747e-154) {
			tmp_1 = fma(0.5, cbrt(pow((x / y), 6.0)), -1.0);
		} else if (x <= 6.904674213912628e+122) {
			tmp_1 = t_1;
		} else {
			tmp_1 = 1.0;
		}
		tmp = tmp_1;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original31.9
Target31.6
Herbie12.2
\[\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 x < -4.90565203100259772e114 or 6.90467421391262833e122 < x

    1. Initial program 54.8

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

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

    if -4.90565203100259772e114 < x < -4.49674276087190126e-109 or 1.81627558859677474e-154 < x < 6.90467421391262833e122

    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. Applied fma-def_binary6415.8

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

    if -4.49674276087190126e-109 < x < 1.81627558859677474e-154

    1. Initial program 29.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 0 15.8

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, \frac{x \cdot x}{y \cdot y}, -1\right)} \]
    4. Applied add-cbrt-cube_binary6415.8

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

      \[\leadsto \mathsf{fma}\left(0.5, \sqrt[3]{\color{blue}{{\left(\frac{x}{y}\right)}^{6}}}, -1\right) \]
  3. Recombined 3 regimes into one program.
  4. Final simplification12.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -4.9056520310025977 \cdot 10^{+114}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -4.496742760871901 \cdot 10^{-109}:\\ \;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}\\ \mathbf{elif}\;x \leq 1.8162755885967747 \cdot 10^{-154}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \sqrt[3]{{\left(\frac{x}{y}\right)}^{6}}, -1\right)\\ \mathbf{elif}\;x \leq 6.904674213912628 \cdot 10^{+122}:\\ \;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]

Reproduce

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