Average Error: 32.1 → 14.6
Time: 3.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} \mathbf{if}\;y \leq -7.17550285507751 \cdot 10^{+99}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -3.889439415150952 \cdot 10^{-35}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_0 := y \cdot \left(y \cdot 4\right)\\ \mathbf{if}\;y \leq -5.632296344258486 \cdot 10^{-143}:\\ \;\;\;\;\frac{x \cdot x - t_0}{x \cdot x + t_0}\\ \mathbf{elif}\;y \leq 1.4444647175745426 \cdot 10^{-110}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 1.2394672638586602 \cdot 10^{+113}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\mathsf{fma}\left(-4, y \cdot y, x \cdot x\right)}{\mathsf{fma}\left(x, x, t_0\right)}\right)\right)\\ \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}\;y \leq -7.17550285507751 \cdot 10^{+99}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \leq -3.889439415150952 \cdot 10^{-35}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_0 := y \cdot \left(y \cdot 4\right)\\
\mathbf{if}\;y \leq -5.632296344258486 \cdot 10^{-143}:\\
\;\;\;\;\frac{x \cdot x - t_0}{x \cdot x + t_0}\\

\mathbf{elif}\;y \leq 1.4444647175745426 \cdot 10^{-110}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \leq 1.2394672638586602 \cdot 10^{+113}:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\mathsf{fma}\left(-4, y \cdot y, x \cdot x\right)}{\mathsf{fma}\left(x, x, t_0\right)}\right)\right)\\

\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 (<= y -7.17550285507751e+99)
   -1.0
   (if (<= y -3.889439415150952e-35)
     1.0
     (let* ((t_0 (* y (* y 4.0))))
       (if (<= y -5.632296344258486e-143)
         (/ (- (* x x) t_0) (+ (* x x) t_0))
         (if (<= y 1.4444647175745426e-110)
           1.0
           (if (<= y 1.2394672638586602e+113)
             (log1p (expm1 (/ (fma -4.0 (* y y) (* x x)) (fma x x t_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 tmp;
	if (y <= -7.17550285507751e+99) {
		tmp = -1.0;
	} else if (y <= -3.889439415150952e-35) {
		tmp = 1.0;
	} else {
		double t_0 = y * (y * 4.0);
		double tmp_1;
		if (y <= -5.632296344258486e-143) {
			tmp_1 = ((x * x) - t_0) / ((x * x) + t_0);
		} else if (y <= 1.4444647175745426e-110) {
			tmp_1 = 1.0;
		} else if (y <= 1.2394672638586602e+113) {
			tmp_1 = log1p(expm1(fma(-4.0, (y * y), (x * x)) / fma(x, x, t_0)));
		} else {
			tmp_1 = -1.0;
		}
		tmp = tmp_1;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original32.1
Target31.8
Herbie14.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 y < -7.17550285507750968e99 or 1.23946726385866024e113 < y

    1. Initial program 52.7

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Simplified52.7

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

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

    if -7.17550285507750968e99 < y < -3.8894394151509519e-35 or -5.63229634425848629e-143 < y < 1.44446471757454257e-110

    1. Initial program 25.4

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Simplified25.5

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

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

    if -3.8894394151509519e-35 < y < -5.63229634425848629e-143

    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} \]

    if 1.44446471757454257e-110 < y < 1.23946726385866024e113

    1. Initial program 16.8

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Simplified16.8

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(-4, y \cdot y, x \cdot x\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}} \]
    3. Applied log1p-expm1-u_binary6416.8

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\mathsf{fma}\left(-4, y \cdot y, x \cdot x\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}\right)\right)} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification14.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -7.17550285507751 \cdot 10^{+99}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -3.889439415150952 \cdot 10^{-35}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq -5.632296344258486 \cdot 10^{-143}:\\ \;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{elif}\;y \leq 1.4444647175745426 \cdot 10^{-110}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 1.2394672638586602 \cdot 10^{+113}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\mathsf{fma}\left(-4, y \cdot y, x \cdot x\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]

Reproduce

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