Average Error: 31.7 → 13.3
Time: 12.9s
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 2.174421384893924790992224326813252125458 \cdot 10^{-280}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(1\right)\right)\\ \mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 8.230670362041086953876160251338189070944 \cdot 10^{131}:\\ \;\;\;\;\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}\\ \mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 2.03161264382512748863457943165021620583 \cdot 10^{174}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(1\right)\right)\\ \mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 1.607743261394176407070185827861113403623 \cdot 10^{244}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{{x}^{2} - 4 \cdot {y}^{2}}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}\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 2.174421384893924790992224326813252125458 \cdot 10^{-280}:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(1\right)\right)\\

\mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 8.230670362041086953876160251338189070944 \cdot 10^{131}:\\
\;\;\;\;\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}\\

\mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 2.03161264382512748863457943165021620583 \cdot 10^{174}:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(1\right)\right)\\

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

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

\end{array}
double f(double x, double y) {
        double r564235 = x;
        double r564236 = r564235 * r564235;
        double r564237 = y;
        double r564238 = 4.0;
        double r564239 = r564237 * r564238;
        double r564240 = r564239 * r564237;
        double r564241 = r564236 - r564240;
        double r564242 = r564236 + r564240;
        double r564243 = r564241 / r564242;
        return r564243;
}

double f(double x, double y) {
        double r564244 = y;
        double r564245 = 4.0;
        double r564246 = r564244 * r564245;
        double r564247 = r564246 * r564244;
        double r564248 = 2.1744213848939248e-280;
        bool r564249 = r564247 <= r564248;
        double r564250 = 1.0;
        double r564251 = expm1(r564250);
        double r564252 = log1p(r564251);
        double r564253 = 8.230670362041087e+131;
        bool r564254 = r564247 <= r564253;
        double r564255 = x;
        double r564256 = r564255 * r564255;
        double r564257 = r564256 - r564247;
        double r564258 = r564256 + r564247;
        double r564259 = r564257 / r564258;
        double r564260 = 2.0316126438251275e+174;
        bool r564261 = r564247 <= r564260;
        double r564262 = 1.6077432613941764e+244;
        bool r564263 = r564247 <= r564262;
        double r564264 = 2.0;
        double r564265 = pow(r564255, r564264);
        double r564266 = pow(r564244, r564264);
        double r564267 = r564245 * r564266;
        double r564268 = r564265 - r564267;
        double r564269 = fma(r564255, r564255, r564247);
        double r564270 = r564268 / r564269;
        double r564271 = expm1(r564270);
        double r564272 = log1p(r564271);
        double r564273 = -1.0;
        double r564274 = r564263 ? r564272 : r564273;
        double r564275 = r564261 ? r564252 : r564274;
        double r564276 = r564254 ? r564259 : r564275;
        double r564277 = r564249 ? r564252 : r564276;
        return r564277;
}

Error

Bits error versus x

Bits error versus y

Target

Original31.7
Target31.4
Herbie13.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} \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) < 2.1744213848939248e-280 or 8.230670362041087e+131 < (* (* y 4.0) y) < 2.0316126438251275e+174

    1. Initial program 27.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 log1p-expm1-u27.4

      \[\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. Simplified27.5

      \[\leadsto \mathsf{log1p}\left(\color{blue}{\mathsf{expm1}\left(\frac{{x}^{2} - 4 \cdot {y}^{2}}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}\right)}\right)\]
    5. Taylor expanded around inf 12.8

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

    if 2.1744213848939248e-280 < (* (* y 4.0) y) < 8.230670362041087e+131

    1. Initial program 16.1

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}\]

    if 2.0316126438251275e+174 < (* (* y 4.0) y) < 1.6077432613941764e+244

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

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

    if 1.6077432613941764e+244 < (* (* y 4.0) y)

    1. Initial program 55.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 10.3

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(y \cdot 4\right) \cdot y \le 2.174421384893924790992224326813252125458 \cdot 10^{-280}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(1\right)\right)\\ \mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 8.230670362041086953876160251338189070944 \cdot 10^{131}:\\ \;\;\;\;\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}\\ \mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 2.03161264382512748863457943165021620583 \cdot 10^{174}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(1\right)\right)\\ \mathbf{elif}\;\left(y \cdot 4\right) \cdot y \le 1.607743261394176407070185827861113403623 \cdot 10^{244}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{{x}^{2} - 4 \cdot {y}^{2}}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array}\]

Reproduce

herbie shell --seed 2019306 +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.974323384962678118) (- (/ (* 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))))