Average Error: 3.6 → 1.8
Time: 7.6s
Precision: binary64
\[x \cdot \left(1 - \left(1 - y\right) \cdot z\right) \]
\[\begin{array}{l} \mathbf{if}\;x \cdot \left(1 - \left(1 - y\right) \cdot z\right) \leq 2.605862687182587 \cdot 10^{-59}:\\ \;\;\;\;x \cdot \left(\mathsf{fma}\left(y, z, 1\right) - z\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x + y \cdot \left(x \cdot z\right)\right) - x \cdot z\\ \end{array} \]
x \cdot \left(1 - \left(1 - y\right) \cdot z\right)
\begin{array}{l}
\mathbf{if}\;x \cdot \left(1 - \left(1 - y\right) \cdot z\right) \leq 2.605862687182587 \cdot 10^{-59}:\\
\;\;\;\;x \cdot \left(\mathsf{fma}\left(y, z, 1\right) - z\right)\\

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


\end{array}
(FPCore (x y z) :precision binary64 (* x (- 1.0 (* (- 1.0 y) z))))
(FPCore (x y z)
 :precision binary64
 (if (<= (* x (- 1.0 (* (- 1.0 y) z))) 2.605862687182587e-59)
   (* x (- (fma y z 1.0) z))
   (- (+ x (* y (* x z))) (* x 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 tmp;
	if ((x * (1.0 - ((1.0 - y) * z))) <= 2.605862687182587e-59) {
		tmp = x * (fma(y, z, 1.0) - z);
	} else {
		tmp = (x + (y * (x * z))) - (x * z);
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Bits error versus z

Target

Original3.6
Target0.2
Herbie1.8
\[\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.6058626871825871e-59

    1. Initial program 2.7

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

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

    if 2.6058626871825871e-59 < (*.f64 x (-.f64 1 (*.f64 (-.f64 1 y) z)))

    1. Initial program 5.1

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

      \[\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} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification1.8

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

Reproduce

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