Average Error: 10.4 → 1.9
Time: 3.6s
Precision: 64
\[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}\]
\[\begin{array}{l} \mathbf{if}\;z \le -1.03093838410670302 \cdot 10^{-10}:\\ \;\;\;\;\frac{x}{\frac{z}{\left(y - z\right) + 1}}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(1, \frac{x}{z}, \frac{x \cdot y}{z}\right) - x\\ \end{array}\]
\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}
\begin{array}{l}
\mathbf{if}\;z \le -1.03093838410670302 \cdot 10^{-10}:\\
\;\;\;\;\frac{x}{\frac{z}{\left(y - z\right) + 1}}\\

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

\end{array}
double f(double x, double y, double z) {
        double r595298 = x;
        double r595299 = y;
        double r595300 = z;
        double r595301 = r595299 - r595300;
        double r595302 = 1.0;
        double r595303 = r595301 + r595302;
        double r595304 = r595298 * r595303;
        double r595305 = r595304 / r595300;
        return r595305;
}

double f(double x, double y, double z) {
        double r595306 = z;
        double r595307 = -1.030938384106703e-10;
        bool r595308 = r595306 <= r595307;
        double r595309 = x;
        double r595310 = y;
        double r595311 = r595310 - r595306;
        double r595312 = 1.0;
        double r595313 = r595311 + r595312;
        double r595314 = r595306 / r595313;
        double r595315 = r595309 / r595314;
        double r595316 = r595309 / r595306;
        double r595317 = r595309 * r595310;
        double r595318 = r595317 / r595306;
        double r595319 = fma(r595312, r595316, r595318);
        double r595320 = r595319 - r595309;
        double r595321 = r595308 ? r595315 : r595320;
        return r595321;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original10.4
Target0.4
Herbie1.9
\[\begin{array}{l} \mathbf{if}\;x \lt -2.7148310671343599 \cdot 10^{-162}:\\ \;\;\;\;\left(1 + y\right) \cdot \frac{x}{z} - x\\ \mathbf{elif}\;x \lt 3.87410881643954616 \cdot 10^{-197}:\\ \;\;\;\;\left(x \cdot \left(\left(y - z\right) + 1\right)\right) \cdot \frac{1}{z}\\ \mathbf{else}:\\ \;\;\;\;\left(1 + y\right) \cdot \frac{x}{z} - x\\ \end{array}\]

Derivation

  1. Split input into 2 regimes
  2. if z < -1.030938384106703e-10

    1. Initial program 16.6

      \[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}\]
    2. Using strategy rm
    3. Applied associate-/l*0.1

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

    if -1.030938384106703e-10 < z

    1. Initial program 7.5

      \[\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}\]
    2. Using strategy rm
    3. Applied *-un-lft-identity7.5

      \[\leadsto \frac{x \cdot \left(\left(y - z\right) + 1\right)}{\color{blue}{1 \cdot z}}\]
    4. Applied times-frac5.0

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

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

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

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

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

Reproduce

herbie shell --seed 2020036 +o rules:numerics
(FPCore (x y z)
  :name "Diagrams.TwoD.Segment.Bernstein:evaluateBernstein from diagrams-lib-1.3.0.3"
  :precision binary64

  :herbie-target
  (if (< x -2.71483106713436e-162) (- (* (+ 1 y) (/ x z)) x) (if (< x 3.874108816439546e-197) (* (* x (+ (- y z) 1)) (/ 1 z)) (- (* (+ 1 y) (/ x z)) x)))

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