Average Error: 31.6 → 12.8
Time: 10.5s
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}\;y \le -2.04179898258906648865287012115961201463 \cdot 10^{153}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -3.379561403631358792480741892956065284459 \cdot 10^{-65}:\\ \;\;\;\;\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}\;y \le 9.959621465941042556551382638800450476793 \cdot 10^{-80}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 2.371593609881423098610405616408534144725 \cdot 10^{60}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(y, y \cdot 4, x \cdot x\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}\;y \le -2.04179898258906648865287012115961201463 \cdot 10^{153}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -3.379561403631358792480741892956065284459 \cdot 10^{-65}:\\
\;\;\;\;\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}\;y \le 9.959621465941042556551382638800450476793 \cdot 10^{-80}:\\
\;\;\;\;1\\

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

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

\end{array}
double f(double x, double y) {
        double r25796401 = x;
        double r25796402 = r25796401 * r25796401;
        double r25796403 = y;
        double r25796404 = 4.0;
        double r25796405 = r25796403 * r25796404;
        double r25796406 = r25796405 * r25796403;
        double r25796407 = r25796402 - r25796406;
        double r25796408 = r25796402 + r25796406;
        double r25796409 = r25796407 / r25796408;
        return r25796409;
}

double f(double x, double y) {
        double r25796410 = y;
        double r25796411 = -2.0417989825890665e+153;
        bool r25796412 = r25796410 <= r25796411;
        double r25796413 = -1.0;
        double r25796414 = -3.379561403631359e-65;
        bool r25796415 = r25796410 <= r25796414;
        double r25796416 = x;
        double r25796417 = r25796416 * r25796416;
        double r25796418 = 4.0;
        double r25796419 = r25796410 * r25796418;
        double r25796420 = r25796419 * r25796410;
        double r25796421 = r25796417 - r25796420;
        double r25796422 = r25796417 + r25796420;
        double r25796423 = r25796421 / r25796422;
        double r25796424 = expm1(r25796423);
        double r25796425 = log1p(r25796424);
        double r25796426 = 9.959621465941043e-80;
        bool r25796427 = r25796410 <= r25796426;
        double r25796428 = 1.0;
        double r25796429 = 2.371593609881423e+60;
        bool r25796430 = r25796410 <= r25796429;
        double r25796431 = fma(r25796410, r25796419, r25796417);
        double r25796432 = r25796421 / r25796431;
        double r25796433 = log1p(r25796432);
        double r25796434 = expm1(r25796433);
        double r25796435 = r25796430 ? r25796434 : r25796413;
        double r25796436 = r25796427 ? r25796428 : r25796435;
        double r25796437 = r25796415 ? r25796425 : r25796436;
        double r25796438 = r25796412 ? r25796413 : r25796437;
        return r25796438;
}

Error

Bits error versus x

Bits error versus y

Target

Original31.6
Target31.3
Herbie12.8
\[\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 < -2.0417989825890665e+153 or 2.371593609881423e+60 < y

    1. Initial program 52.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.5

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

    if -2.0417989825890665e+153 < y < -3.379561403631359e-65

    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 -3.379561403631359e-65 < y < 9.959621465941043e-80

    1. Initial program 25.4

      \[\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 12.3

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

    if 9.959621465941043e-80 < y < 2.371593609881423e+60

    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. Taylor expanded around 0 15.9

      \[\leadsto \frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\color{blue}{4 \cdot {y}^{2} + {x}^{2}}}\]
    3. Simplified15.9

      \[\leadsto \frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\color{blue}{\mathsf{fma}\left(y, y \cdot 4, x \cdot x\right)}}\]
    4. Using strategy rm
    5. Applied expm1-log1p-u15.9

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(y, y \cdot 4, x \cdot x\right)}\right)\right)}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification12.8

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -2.04179898258906648865287012115961201463 \cdot 10^{153}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -3.379561403631358792480741892956065284459 \cdot 10^{-65}:\\ \;\;\;\;\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}\;y \le 9.959621465941042556551382638800450476793 \cdot 10^{-80}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 2.371593609881423098610405616408534144725 \cdot 10^{60}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(y, y \cdot 4, x \cdot x\right)}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array}\]

Reproduce

herbie shell --seed 2019179 +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.0) (/ (* (* y y) 4.0) (+ (* x x) (* (* y y) 4.0)))))

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