Average Error: 3.5 → 0.2
Time: 7.7s
Precision: binary64
Cost: 7113
\[x \cdot \left(1 - \left(1 - y\right) \cdot z\right) \]
\[\begin{array}{l} \mathbf{if}\;x \leq -2 \cdot 10^{+49} \lor \neg \left(x \leq 10^{-31}\right):\\ \;\;\;\;x \cdot \left(1 + z \cdot \left(y + -1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(z, x \cdot \left(y + -1\right), x\right)\\ \end{array} \]
(FPCore (x y z) :precision binary64 (* x (- 1.0 (* (- 1.0 y) z))))
(FPCore (x y z)
 :precision binary64
 (if (or (<= x -2e+49) (not (<= x 1e-31)))
   (* x (+ 1.0 (* z (+ y -1.0))))
   (fma z (* x (+ y -1.0)) x)))
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 <= -2e+49) || !(x <= 1e-31)) {
		tmp = x * (1.0 + (z * (y + -1.0)));
	} else {
		tmp = fma(z, (x * (y + -1.0)), x);
	}
	return tmp;
}
function code(x, y, z)
	return Float64(x * Float64(1.0 - Float64(Float64(1.0 - y) * z)))
end
function code(x, y, z)
	tmp = 0.0
	if ((x <= -2e+49) || !(x <= 1e-31))
		tmp = Float64(x * Float64(1.0 + Float64(z * Float64(y + -1.0))));
	else
		tmp = fma(z, Float64(x * Float64(y + -1.0)), x);
	end
	return tmp
