Average Error: 22.6 → 0.7
Time: 3.6s
Precision: binary64
\[1 - \frac{\left(1 - x\right) \cdot y}{y + 1} \]
\[\begin{array}{l} \mathbf{if}\;\begin{array}{l} t_0 := \frac{\left(1 - x\right) \cdot y}{1 + y}\\ t_0 \leq 1.3651577721425698 \cdot 10^{-6} \lor \neg \left(t_0 \leq 1\right) \end{array}:\\ \;\;\;\;\mathsf{fma}\left(\frac{y}{1 + y}, x - 1, 1\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x + \frac{x}{y \cdot y}\right) - \left({y}^{-2} + \frac{x - 1}{y}\right)\\ \end{array} \]
1 - \frac{\left(1 - x\right) \cdot y}{y + 1}
\begin{array}{l}
\mathbf{if}\;\begin{array}{l}
t_0 := \frac{\left(1 - x\right) \cdot y}{1 + y}\\
t_0 \leq 1.3651577721425698 \cdot 10^{-6} \lor \neg \left(t_0 \leq 1\right)
\end{array}:\\
\;\;\;\;\mathsf{fma}\left(\frac{y}{1 + y}, x - 1, 1\right)\\

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


\end{array}
(FPCore (x y) :precision binary64 (- 1.0 (/ (* (- 1.0 x) y) (+ y 1.0))))
(FPCore (x y)
 :precision binary64
 (if (let* ((t_0 (/ (* (- 1.0 x) y) (+ 1.0 y))))
       (or (<= t_0 1.3651577721425698e-6) (not (<= t_0 1.0))))
   (fma (/ y (+ 1.0 y)) (- x 1.0) 1.0)
   (- (+ x (/ x (* y y))) (+ (pow y -2.0) (/ (- x 1.0) y)))))
double code(double x, double y) {
	return 1.0 - (((1.0 - x) * y) / (y + 1.0));
}
double code(double x, double y) {
	double t_0 = ((1.0 - x) * y) / (1.0 + y);
	double tmp;
	if ((t_0 <= 1.3651577721425698e-6) || !(t_0 <= 1.0)) {
		tmp = fma((y / (1.0 + y)), (x - 1.0), 1.0);
	} else {
		tmp = (x + (x / (y * y))) - (pow(y, -2.0) + ((x - 1.0) / y));
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original22.6
Target0.2
Herbie0.7
\[\begin{array}{l} \mathbf{if}\;y < -3693.8482788297247:\\ \;\;\;\;\frac{1}{y} - \left(\frac{x}{y} - x\right)\\ \mathbf{elif}\;y < 6799310503.41891:\\ \;\;\;\;1 - \frac{\left(1 - x\right) \cdot y}{y + 1}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{y} - \left(\frac{x}{y} - x\right)\\ \end{array} \]

Derivation

  1. Split input into 2 regimes
  2. if (/.f64 (*.f64 (-.f64 1 x) y) (+.f64 y 1)) < 1.3651577721425698e-6 or 1 < (/.f64 (*.f64 (-.f64 1 x) y) (+.f64 y 1))

    1. Initial program 11.2

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, \frac{x + -1}{1 + y}, 1\right)} \]
    3. Applied fma-udef_binary640.4

      \[\leadsto \color{blue}{y \cdot \frac{x + -1}{1 + y} + 1} \]
    4. Simplified0.4

      \[\leadsto \color{blue}{\frac{y}{1 + y} \cdot \left(x + -1\right)} + 1 \]
    5. Taylor expanded in x around 0 11.2

      \[\leadsto \color{blue}{\left(1 + \frac{y \cdot x}{1 + y}\right) - \frac{y}{1 + y}} \]
    6. Simplified0.4

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{y}{1 + y}, x - 1, 1\right)} \]

    if 1.3651577721425698e-6 < (/.f64 (*.f64 (-.f64 1 x) y) (+.f64 y 1)) < 1

    1. Initial program 58.0

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, \frac{x + -1}{1 + y}, 1\right)} \]
    3. Taylor expanded in y around inf 1.6

      \[\leadsto \color{blue}{\left(\frac{1}{y} + \left(\frac{x}{{y}^{2}} + x\right)\right) - \left(\frac{x}{y} + \frac{1}{{y}^{2}}\right)} \]
    4. Simplified1.6

      \[\leadsto \color{blue}{\left(x + \frac{x}{y \cdot y}\right) - \left(\frac{1}{y \cdot y} + \frac{x + -1}{y}\right)} \]
    5. Applied pow2_binary641.6

      \[\leadsto \left(x + \frac{x}{y \cdot y}\right) - \left(\frac{1}{\color{blue}{{y}^{2}}} + \frac{x + -1}{y}\right) \]
    6. Applied pow-flip_binary641.6

      \[\leadsto \left(x + \frac{x}{y \cdot y}\right) - \left(\color{blue}{{y}^{\left(-2\right)}} + \frac{x + -1}{y}\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{\left(1 - x\right) \cdot y}{1 + y} \leq 1.3651577721425698 \cdot 10^{-6} \lor \neg \left(\frac{\left(1 - x\right) \cdot y}{1 + y} \leq 1\right):\\ \;\;\;\;\mathsf{fma}\left(\frac{y}{1 + y}, x - 1, 1\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x + \frac{x}{y \cdot y}\right) - \left({y}^{-2} + \frac{x - 1}{y}\right)\\ \end{array} \]

Reproduce

herbie shell --seed 2021313 
(FPCore (x y)
  :name "Diagrams.Trail:splitAtParam  from diagrams-lib-1.3.0.3, D"
  :precision binary64

  :herbie-target
  (if (< y -3693.8482788297247) (- (/ 1.0 y) (- (/ x y) x)) (if (< y 6799310503.41891) (- 1.0 (/ (* (- 1.0 x) y) (+ y 1.0))) (- (/ 1.0 y) (- (/ x y) x))))

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