Average Error: 32.2 → 13.6
Time: 2.8s
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}\;\left(y \cdot 4\right) \cdot y \le 3.555296387473610131894589033479720995539 \cdot 10^{-320}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(1\right)\right)\\ \mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 6.796343694084571001073879733570555857418 \cdot 10^{-30}:\\ \;\;\;\;\frac{1}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}{x \cdot x - \left(y \cdot 4\right) \cdot y}}\\ \mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 346611751697267904479232:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(1\right)\right)\\ \mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 3.303575063990275761709788655917798036004 \cdot 10^{146}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\mathsf{fma}\left(x, x, -\left(y \cdot 4\right) \cdot y\right)}{x \cdot x + \left(y \cdot 4\right) \cdot y}\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}\;\left(y \cdot 4\right) \cdot y \le 3.555296387473610131894589033479720995539 \cdot 10^{-320}:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(1\right)\right)\\

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

\mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 346611751697267904479232:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(1\right)\right)\\

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

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

\end{array}
double f(double x, double y) {
        double r804991 = x;
        double r804992 = r804991 * r804991;
        double r804993 = y;
        double r804994 = 4.0;
        double r804995 = r804993 * r804994;
        double r804996 = r804995 * r804993;
        double r804997 = r804992 - r804996;
        double r804998 = r804992 + r804996;
        double r804999 = r804997 / r804998;
        return r804999;
}

double f(double x, double y) {
        double r805000 = y;
        double r805001 = 4.0;
        double r805002 = r805000 * r805001;
        double r805003 = r805002 * r805000;
        double r805004 = 3.5552963874736e-320;
        bool r805005 = r805003 <= r805004;
        double r805006 = 1.0;
        double r805007 = expm1(r805006);
        double r805008 = log1p(r805007);
        double r805009 = 6.796343694084571e-30;
        bool r805010 = r805003 <= r805009;
        double r805011 = x;
        double r805012 = fma(r805011, r805011, r805003);
        double r805013 = r805011 * r805011;
        double r805014 = r805013 - r805003;
        double r805015 = r805012 / r805014;
        double r805016 = r805006 / r805015;
        double r805017 = 3.466117516972679e+23;
        bool r805018 = r805003 <= r805017;
        double r805019 = 3.3035750639902758e+146;
        bool r805020 = r805003 <= r805019;
        double r805021 = -r805003;
        double r805022 = fma(r805011, r805011, r805021);
        double r805023 = r805013 + r805003;
        double r805024 = r805022 / r805023;
        double r805025 = expm1(r805024);
        double r805026 = log1p(r805025);
        double r805027 = -1.0;
        double r805028 = r805020 ? r805026 : r805027;
        double r805029 = r805018 ? r805008 : r805028;
        double r805030 = r805010 ? r805016 : r805029;
        double r805031 = r805005 ? r805008 : r805030;
        return r805031;
}

Error

Bits error versus x

Bits error versus y

Target

Original32.2
Target31.8
Herbie13.6
\[\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.9743233849626781184483093056769575923681:\\ \;\;\;\;\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.0) y) < 3.5552963874736e-320 or 6.796343694084571e-30 < (* (* y 4.0) y) < 3.466117516972679e+23

    1. Initial program 28.7

      \[\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-u28.7

      \[\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)}\]
    4. Taylor expanded around inf 11.2

      \[\leadsto \mathsf{log1p}\left(\mathsf{expm1}\left(\color{blue}{1}\right)\right)\]

    if 3.5552963874736e-320 < (* (* y 4.0) y) < 6.796343694084571e-30

    1. Initial program 16.4

      \[\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 clear-num16.4

      \[\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}}}\]
    4. Simplified16.4

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

    if 3.466117516972679e+23 < (* (* y 4.0) y) < 3.3035750639902758e+146

    1. Initial program 17.7

      \[\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-u17.7

      \[\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)}\]
    4. Using strategy rm
    5. Applied fma-neg17.7

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

    if 3.3035750639902758e+146 < (* (* y 4.0) y)

    1. Initial program 47.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 around 0 12.7

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(y \cdot 4\right) \cdot y \le 3.555296387473610131894589033479720995539 \cdot 10^{-320}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(1\right)\right)\\ \mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 6.796343694084571001073879733570555857418 \cdot 10^{-30}:\\ \;\;\;\;\frac{1}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}{x \cdot x - \left(y \cdot 4\right) \cdot y}}\\ \mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 346611751697267904479232:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(1\right)\right)\\ \mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 3.303575063990275761709788655917798036004 \cdot 10^{146}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\mathsf{fma}\left(x, x, -\left(y \cdot 4\right) \cdot y\right)}{x \cdot x + \left(y \cdot 4\right) \cdot y}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array}\]

Reproduce

herbie shell --seed 2019354 +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))))