Average Error: 3.5 → 0.2
Time: 5.4s
Precision: binary64
\[x \cdot \left(1 - \left(1 - y\right) \cdot z\right) \]
\[\begin{array}{l} \mathbf{if}\;\begin{array}{l} t_0 := x \cdot \left(1 - \left(1 - y\right) \cdot z\right)\\ t_0 \leq -2.6255077649920472 \cdot 10^{+213} \lor \neg \left(t_0 \leq 6.795679507506787 \cdot 10^{-67}\right) \end{array}:\\ \;\;\;\;\mathsf{fma}\left(x \cdot z, y, x\right) - x \cdot z\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(\left(1 + y \cdot z\right) - z\right)\\ \end{array} \]
x \cdot \left(1 - \left(1 - y\right) \cdot z\right)
\begin{array}{l}
\mathbf{if}\;\begin{array}{l}
t_0 := x \cdot \left(1 - \left(1 - y\right) \cdot z\right)\\
t_0 \leq -2.6255077649920472 \cdot 10^{+213} \lor \neg \left(t_0 \leq 6.795679507506787 \cdot 10^{-67}\right)
\end{array}:\\
\;\;\;\;\mathsf{fma}\left(x \cdot z, y, x\right) - x \cdot z\\

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


\end{array}
(FPCore (x y z) :precision binary64 (* x (- 1.0 (* (- 1.0 y) z))))
(FPCore (x y z)
 :precision binary64
 (if (let* ((t_0 (* x (- 1.0 (* (- 1.0 y) z)))))
       (or (<= t_0 -2.6255077649920472e+213)
           (not (<= t_0 6.795679507506787e-67))))
   (- (fma (* x z) y x) (* x z))
   (* x (- (+ 1.0 (* y z)) z))))
double code(double x, double y, double z) {
	return x * (1.0 - ((1.0 - y) * z));
}
double code(double x, double y, double z) {
	double t_0 = x * (1.0 - ((1.0 - y) * z));
	double tmp;
	if ((t_0 <= -2.6255077649920472e+213) || !(t_0 <= 6.795679507506787e-67)) {
		tmp = fma((x * z), y, x) - (x * z);
	} else {
		tmp = x * ((1.0 + (y * z)) - z);
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original3.5
Target0.3
Herbie0.2
\[\begin{array}{l} \mathbf{if}\;x \cdot \left(1 - \left(1 - y\right) \cdot z\right) < -1.618195973607049 \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) < 3.892237649663903 \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 (*.f64 x (-.f64 1 (*.f64 (-.f64 1 y) z))) < -2.6255077649920472e213 or 6.79567950750678696e-67 < (*.f64 x (-.f64 1 (*.f64 (-.f64 1 y) z)))

    1. Initial program 7.3

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

      \[\leadsto \color{blue}{x \cdot \left(\mathsf{fma}\left(y, z, 1\right) - z\right)} \]
    3. Taylor expanded in y around 0 0.2

      \[\leadsto \color{blue}{\left(y \cdot \left(z \cdot x\right) + x\right) - z \cdot x} \]
    4. Taylor expanded in y around inf 0.2

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

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

    if -2.6255077649920472e213 < (*.f64 x (-.f64 1 (*.f64 (-.f64 1 y) z))) < 6.79567950750678696e-67

    1. Initial program 0.1

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

      \[\leadsto \color{blue}{x \cdot \left(\mathsf{fma}\left(y, z, 1\right) - z\right)} \]
    3. Taylor expanded in y around 0 2.8

      \[\leadsto \color{blue}{\left(y \cdot \left(z \cdot x\right) + x\right) - z \cdot x} \]
    4. Taylor expanded in x around -inf 0.1

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

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

Reproduce

herbie shell --seed 2021280 
(FPCore (x y z)
  :name "Data.Colour.RGBSpace.HSV:hsv from colour-2.3.3, J"
  :precision binary64

  :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))))