Average Error: 31.2 → 13.9
Time: 19.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 -4.758167358261969805068202523877348166337 \cdot 10^{148}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.63689029313189507811324572738338926855 \cdot 10^{-160}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}\right)\right)\\ \mathbf{elif}\;y \le 1.575397687437991456648509198388754062127 \cdot 10^{-55}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 8.134021215145637850375061461407044861883 \cdot 10^{-38}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}\right)\right)\\ \mathbf{elif}\;y \le 98355.24381927796639502048492431640625:\\ \;\;\;\;1\\ \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 -4.758167358261969805068202523877348166337 \cdot 10^{148}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le 1.575397687437991456648509198388754062127 \cdot 10^{-55}:\\
\;\;\;\;1\\

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

\mathbf{elif}\;y \le 98355.24381927796639502048492431640625:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r397638 = x;
        double r397639 = r397638 * r397638;
        double r397640 = y;
        double r397641 = 4.0;
        double r397642 = r397640 * r397641;
        double r397643 = r397642 * r397640;
        double r397644 = r397639 - r397643;
        double r397645 = r397639 + r397643;
        double r397646 = r397644 / r397645;
        return r397646;
}

double f(double x, double y) {
        double r397647 = y;
        double r397648 = -4.75816735826197e+148;
        bool r397649 = r397647 <= r397648;
        double r397650 = -1.0;
        double r397651 = -1.636890293131895e-160;
        bool r397652 = r397647 <= r397651;
        double r397653 = x;
        double r397654 = r397653 * r397653;
        double r397655 = 4.0;
        double r397656 = r397647 * r397655;
        double r397657 = r397656 * r397647;
        double r397658 = r397654 - r397657;
        double r397659 = fma(r397653, r397653, r397657);
        double r397660 = r397658 / r397659;
        double r397661 = log1p(r397660);
        double r397662 = expm1(r397661);
        double r397663 = 1.5753976874379915e-55;
        bool r397664 = r397647 <= r397663;
        double r397665 = 1.0;
        double r397666 = 8.134021215145638e-38;
        bool r397667 = r397647 <= r397666;
        double r397668 = expm1(r397660);
        double r397669 = log1p(r397668);
        double r397670 = 98355.24381927797;
        bool r397671 = r397647 <= r397670;
        double r397672 = r397671 ? r397665 : r397650;
        double r397673 = r397667 ? r397669 : r397672;
        double r397674 = r397664 ? r397665 : r397673;
        double r397675 = r397652 ? r397662 : r397674;
        double r397676 = r397649 ? r397650 : r397675;
        return r397676;
}

Error

Bits error versus x

Bits error versus y

Target

Original31.2
Target30.9
Herbie13.9
\[\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.75816735826197e+148 or 98355.24381927797 < 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. Simplified47.9

      \[\leadsto \color{blue}{\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}}\]
    3. Taylor expanded around 0 13.3

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

    if -4.75816735826197e+148 < y < -1.636890293131895e-160

    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. Simplified16.0

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

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

    if -1.636890293131895e-160 < y < 1.5753976874379915e-55 or 8.134021215145638e-38 < y < 98355.24381927797

    1. Initial program 24.9

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

      \[\leadsto \color{blue}{\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}}\]
    3. Taylor expanded around inf 12.6

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

    if 1.5753976874379915e-55 < y < 8.134021215145638e-38

    1. Initial program 20.1

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -4.758167358261969805068202523877348166337 \cdot 10^{148}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.63689029313189507811324572738338926855 \cdot 10^{-160}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}\right)\right)\\ \mathbf{elif}\;y \le 1.575397687437991456648509198388754062127 \cdot 10^{-55}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 8.134021215145637850375061461407044861883 \cdot 10^{-38}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}\right)\right)\\ \mathbf{elif}\;y \le 98355.24381927796639502048492431640625:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array}\]

Reproduce

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