Average Error: 30.9 → 4.3
Time: 2.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} t_0 := y \cdot \left(y \cdot 4\right)\\ \mathbf{if}\;t_0 \leq 0:\\ \;\;\;\;1\\ \mathbf{elif}\;t_0 \leq 3.6088182077818963 \cdot 10^{+298}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{fma}\left(\frac{y \cdot y}{x \cdot x}, -4, \log 2\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}
t_0 := y \cdot \left(y \cdot 4\right)\\
\mathbf{if}\;t_0 \leq 0:\\
\;\;\;\;1\\

\mathbf{elif}\;t_0 \leq 3.6088182077818963 \cdot 10^{+298}:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{fma}\left(\frac{y \cdot y}{x \cdot x}, -4, \log 2\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
 (let* ((t_0 (* y (* y 4.0))))
   (if (<= t_0 0.0)
     1.0
     (if (<= t_0 3.6088182077818963e+298)
       (expm1 (fma (/ (* y y) (* x x)) -4.0 (log 2.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 t_0 = y * (y * 4.0);
	double tmp;
	if (t_0 <= 0.0) {
		tmp = 1.0;
	} else if (t_0 <= 3.6088182077818963e+298) {
		tmp = expm1(fma(((y * y) / (x * x)), -4.0, log(2.0)));
	} else {
		tmp = -1.0;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original30.9
Target30.6
Herbie4.3
\[\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 (*.f64 (*.f64 y 4) y) < 0.0

    1. Initial program 30.4

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

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

    if 0.0 < (*.f64 (*.f64 y 4) y) < 3.60881820778189633e298

    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 expm1-log1p-u_binary6415.1

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}\right)\right)} \]
    3. Taylor expanded in x around inf 0.5

      \[\leadsto \mathsf{expm1}\left(\color{blue}{\log 2 - 4 \cdot \frac{{y}^{2}}{{x}^{2}}}\right) \]
    4. Simplified0.5

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

    if 3.60881820778189633e298 < (*.f64 (*.f64 y 4) y)

    1. Initial program 62.6

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot \left(y \cdot 4\right) \leq 0:\\ \;\;\;\;1\\ \mathbf{elif}\;y \cdot \left(y \cdot 4\right) \leq 3.6088182077818963 \cdot 10^{+298}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{fma}\left(\frac{y \cdot y}{x \cdot x}, -4, \log 2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]

Reproduce

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