end
code[x_, y_, z_] := N[(x * N[(1.0 - N[(N[(1.0 - y), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_] := If[Or[LessEqual[x, -2e+49], N[Not[LessEqual[x, 1e-31]], $MachinePrecision]], N[(x * N[(1.0 + N[(z * N[(y + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(z * N[(x * N[(y + -1.0), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision]]
x \cdot \left(1 - \left(1 - y\right) \cdot z\right)
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{+49} \lor \neg \left(x \leq 10^{-31}\right):\\
\;\;\;\;x \cdot \left(1 + z \cdot \left(y + -1\right)\right)\\

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


\end{array}

Error

Target

Original3.5
Target0.2
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 x < -1.99999999999999989e49 or 1e-31 < x

    1. Initial program 0.1

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

    if -1.99999999999999989e49 < x < 1e-31

    1. Initial program 5.6

      \[x \cdot \left(1 - \left(1 - y\right) \cdot z\right) \]
    2. Taylor expanded in x around 0 5.6

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, x \cdot \left(y + -1\right), x\right)} \]
      Proof
      (fma.f64 z (*.f64 x (+.f64 y -1)) x): 0 points increase in error, 0 points decrease in error
      (fma.f64 z (*.f64 x (+.f64 y (Rewrite<= metadata-eval (neg.f64 1)))) x): 13 points increase in error, 0 points decrease in error
      (fma.f64 z (*.f64 x (Rewrite<= sub-neg_binary64 (-.f64 y 1))) x): 0 points increase in error, 13 points decrease in error
      (fma.f64 z (Rewrite<= *-commutative_binary64 (*.f64 (-.f64 y 1) x)) x): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 z (*.f64 (-.f64 y 1) x)) x)): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite=> associate-*r*_binary64 (*.f64 (*.f64 z (-.f64 y 1)) x)) x): 0 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 (*.f64 z (Rewrite=> sub-neg_binary64 (+.f64 y (neg.f64 1)))) x) x): 13 points increase in error, 0 points decrease in error
      (+.f64 (*.f64 (*.f64 z (+.f64 y (Rewrite=> metadata-eval -1))) x) x): 5 points increase in error, 13 points decrease in error
      (Rewrite=> distribute-lft1-in_binary64 (*.f64 (+.f64 (*.f64 z (+.f64 y -1)) 1) x)): 0 points increase in error, 5 points decrease in error
      (*.f64 (+.f64 (Rewrite=> distribute-rgt-in_binary64 (+.f64 (*.f64 y z) (*.f64 -1 z))) 1) x): 0 points increase in error, 0 points decrease in error
      (*.f64 (Rewrite<= associate-+r+_binary64 (+.f64 (*.f64 y z) (+.f64 (*.f64 -1 z) 1))) x): 5 points increase in error, 0 points decrease in error
      (*.f64 (+.f64 (*.f64 y z) (+.f64 (Rewrite=> mul-1-neg_binary64 (neg.f64 z)) 1)) x): 0 points increase in error, 5 points decrease in error
      (*.f64 (+.f64 (*.f64 y z) (Rewrite<= +-commutative_binary64 (+.f64 1 (neg.f64 z)))) x): 13 points increase in error, 0 points decrease in error
      (*.f64 (+.f64 (*.f64 y z) (Rewrite<= sub-neg_binary64 (-.f64 1 z))) x): 0 points increase in error, 13 points decrease in error
      (*.f64 (Rewrite<= +-commutative_binary64 (+.f64 (-.f64 1 z) (*.f64 y z))) x): 5 points increase in error, 0 points decrease in error
      (*.f64 (Rewrite=> associate-+l-_binary64 (-.f64 1 (-.f64 z (*.f64 y z)))) x): 0 points increase in error, 5 points decrease in error
      (*.f64 (-.f64 1 (-.f64 (Rewrite<= *-lft-identity_binary64 (*.f64 1 z)) (*.f64 y z))) x): 13 points increase in error, 0 points decrease in error
      (*.f64 (-.f64 1 (Rewrite=> distribute-rgt-out--_binary64 (*.f64 z (-.f64 1 y)))) x): 0 points increase in error, 13 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification0.2

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

Alternatives

Alternative 1
Error0.2
Cost841
\[\begin{array}{l} \mathbf{if}\;x \leq -1 \cdot 10^{+45} \lor \neg \left(x \leq 1.56 \cdot 10^{-31}\right):\\ \;\;\;\;x \cdot \left(1 + z \cdot \left(y + -1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x + z \cdot \left(x \cdot \left(y + -1\right)\right)\\ \end{array} \]
Alternative 2
Error15.0
Cost717
\[\begin{array}{l} \mathbf{if}\;y \leq 2.25 \cdot 10^{+21}:\\ \;\;\;\;x \cdot \left(1 - z\right)\\ \mathbf{elif}\;y \leq 7 \cdot 10^{+81} \lor \neg \left(y \leq 7.2 \cdot 10^{+113}\right):\\ \;\;\;\;y \cdot \left(x \cdot z\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 3
Error15.1
Cost716
\[\begin{array}{l} \mathbf{if}\;y \leq 3.3 \cdot 10^{-6}:\\ \;\;\;\;x \cdot \left(1 - z\right)\\ \mathbf{elif}\;y \leq 8.2 \cdot 10^{+81}:\\ \;\;\;\;x \cdot \left(z \cdot \left(y + -1\right)\right)\\ \mathbf{elif}\;y \leq 5.5 \cdot 10^{+111}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(x \cdot z\right)\\ \end{array} \]
Alternative 4
Error9.4
Cost713
\[\begin{array}{l} \mathbf{if}\;z \leq -3700 \lor \neg \left(z \leq 1.05 \cdot 10^{+15}\right):\\ \;\;\;\;z \cdot \left(x \cdot \left(y + -1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(1 - z\right)\\ \end{array} \]
Alternative 5
Error0.8
Cost713
\[\begin{array}{l} \mathbf{if}\;z \leq -1 \lor \neg \left(z \leq 1\right):\\ \;\;\;\;z \cdot \left(x \cdot \left(y + -1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x + x \cdot \left(y \cdot z\right)\\ \end{array} \]
Alternative 6
Error1.7
Cost708
\[\begin{array}{l} \mathbf{if}\;z \leq -360000000000:\\ \;\;\;\;z \cdot \left(x \cdot \left(y + -1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(1 + z \cdot \left(y + -1\right)\right)\\ \end{array} \]
Alternative 7
Error19.5
Cost521
\[\begin{array}{l} \mathbf{if}\;z \leq -1 \lor \neg \left(z \leq 1\right):\\ \;\;\;\;x \cdot \left(-z\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 8
Error33.7
Cost64
\[x \]

Error

Reproduce

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