Average Error: 31.5 → 13.0
Time: 2.3s
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 -4.783857141098266 \cdot 10^{+105}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -1.6627280741517615 \cdot 10^{-25}:\\ \;\;\;\;\log \left(e^{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot -4\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot 4\right)}}\right)\\ \mathbf{elif}\;y \leq 1.2000096904552647 \cdot 10^{-140}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 1.1059142702918062 \cdot 10^{+101}:\\ \;\;\;\;\begin{array}{l} t_0 := y \cdot \left(y \cdot 4\right)\\ \frac{1}{\frac{x \cdot x + t_0}{x \cdot x - t_0}} \end{array}\\ \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 -4.783857141098266 \cdot 10^{+105}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \leq -1.6627280741517615 \cdot 10^{-25}:\\
\;\;\;\;\log \left(e^{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot -4\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot 4\right)}}\right)\\

\mathbf{elif}\;y \leq 1.2000096904552647 \cdot 10^{-140}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \leq 1.1059142702918062 \cdot 10^{+101}:\\
\;\;\;\;\begin{array}{l}
t_0 := y \cdot \left(y \cdot 4\right)\\
\frac{1}{\frac{x \cdot x + t_0}{x \cdot x - t_0}}
\end{array}\\

\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 -4.783857141098266e+105)
   -1.0
   (if (<= y -1.6627280741517615e-25)
     (log (exp (/ (fma x x (* (* y y) -4.0)) (fma x x (* (* y y) 4.0)))))
     (if (<= y 1.2000096904552647e-140)
       1.0
       (if (<= y 1.1059142702918062e+101)
         (let* ((t_0 (* y (* y 4.0))))
           (/ 1.0 (/ (+ (* x x) t_0) (- (* 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 <= -4.783857141098266e+105) {
		tmp = -1.0;
	} else if (y <= -1.6627280741517615e-25) {
		tmp = log(exp(fma(x, x, ((y * y) * -4.0)) / fma(x, x, ((y * y) * 4.0))));
	} else if (y <= 1.2000096904552647e-140) {
		tmp = 1.0;
	} else if (y <= 1.1059142702918062e+101) {
		double t_0 = y * (y * 4.0);
		tmp = 1.0 / (((x * x) + t_0) / ((x * x) - t_0));
	} else {
		tmp = -1.0;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original31.5
Target31.2
Herbie13.0
\[\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 < -4.783857141098266e105 or 1.1059142702918062e101 < y

    1. Initial program 51.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 10.9

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

    if -4.783857141098266e105 < y < -1.66272807415176148e-25

    1. Initial program 16.5

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Applied add-log-exp_binary6416.5

      \[\leadsto \color{blue}{\log \left(e^{\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}}\right)} \]
    3. Simplified16.5

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

    if -1.66272807415176148e-25 < y < 1.2000096904552647e-140

    1. Initial program 25.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 inf 12.4

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

    if 1.2000096904552647e-140 < y < 1.1059142702918062e101

    1. Initial program 15.9

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Applied clear-num_binary6415.9

      \[\leadsto \color{blue}{\frac{1}{\frac{x \cdot x + \left(y \cdot 4\right) \cdot y}{x \cdot x - \left(y \cdot 4\right) \cdot y}}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification13.0

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -4.783857141098266 \cdot 10^{+105}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -1.6627280741517615 \cdot 10^{-25}:\\ \;\;\;\;\log \left(e^{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot -4\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\right) \cdot 4\right)}}\right)\\ \mathbf{elif}\;y \leq 1.2000096904552647 \cdot 10^{-140}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 1.1059142702918062 \cdot 10^{+101}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot \left(y \cdot 4\right)}{x \cdot x - y \cdot \left(y \cdot 4\right)}}\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]

Reproduce

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