Average Error: 30.8 → 12.5
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}\;x \leq -4.624630821563891 \cdot 10^{+151}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -5.472397692701883 \cdot 10^{-68}:\\ \;\;\;\;\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)}\\ \mathbf{elif}\;x \leq 1.733125606425097 \cdot 10^{-69}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 9.896373077841149 \cdot 10^{+151}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\mathsf{fma}\left(y, -4 \cdot y, x \cdot x\right)}{\mathsf{fma}\left(y, y \cdot 4, x \cdot x\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}\;x \leq -4.624630821563891 \cdot 10^{+151}:\\
\;\;\;\;1\\

\mathbf{elif}\;x \leq -5.472397692701883 \cdot 10^{-68}:\\
\;\;\;\;\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)}\\

\mathbf{elif}\;x \leq 1.733125606425097 \cdot 10^{-69}:\\
\;\;\;\;-1\\

\mathbf{elif}\;x \leq 9.896373077841149 \cdot 10^{+151}:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\mathsf{fma}\left(y, -4 \cdot y, x \cdot x\right)}{\mathsf{fma}\left(y, y \cdot 4, x \cdot x\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 (<= x -4.624630821563891e+151)
   1.0
   (if (<= x -5.472397692701883e-68)
     (/ (fma -4.0 (* y y) (* x x)) (fma x x (* y (* y 4.0))))
     (if (<= x 1.733125606425097e-69)
       -1.0
       (if (<= x 9.896373077841149e+151)
         (expm1
          (log1p (/ (fma y (* -4.0 y) (* x x)) (fma y (* y 4.0) (* x x)))))
         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.624630821563891e+151) {
		tmp = 1.0;
	} else if (x <= -5.472397692701883e-68) {
		tmp = fma(-4.0, (y * y), (x * x)) / fma(x, x, (y * (y * 4.0)));
	} else if (x <= 1.733125606425097e-69) {
		tmp = -1.0;
	} else if (x <= 9.896373077841149e+151) {
		tmp = expm1(log1p(fma(y, (-4.0 * y), (x * x)) / fma(y, (y * 4.0), (x * x))));
	} else {
		tmp = 1.0;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original30.8
Target30.5
Herbie12.5
\[\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 x < -4.62463082156389072e151 or 9.89637307784114881e151 < x

    1. Initial program 63.4

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

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

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

    if -4.62463082156389072e151 < x < -5.47239769270188322e-68

    1. Initial program 15.3

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

    if -5.47239769270188322e-68 < x < 1.733125606425097e-69

    1. Initial program 24.5

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

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

    if 1.733125606425097e-69 < x < 9.89637307784114881e151

    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. Simplified14.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 expm1-log1p-u_binary6414.9

      \[\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)} \]
    4. Simplified14.8

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -4.624630821563891 \cdot 10^{+151}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -5.472397692701883 \cdot 10^{-68}:\\ \;\;\;\;\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)}\\ \mathbf{elif}\;x \leq 1.733125606425097 \cdot 10^{-69}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 9.896373077841149 \cdot 10^{+151}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\mathsf{fma}\left(y, -4 \cdot y, x \cdot x\right)}{\mathsf{fma}\left(y, y \cdot 4, x \cdot x\right)}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]

Reproduce

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