Average Error: 32.1 → 13.0
Time: 2.3s
Precision: 64
\[\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 \cdot x \le 0.0:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \cdot x \le 1.5398622348223129 \cdot 10^{-35}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}\right)\right)\\ \mathbf{elif}\;x \cdot x \le 141747905838.435699:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \cdot x \le 4.0633860768193028 \cdot 10^{300}:\\ \;\;\;\;\frac{x}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}{x}} - \frac{y \cdot 4}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}{y}}\\ \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 \cdot x \le 0.0:\\
\;\;\;\;-1\\

\mathbf{elif}\;x \cdot x \le 1.5398622348223129 \cdot 10^{-35}:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}\right)\right)\\

\mathbf{elif}\;x \cdot x \le 141747905838.435699:\\
\;\;\;\;-1\\

\mathbf{elif}\;x \cdot x \le 4.0633860768193028 \cdot 10^{300}:\\
\;\;\;\;\frac{x}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}{x}} - \frac{y \cdot 4}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}{y}}\\

\mathbf{else}:\\
\;\;\;\;1\\

\end{array}
double f(double x, double y) {
        double r634785 = x;
        double r634786 = r634785 * r634785;
        double r634787 = y;
        double r634788 = 4.0;
        double r634789 = r634787 * r634788;
        double r634790 = r634789 * r634787;
        double r634791 = r634786 - r634790;
        double r634792 = r634786 + r634790;
        double r634793 = r634791 / r634792;
        return r634793;
}

double f(double x, double y) {
        double r634794 = x;
        double r634795 = r634794 * r634794;
        double r634796 = 0.0;
        bool r634797 = r634795 <= r634796;
        double r634798 = -1.0;
        double r634799 = 1.539862234822313e-35;
        bool r634800 = r634795 <= r634799;
        double r634801 = y;
        double r634802 = 4.0;
        double r634803 = r634801 * r634802;
        double r634804 = r634803 * r634801;
        double r634805 = r634795 - r634804;
        double r634806 = r634795 + r634804;
        double r634807 = r634805 / r634806;
        double r634808 = expm1(r634807);
        double r634809 = log1p(r634808);
        double r634810 = 141747905838.4357;
        bool r634811 = r634795 <= r634810;
        double r634812 = 4.063386076819303e+300;
        bool r634813 = r634795 <= r634812;
        double r634814 = fma(r634794, r634794, r634804);
        double r634815 = r634814 / r634794;
        double r634816 = r634794 / r634815;
        double r634817 = r634814 / r634801;
        double r634818 = r634803 / r634817;
        double r634819 = r634816 - r634818;
        double r634820 = 1.0;
        double r634821 = r634813 ? r634819 : r634820;
        double r634822 = r634811 ? r634798 : r634821;
        double r634823 = r634800 ? r634809 : r634822;
        double r634824 = r634797 ? r634798 : r634823;
        return r634824;
}

Error

Bits error versus x

Bits error versus y

Target

Original32.1
Target31.8
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} \lt 0.974323384962678118:\\ \;\;\;\;\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 x) < 0.0 or 1.539862234822313e-35 < (* x x) < 141747905838.4357

    1. Initial program 30.3

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

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

    if 0.0 < (* x x) < 1.539862234822313e-35

    1. Initial program 16.2

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}\]
    2. Using strategy rm
    3. Applied log1p-expm1-u16.2

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}\right)\right)}\]

    if 141747905838.4357 < (* x x) < 4.063386076819303e+300

    1. Initial program 17.5

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}\]
    2. Using strategy rm
    3. Applied div-sub17.5

      \[\leadsto \color{blue}{\frac{x \cdot x}{x \cdot x + \left(y \cdot 4\right) \cdot y} - \frac{\left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}}\]
    4. Simplified17.6

      \[\leadsto \color{blue}{\frac{x}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}{x}}} - \frac{\left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}\]
    5. Simplified17.0

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

    if 4.063386076819303e+300 < (* x x)

    1. Initial program 63.0

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \le 0.0:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \cdot x \le 1.5398622348223129 \cdot 10^{-35}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}\right)\right)\\ \mathbf{elif}\;x \cdot x \le 141747905838.435699:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \cdot x \le 4.0633860768193028 \cdot 10^{300}:\\ \;\;\;\;\frac{x}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}{x}} - \frac{y \cdot 4}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}{y}}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array}\]

Reproduce

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

  :herbie-target
  (if (< (/ (- (* x x) (* (* y 4) y)) (+ (* x x) (* (* y 4) y))) 0.9743233849626781) (- (/ (* x x) (+ (* x x) (* (* y y) 4))) (/ (* (* y y) 4) (+ (* x x) (* (* y y) 4)))) (- (pow (/ x (sqrt (+ (* x x) (* (* y y) 4)))) 2) (/ (* (* y y) 4) (+ (* x x) (* (* y y) 4)))))

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