Average Error: 7.0 → 0.2
Time: 3.1s
Precision: binary64
\[\frac{x + y}{1 - \frac{y}{z}} \]
\[\begin{array}{l} t_0 := \frac{x + y}{1 - \frac{y}{z}}\\ \mathbf{if}\;t_0 \leq -3.266669259955589 \cdot 10^{-255} \lor \neg \left(t_0 \leq 0\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;-\mathsf{fma}\left(\frac{x}{y}, z, z\right)\\ \end{array} \]
\frac{x + y}{1 - \frac{y}{z}}
\begin{array}{l}
t_0 := \frac{x + y}{1 - \frac{y}{z}}\\
\mathbf{if}\;t_0 \leq -3.266669259955589 \cdot 10^{-255} \lor \neg \left(t_0 \leq 0\right):\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;-\mathsf{fma}\left(\frac{x}{y}, z, z\right)\\


\end{array}
(FPCore (x y z) :precision binary64 (/ (+ x y) (- 1.0 (/ y z))))
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (/ (+ x y) (- 1.0 (/ y z)))))
   (if (or (<= t_0 -3.266669259955589e-255) (not (<= t_0 0.0)))
     t_0
     (- (fma (/ x y) z z)))))
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 = (x + y) / (1.0 - (y / z));
	double tmp;
	if ((t_0 <= -3.266669259955589e-255) || !(t_0 <= 0.0)) {
		tmp = t_0;
	} else {
		tmp = -fma((x / y), z, z);
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original7.0
Target4.0
Herbie0.2
\[\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))) < -3.2666692599555891e-255 or -0.0 < (/.f64 (+.f64 x y) (-.f64 1 (/.f64 y z)))

    1. Initial program 0.1

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

    if -3.2666692599555891e-255 < (/.f64 (+.f64 x y) (-.f64 1 (/.f64 y z))) < -0.0

    1. Initial program 56.0

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

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

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

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

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

Reproduce

herbie shell --seed 2021280 
(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))))