Average Error: 30.8 → 12.4
Time: 10.7s
Precision: 64
\[\frac{x \cdot x - \left(y \cdot 4.0\right) \cdot y}{x \cdot x + \left(y \cdot 4.0\right) \cdot y}\]
\[\begin{array}{l} \mathbf{if}\;y \cdot \left(y \cdot 4.0\right) \le 4.9406564584125 \cdot 10^{-324}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \cdot \left(y \cdot 4.0\right) \le 6.588589890767051 \cdot 10^{-113}:\\ \;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4.0\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4.0\right)\right)}\\ \mathbf{elif}\;y \cdot \left(y \cdot 4.0\right) \le 3.0328235087703392 \cdot 10^{-58}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \cdot \left(y \cdot 4.0\right) \le 2.1123622978353503 \cdot 10^{+259}:\\ \;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4.0\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4.0\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;-1.0\\ \end{array}\]
\frac{x \cdot x - \left(y \cdot 4.0\right) \cdot y}{x \cdot x + \left(y \cdot 4.0\right) \cdot y}
\begin{array}{l}
\mathbf{if}\;y \cdot \left(y \cdot 4.0\right) \le 4.9406564584125 \cdot 10^{-324}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \cdot \left(y \cdot 4.0\right) \le 6.588589890767051 \cdot 10^{-113}:\\
\;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4.0\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4.0\right)\right)}\\

\mathbf{elif}\;y \cdot \left(y \cdot 4.0\right) \le 3.0328235087703392 \cdot 10^{-58}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \cdot \left(y \cdot 4.0\right) \le 2.1123622978353503 \cdot 10^{+259}:\\
\;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4.0\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4.0\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;-1.0\\

\end{array}
double f(double x, double y) {
        double r29656757 = x;
        double r29656758 = r29656757 * r29656757;
        double r29656759 = y;
        double r29656760 = 4.0;
        double r29656761 = r29656759 * r29656760;
        double r29656762 = r29656761 * r29656759;
        double r29656763 = r29656758 - r29656762;
        double r29656764 = r29656758 + r29656762;
        double r29656765 = r29656763 / r29656764;
        return r29656765;
}

double f(double x, double y) {
        double r29656766 = y;
        double r29656767 = 4.0;
        double r29656768 = r29656766 * r29656767;
        double r29656769 = r29656766 * r29656768;
        double r29656770 = 4.9406564584125e-324;
        bool r29656771 = r29656769 <= r29656770;
        double r29656772 = 1.0;
        double r29656773 = 6.588589890767051e-113;
        bool r29656774 = r29656769 <= r29656773;
        double r29656775 = x;
        double r29656776 = r29656775 * r29656775;
        double r29656777 = r29656776 - r29656769;
        double r29656778 = fma(r29656775, r29656775, r29656769);
        double r29656779 = r29656777 / r29656778;
        double r29656780 = 3.0328235087703392e-58;
        bool r29656781 = r29656769 <= r29656780;
        double r29656782 = 2.1123622978353503e+259;
        bool r29656783 = r29656769 <= r29656782;
        double r29656784 = -1.0;
        double r29656785 = r29656783 ? r29656779 : r29656784;
        double r29656786 = r29656781 ? r29656772 : r29656785;
        double r29656787 = r29656774 ? r29656779 : r29656786;
        double r29656788 = r29656771 ? r29656772 : r29656787;
        return r29656788;
}

Error

Bits error versus x

Bits error versus y

Target

Original30.8
Target30.9
Herbie12.4
\[\begin{array}{l} \mathbf{if}\;\frac{x \cdot x - \left(y \cdot 4.0\right) \cdot y}{x \cdot x + \left(y \cdot 4.0\right) \cdot y} \lt 0.9743233849626781:\\ \;\;\;\;\frac{x \cdot x}{x \cdot x + \left(y \cdot y\right) \cdot 4.0} - \frac{\left(y \cdot y\right) \cdot 4.0}{x \cdot x + \left(y \cdot y\right) \cdot 4.0}\\ \mathbf{else}:\\ \;\;\;\;{\left(\frac{x}{\sqrt{x \cdot x + \left(y \cdot y\right) \cdot 4.0}}\right)}^{2} - \frac{\left(y \cdot y\right) \cdot 4.0}{x \cdot x + \left(y \cdot y\right) \cdot 4.0}\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if (* (* y 4.0) y) < 4.9406564584125e-324 or 6.588589890767051e-113 < (* (* y 4.0) y) < 3.0328235087703392e-58

    1. Initial program 28.7

      \[\frac{x \cdot x - \left(y \cdot 4.0\right) \cdot y}{x \cdot x + \left(y \cdot 4.0\right) \cdot y}\]
    2. Taylor expanded around inf 11.0

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

    if 4.9406564584125e-324 < (* (* y 4.0) y) < 6.588589890767051e-113 or 3.0328235087703392e-58 < (* (* y 4.0) y) < 2.1123622978353503e+259

    1. Initial program 15.6

      \[\frac{x \cdot x - \left(y \cdot 4.0\right) \cdot y}{x \cdot x + \left(y \cdot 4.0\right) \cdot y}\]
    2. Using strategy rm
    3. Applied fma-def15.6

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

    if 2.1123622978353503e+259 < (* (* y 4.0) y)

    1. Initial program 56.5

      \[\frac{x \cdot x - \left(y \cdot 4.0\right) \cdot y}{x \cdot x + \left(y \cdot 4.0\right) \cdot y}\]
    2. Taylor expanded around 0 8.9

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot \left(y \cdot 4.0\right) \le 4.9406564584125 \cdot 10^{-324}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \cdot \left(y \cdot 4.0\right) \le 6.588589890767051 \cdot 10^{-113}:\\ \;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4.0\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4.0\right)\right)}\\ \mathbf{elif}\;y \cdot \left(y \cdot 4.0\right) \le 3.0328235087703392 \cdot 10^{-58}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \cdot \left(y \cdot 4.0\right) \le 2.1123622978353503 \cdot 10^{+259}:\\ \;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4.0\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4.0\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;-1.0\\ \end{array}\]

Reproduce

herbie shell --seed 2019164 +o rules:numerics
(FPCore (x y)
  :name "Diagrams.TwoD.Arc:arcBetween from diagrams-lib-1.3.0.3"

  :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) (/ (* (* y y) 4.0) (+ (* x x) (* (* y y) 4.0)))))

  (/ (- (* x x) (* (* y 4.0) y)) (+ (* x x) (* (* y 4.0) y))))