Average Error: 31.4 → 14.2
Time: 2.1s
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 -1.655205010790235 \cdot 10^{+28}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 1.0915839863199315 \cdot 10^{-96}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 4.308904655747995 \cdot 10^{+153}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\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} \]
\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 -1.655205010790235 \cdot 10^{+28}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \leq 1.0915839863199315 \cdot 10^{-96}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \leq 4.308904655747995 \cdot 10^{+153}:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\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}
(FPCore (x y)
 :precision binary64
 (/ (- (* x x) (* (* y 4.0) y)) (+ (* x x) (* (* y 4.0) y))))
(FPCore (x y)
 :precision binary64
 (if (<= y -1.655205010790235e+28)
   -1.0
   (if (<= y 1.0915839863199315e-96)
     1.0
     (if (<= y 4.308904655747995e+153)
       (expm1 (log1p (/ (fma -4.0 (* y y) (* x x)) (fma x x (* y (* y 4.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 <= -1.655205010790235e+28) {
		tmp = -1.0;
	} else if (y <= 1.0915839863199315e-96) {
		tmp = 1.0;
	} else if (y <= 4.308904655747995e+153) {
		tmp = expm1(log1p(fma(-4.0, (y * y), (x * x)) / fma(x, x, (y * (y * 4.0)))));
	} else {
		tmp = -1.0;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original31.4
Target31.1
Herbie14.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 y < -1.6552050107902349e28 or 4.30890465574799467e153 < y

    1. Initial program 49.2

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

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

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

    if -1.6552050107902349e28 < y < 1.09158398631993154e-96

    1. Initial program 24.4

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Simplified24.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 15.1

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

    if 1.09158398631993154e-96 < y < 4.30890465574799467e153

    1. Initial program 16.3

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

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

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\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 3 regimes into one program.
  4. Final simplification14.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.655205010790235 \cdot 10^{+28}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 1.0915839863199315 \cdot 10^{-96}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 4.308904655747995 \cdot 10^{+153}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\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 2022068 
(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))))