Average Error: 31.7 → 14.8
Time: 27.7s
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 -1.302473734361689583818414967767065647429 \cdot 10^{64}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 4.427972959141087508327226963770083374106 \cdot 10^{-125}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 4.117086201251834897728735635968684829902 \cdot 10^{60}:\\ \;\;\;\;\log \left(e^{\frac{x \cdot x - y \cdot \left(4 \cdot y\right)}{x \cdot x + y \cdot \left(4 \cdot y\right)}}\right)\\ \mathbf{elif}\;y \le 5.419438360639095008822556977586250635758 \cdot 10^{72}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 1.027562094282635282760369761847330220115 \cdot 10^{103}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{x \cdot x}{\mathsf{fma}\left(4 \cdot y, y, x \cdot x\right)} - \frac{4 \cdot y}{\mathsf{fma}\left(4 \cdot y, y, x \cdot x\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}\;y \le -1.302473734361689583818414967767065647429 \cdot 10^{64}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le 4.427972959141087508327226963770083374106 \cdot 10^{-125}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \le 4.117086201251834897728735635968684829902 \cdot 10^{60}:\\
\;\;\;\;\log \left(e^{\frac{x \cdot x - y \cdot \left(4 \cdot y\right)}{x \cdot x + y \cdot \left(4 \cdot y\right)}}\right)\\

\mathbf{elif}\;y \le 5.419438360639095008822556977586250635758 \cdot 10^{72}:\\
\;\;\;\;1\\

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

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

\end{array}
double f(double x, double y) {
        double r29253925 = x;
        double r29253926 = r29253925 * r29253925;
        double r29253927 = y;
        double r29253928 = 4.0;
        double r29253929 = r29253927 * r29253928;
        double r29253930 = r29253929 * r29253927;
        double r29253931 = r29253926 - r29253930;
        double r29253932 = r29253926 + r29253930;
        double r29253933 = r29253931 / r29253932;
        return r29253933;
}

double f(double x, double y) {
        double r29253934 = y;
        double r29253935 = -1.3024737343616896e+64;
        bool r29253936 = r29253934 <= r29253935;
        double r29253937 = -1.0;
        double r29253938 = 4.4279729591410875e-125;
        bool r29253939 = r29253934 <= r29253938;
        double r29253940 = 1.0;
        double r29253941 = 4.117086201251835e+60;
        bool r29253942 = r29253934 <= r29253941;
        double r29253943 = x;
        double r29253944 = r29253943 * r29253943;
        double r29253945 = 4.0;
        double r29253946 = r29253945 * r29253934;
        double r29253947 = r29253934 * r29253946;
        double r29253948 = r29253944 - r29253947;
        double r29253949 = r29253944 + r29253947;
        double r29253950 = r29253948 / r29253949;
        double r29253951 = exp(r29253950);
        double r29253952 = log(r29253951);
        double r29253953 = 5.419438360639095e+72;
        bool r29253954 = r29253934 <= r29253953;
        double r29253955 = 1.0275620942826353e+103;
        bool r29253956 = r29253934 <= r29253955;
        double r29253957 = fma(r29253946, r29253934, r29253944);
        double r29253958 = r29253944 / r29253957;
        double r29253959 = r29253946 / r29253957;
        double r29253960 = r29253959 * r29253934;
        double r29253961 = r29253958 - r29253960;
        double r29253962 = expm1(r29253961);
        double r29253963 = log1p(r29253962);
        double r29253964 = r29253956 ? r29253963 : r29253937;
        double r29253965 = r29253954 ? r29253940 : r29253964;
        double r29253966 = r29253942 ? r29253952 : r29253965;
        double r29253967 = r29253939 ? r29253940 : r29253966;
        double r29253968 = r29253936 ? r29253937 : r29253967;
        return r29253968;
}

Error

Bits error versus x

Bits error versus y

Target

Original31.7
Target31.4
Herbie14.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 < -1.3024737343616896e+64 or 1.0275620942826353e+103 < y

    1. Initial program 48.8

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

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

    if -1.3024737343616896e+64 < y < 4.4279729591410875e-125 or 4.117086201251835e+60 < y < 5.419438360639095e+72

    1. Initial program 24.5

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

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

    if 4.4279729591410875e-125 < y < 4.117086201251835e+60

    1. Initial program 16.0

      \[\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 add-log-exp16.0

      \[\leadsto \color{blue}{\log \left(e^{\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}}\right)}\]

    if 5.419438360639095e+72 < y < 1.0275620942826353e+103

    1. Initial program 18.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 add-log-exp18.2

      \[\leadsto \color{blue}{\log \left(e^{\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}}\right)}\]
    4. Using strategy rm
    5. Applied log1p-expm1-u18.2

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log \left(e^{\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}}\right)\right)\right)}\]
    6. Simplified18.3

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.302473734361689583818414967767065647429 \cdot 10^{64}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 4.427972959141087508327226963770083374106 \cdot 10^{-125}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 4.117086201251834897728735635968684829902 \cdot 10^{60}:\\ \;\;\;\;\log \left(e^{\frac{x \cdot x - y \cdot \left(4 \cdot y\right)}{x \cdot x + y \cdot \left(4 \cdot y\right)}}\right)\\ \mathbf{elif}\;y \le 5.419438360639095008822556977586250635758 \cdot 10^{72}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 1.027562094282635282760369761847330220115 \cdot 10^{103}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{x \cdot x}{\mathsf{fma}\left(4 \cdot y, y, x \cdot x\right)} - \frac{4 \cdot y}{\mathsf{fma}\left(4 \cdot y, y, x \cdot x\right)} \cdot y\right)\right)\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array}\]

Reproduce

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