Average Error: 7.5 → 0.1
Time: 4.7s
Precision: binary64
\[\frac{x + y}{1 - \frac{y}{z}} \]
\[\begin{array}{l} t_0 := 1 - \frac{y}{z}\\ t_1 := \frac{x + y}{t_0}\\ t_2 := \frac{x}{t_0} + \frac{y}{t_0}\\ \mathbf{if}\;t_1 \leq -2.117151647904118 \cdot 10^{-275}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t_1 \leq 0:\\ \;\;\;\;-\mathsf{fma}\left(\frac{x}{y}, z, z\right)\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
\frac{x + y}{1 - \frac{y}{z}}
\begin{array}{l}
t_0 := 1 - \frac{y}{z}\\
t_1 := \frac{x + y}{t_0}\\
t_2 := \frac{x}{t_0} + \frac{y}{t_0}\\
\mathbf{if}\;t_1 \leq -2.117151647904118 \cdot 10^{-275}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;t_1 \leq 0:\\
\;\;\;\;-\mathsf{fma}\left(\frac{x}{y}, z, z\right)\\

\mathbf{else}:\\
\;\;\;\;t_2\\


\end{array}
(FPCore (x y z) :precision binary64 (/ (+ x y) (- 1.0 (/ y z))))
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (- 1.0 (/ y z)))
        (t_1 (/ (+ x y) t_0))
        (t_2 (+ (/ x t_0) (/ y t_0))))
   (if (<= t_1 -2.117151647904118e-275)
     t_2
     (if (<= t_1 0.0) (- (fma (/ x y) z z)) t_2))))
double code(double x, double y, double z) {
	return (x + y) / (1.0 - (y / z));
}
double code(double x, double y, double z) {
	double t_0 = 1.0 - (y / z);
	double t_1 = (x + y) / t_0;
	double t_2 = (x / t_0) + (y / t_0);
	double tmp;
	if (t_1 <= -2.117151647904118e-275) {
		tmp = t_2;
	} else if (t_1 <= 0.0) {
		tmp = -fma((x / y), z, z);
	} else {
		tmp = t_2;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original7.5
Target4.0
Herbie0.1
\[\begin{array}{l} \mathbf{if}\;y < -3.7429310762689856 \cdot 10^{+171}:\\ \;\;\;\;\frac{y + x}{-y} \cdot z\\ \mathbf{elif}\;y < 3.5534662456086734 \cdot 10^{+168}:\\ \;\;\;\;\frac{x + y}{1 - \frac{y}{z}}\\ \mathbf{else}:\\ \;\;\;\;\frac{y + x}{-y} \cdot z\\ \end{array} \]

Derivation

  1. Split input into 2 regimes
  2. if (/.f64 (+.f64 x y) (-.f64 1 (/.f64 y z))) < -2.117151647904118e-275 or -0.0 < (/.f64 (+.f64 x y) (-.f64 1 (/.f64 y z)))

    1. Initial program 0.1

      \[\frac{x + y}{1 - \frac{y}{z}} \]
    2. Taylor expanded in x around 0 0.1

      \[\leadsto \color{blue}{\frac{x}{1 - \frac{y}{z}} + \frac{y}{1 - \frac{y}{z}}} \]

    if -2.117151647904118e-275 < (/.f64 (+.f64 x y) (-.f64 1 (/.f64 y z))) < -0.0

    1. Initial program 57.9

      \[\frac{x + y}{1 - \frac{y}{z}} \]
    2. Taylor expanded in y around inf 0.9

      \[\leadsto \color{blue}{-\left(\frac{z \cdot x}{y} + \left(\frac{{z}^{2}}{y} + z\right)\right)} \]
    3. Simplified9.8

      \[\leadsto \color{blue}{-\mathsf{fma}\left(\frac{z}{y}, x, z + \frac{z}{\frac{y}{z}}\right)} \]
    4. Taylor expanded in z around 0 0.5

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

      \[\leadsto -\color{blue}{\mathsf{fma}\left(\frac{x}{y}, z, z\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x + y}{1 - \frac{y}{z}} \leq -2.117151647904118 \cdot 10^{-275}:\\ \;\;\;\;\frac{x}{1 - \frac{y}{z}} + \frac{y}{1 - \frac{y}{z}}\\ \mathbf{elif}\;\frac{x + y}{1 - \frac{y}{z}} \leq 0:\\ \;\;\;\;-\mathsf{fma}\left(\frac{x}{y}, z, z\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{1 - \frac{y}{z}} + \frac{y}{1 - \frac{y}{z}}\\ \end{array} \]

Reproduce

herbie shell --seed 2022130 
(FPCore (x y z)
  :name "Graphics.Rendering.Chart.Backend.Diagrams:calcFontMetrics from Chart-diagrams-1.5.1, A"
  :precision binary64

  :herbie-target
  (if (< y -3.7429310762689856e+171) (* (/ (+ y x) (- y)) z) (if (< y 3.5534662456086734e+168) (/ (+ x y) (- 1.0 (/ y z))) (* (/ (+ y x) (- y)) z)))

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