Average Error: 3.4 → 0.2
Time: 14.0s
Precision: 64
\[x \cdot \left(1 - \left(1 - y\right) \cdot z\right)\]
\[\begin{array}{l} \mathbf{if}\;z \le -2.19712079606791561744021540423510388494 \cdot 10^{-16}:\\ \;\;\;\;\mathsf{fma}\left(z, x \cdot \left(y - 1\right), x \cdot 1\right)\\ \mathbf{elif}\;z \le 370701728934447944371113712819372032:\\ \;\;\;\;\mathsf{fma}\left(z, y - 1, 1\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(z, x \cdot \left(y - 1\right), x \cdot 1\right)\\ \end{array}\]
x \cdot \left(1 - \left(1 - y\right) \cdot z\right)
\begin{array}{l}
\mathbf{if}\;z \le -2.19712079606791561744021540423510388494 \cdot 10^{-16}:\\
\;\;\;\;\mathsf{fma}\left(z, x \cdot \left(y - 1\right), x \cdot 1\right)\\

\mathbf{elif}\;z \le 370701728934447944371113712819372032:\\
\;\;\;\;\mathsf{fma}\left(z, y - 1, 1\right) \cdot x\\

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

\end{array}
double f(double x, double y, double z) {
        double r40881323 = x;
        double r40881324 = 1.0;
        double r40881325 = y;
        double r40881326 = r40881324 - r40881325;
        double r40881327 = z;
        double r40881328 = r40881326 * r40881327;
        double r40881329 = r40881324 - r40881328;
        double r40881330 = r40881323 * r40881329;
        return r40881330;
}

double f(double x, double y, double z) {
        double r40881331 = z;
        double r40881332 = -2.1971207960679156e-16;
        bool r40881333 = r40881331 <= r40881332;
        double r40881334 = x;
        double r40881335 = y;
        double r40881336 = 1.0;
        double r40881337 = r40881335 - r40881336;
        double r40881338 = r40881334 * r40881337;
        double r40881339 = r40881334 * r40881336;
        double r40881340 = fma(r40881331, r40881338, r40881339);
        double r40881341 = 3.7070172893444794e+35;
        bool r40881342 = r40881331 <= r40881341;
        double r40881343 = fma(r40881331, r40881337, r40881336);
        double r40881344 = r40881343 * r40881334;
        double r40881345 = r40881342 ? r40881344 : r40881340;
        double r40881346 = r40881333 ? r40881340 : r40881345;
        return r40881346;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original3.4
Target0.2
Herbie0.2
\[\begin{array}{l} \mathbf{if}\;x \cdot \left(1 - \left(1 - y\right) \cdot z\right) \lt -1.618195973607048970493874632750554853795 \cdot 10^{50}:\\ \;\;\;\;x + \left(1 - y\right) \cdot \left(\left(-z\right) \cdot x\right)\\ \mathbf{elif}\;x \cdot \left(1 - \left(1 - y\right) \cdot z\right) \lt 3.892237649663902900973248011051357504727 \cdot 10^{134}:\\ \;\;\;\;\left(x \cdot y\right) \cdot z - \left(x \cdot z - x\right)\\ \mathbf{else}:\\ \;\;\;\;x + \left(1 - y\right) \cdot \left(\left(-z\right) \cdot x\right)\\ \end{array}\]

Derivation

  1. Split input into 2 regimes
  2. if z < -2.1971207960679156e-16 or 3.7070172893444794e+35 < z

    1. Initial program 9.1

      \[x \cdot \left(1 - \left(1 - y\right) \cdot z\right)\]
    2. Simplified9.1

      \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(z, y - 1, 1\right)}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt35.4

      \[\leadsto x \cdot \color{blue}{\left(\sqrt{\mathsf{fma}\left(z, y - 1, 1\right)} \cdot \sqrt{\mathsf{fma}\left(z, y - 1, 1\right)}\right)}\]
    5. Applied associate-*r*35.4

      \[\leadsto \color{blue}{\left(x \cdot \sqrt{\mathsf{fma}\left(z, y - 1, 1\right)}\right) \cdot \sqrt{\mathsf{fma}\left(z, y - 1, 1\right)}}\]
    6. Taylor expanded around inf 9.1

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot z, y, 1 \cdot \left(x - x \cdot z\right)\right)}\]
    8. Taylor expanded around inf 9.1

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

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

    if -2.1971207960679156e-16 < z < 3.7070172893444794e+35

    1. Initial program 0.2

      \[x \cdot \left(1 - \left(1 - y\right) \cdot z\right)\]
    2. Simplified0.2

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

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

Reproduce

herbie shell --seed 2019169 +o rules:numerics
(FPCore (x y z)
  :name "Data.Colour.RGBSpace.HSV:hsv from colour-2.3.3, J"

  :herbie-target
  (if (< (* x (- 1.0 (* (- 1.0 y) z))) -1.618195973607049e+50) (+ x (* (- 1.0 y) (* (- z) x))) (if (< (* x (- 1.0 (* (- 1.0 y) z))) 3.892237649663903e+134) (- (* (* x y) z) (- (* x z) x)) (+ x (* (- 1.0 y) (* (- z) x)))))

  (* x (- 1.0 (* (- 1.0 y) z))))