Average Error: 20.4 → 5.1
Time: 19.9s
Precision: 64
\[0.0 \lt x \lt 1 \land y \lt 1\]
\[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
\[\begin{array}{l} \mathbf{if}\;y \le -5.351376453551335025298421937367139214806 \cdot 10^{150}:\\ \;\;\;\;\log \left(e^{-1}\right)\\ \mathbf{elif}\;y \le -6.671301972241293882608101356321744148883 \cdot 10^{-163}:\\ \;\;\;\;\log \left(e^{\frac{\left(y + x\right) \cdot \left(x - y\right)}{x \cdot x + y \cdot y}}\right)\\ \mathbf{elif}\;y \le -6.640367748625040373106765238414079239901 \cdot 10^{-188}:\\ \;\;\;\;\log \left(e^{-1}\right)\\ \mathbf{elif}\;y \le 2.495375493781523854915214205899519972995 \cdot 10^{-161}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{\left(y + x\right) \cdot \left(x - y\right)}{x \cdot x + y \cdot y}}\right)\\ \end{array}\]
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\begin{array}{l}
\mathbf{if}\;y \le -5.351376453551335025298421937367139214806 \cdot 10^{150}:\\
\;\;\;\;\log \left(e^{-1}\right)\\

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

\mathbf{elif}\;y \le -6.640367748625040373106765238414079239901 \cdot 10^{-188}:\\
\;\;\;\;\log \left(e^{-1}\right)\\

\mathbf{elif}\;y \le 2.495375493781523854915214205899519972995 \cdot 10^{-161}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;\log \left(e^{\frac{\left(y + x\right) \cdot \left(x - y\right)}{x \cdot x + y \cdot y}}\right)\\

\end{array}
double f(double x, double y) {
        double r114539 = x;
        double r114540 = y;
        double r114541 = r114539 - r114540;
        double r114542 = r114539 + r114540;
        double r114543 = r114541 * r114542;
        double r114544 = r114539 * r114539;
        double r114545 = r114540 * r114540;
        double r114546 = r114544 + r114545;
        double r114547 = r114543 / r114546;
        return r114547;
}

double f(double x, double y) {
        double r114548 = y;
        double r114549 = -5.351376453551335e+150;
        bool r114550 = r114548 <= r114549;
        double r114551 = -1.0;
        double r114552 = exp(r114551);
        double r114553 = log(r114552);
        double r114554 = -6.671301972241294e-163;
        bool r114555 = r114548 <= r114554;
        double r114556 = x;
        double r114557 = r114548 + r114556;
        double r114558 = r114556 - r114548;
        double r114559 = r114557 * r114558;
        double r114560 = r114556 * r114556;
        double r114561 = r114548 * r114548;
        double r114562 = r114560 + r114561;
        double r114563 = r114559 / r114562;
        double r114564 = exp(r114563);
        double r114565 = log(r114564);
        double r114566 = -6.64036774862504e-188;
        bool r114567 = r114548 <= r114566;
        double r114568 = 2.495375493781524e-161;
        bool r114569 = r114548 <= r114568;
        double r114570 = 1.0;
        double r114571 = r114569 ? r114570 : r114565;
        double r114572 = r114567 ? r114553 : r114571;
        double r114573 = r114555 ? r114565 : r114572;
        double r114574 = r114550 ? r114553 : r114573;
        return r114574;
}

Error

Bits error versus x

Bits error versus y

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original20.4
Target0.0
Herbie5.1
\[\begin{array}{l} \mathbf{if}\;0.5 \lt \left|\frac{x}{y}\right| \lt 2:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;1 - \frac{2}{1 + \frac{x}{y} \cdot \frac{x}{y}}\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if y < -5.351376453551335e+150 or -6.671301972241294e-163 < y < -6.64036774862504e-188

    1. Initial program 59.1

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified57.5

      \[\leadsto \color{blue}{\frac{x - y}{x \cdot x + y \cdot y} \cdot \left(y + x\right)}\]
    3. Using strategy rm
    4. Applied add-log-exp57.5

      \[\leadsto \color{blue}{\log \left(e^{\frac{x - y}{x \cdot x + y \cdot y} \cdot \left(y + x\right)}\right)}\]
    5. Taylor expanded around 0 4.5

      \[\leadsto \log \left(e^{\color{blue}{-1}}\right)\]

    if -5.351376453551335e+150 < y < -6.671301972241294e-163 or 2.495375493781524e-161 < y

    1. Initial program 0.1

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified0.6

      \[\leadsto \color{blue}{\frac{x - y}{x \cdot x + y \cdot y} \cdot \left(y + x\right)}\]
    3. Using strategy rm
    4. Applied add-log-exp0.7

      \[\leadsto \color{blue}{\log \left(e^{\frac{x - y}{x \cdot x + y \cdot y} \cdot \left(y + x\right)}\right)}\]
    5. Using strategy rm
    6. Applied associate-*l/0.1

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

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

    if -6.64036774862504e-188 < y < 2.495375493781524e-161

    1. Initial program 30.1

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified30.8

      \[\leadsto \color{blue}{\frac{x - y}{x \cdot x + y \cdot y} \cdot \left(y + x\right)}\]
    3. Using strategy rm
    4. Applied add-log-exp30.7

      \[\leadsto \color{blue}{\log \left(e^{\frac{x - y}{x \cdot x + y \cdot y} \cdot \left(y + x\right)}\right)}\]
    5. Using strategy rm
    6. Applied associate-*l/30.1

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

      \[\leadsto \log \left(e^{\frac{\color{blue}{\left(y + x\right) \cdot \left(x - y\right)}}{x \cdot x + y \cdot y}}\right)\]
    8. Taylor expanded around 0 14.2

      \[\leadsto \log \left(e^{\color{blue}{1}}\right)\]
  3. Recombined 3 regimes into one program.
  4. Final simplification5.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -5.351376453551335025298421937367139214806 \cdot 10^{150}:\\ \;\;\;\;\log \left(e^{-1}\right)\\ \mathbf{elif}\;y \le -6.671301972241293882608101356321744148883 \cdot 10^{-163}:\\ \;\;\;\;\log \left(e^{\frac{\left(y + x\right) \cdot \left(x - y\right)}{x \cdot x + y \cdot y}}\right)\\ \mathbf{elif}\;y \le -6.640367748625040373106765238414079239901 \cdot 10^{-188}:\\ \;\;\;\;\log \left(e^{-1}\right)\\ \mathbf{elif}\;y \le 2.495375493781523854915214205899519972995 \cdot 10^{-161}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{\left(y + x\right) \cdot \left(x - y\right)}{x \cdot x + y \cdot y}}\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2019208 
(FPCore (x y)
  :name "Kahan p9 Example"
  :precision binary64
  :pre (and (< 0.0 x 1) (< y 1))

  :herbie-target
  (if (< 0.5 (fabs (/ x y)) 2) (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))) (- 1 (/ 2 (+ 1 (* (/ x y) (/ x y))))))

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