Average Error: 3.5 → 0.8
Time: 7.2s
Precision: binary64
Cost: 14472
\[x \cdot \left(1 - \left(1 - y\right) \cdot z\right) \]
\[\begin{array}{l} t_0 := x \cdot \left(1 + z \cdot \left(y + -1\right)\right)\\ \mathbf{if}\;t_0 \leq -5 \cdot 10^{-82}:\\ \;\;\;\;\mathsf{fma}\left(x \cdot z, y + -1, x\right)\\ \mathbf{elif}\;t_0 \leq 4 \cdot 10^{+286}:\\ \;\;\;\;\mathsf{fma}\left(x, \mathsf{fma}\left(y, z, -z\right), x\right)\\ \mathbf{else}:\\ \;\;\;\;z \cdot \left(x \cdot \left(y + -1\right)\right)\\ \end{array} \]
(FPCore (x y z) :precision binary64 (* x (- 1.0 (* (- 1.0 y) z))))
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* x (+ 1.0 (* z (+ y -1.0))))))
   (if (<= t_0 -5e-82)
     (fma (* x z) (+ y -1.0) x)
     (if (<= t_0 4e+286) (fma x (fma y z (- z)) x) (* z (* x (+ y -1.0)))))))
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 + (z * (y + -1.0)));
	double tmp;
	if (t_0 <= -5e-82) {
		tmp = fma((x * z), (y + -1.0), x);
	} else if (t_0 <= 4e+286) {
		tmp = fma(x, fma(y, z, -z), x);
	} else {
		tmp = z * (x * (y + -1.0));
	}
	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)
	t_0 = Float64(x * Float64(1.0 + Float64(z * Float64(y + -1.0))))
	tmp = 0.0
	if (t_0 <= -5e-82)
		tmp = fma(Float64(x * z), Float64(y + -1.0), x);
	elseif (t_0 <= 4e+286)
		tmp = fma(x, fma(y, z, Float64(-z)), x);
	else
		tmp = Float64(z * Float64(x * Float64(y + -1.0)));
	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_] := Block[{t$95$0 = N[(x * N[(1.0 + N[(z * N[(y + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -5e-82], N[(N[(x * z), $MachinePrecision] * N[(y + -1.0), $MachinePrecision] + x), $MachinePrecision], If[LessEqual[t$95$0, 4e+286], N[(x * N[(y * z + (-z)), $MachinePrecision] + x), $MachinePrecision], N[(z * N[(x * N[(y + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
x \cdot \left(1 - \left(1 - y\right) \cdot z\right)
\begin{array}{l}
t_0 := x \cdot \left(1 + z \cdot \left(y + -1\right)\right)\\
\mathbf{if}\;t_0 \leq -5 \cdot 10^{-82}:\\
\;\;\;\;\mathsf{fma}\left(x \cdot z, y + -1, x\right)\\

\mathbf{elif}\;t_0 \leq 4 \cdot 10^{+286}:\\
\;\;\;\;\mathsf{fma}\left(x, \mathsf{fma}\left(y, z, -z\right), x\right)\\

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


\end{array}

Error

Target

Original3.5
Target0.3
Herbie0.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 3 regimes
  2. if (*.f64 x (-.f64 1 (*.f64 (-.f64 1 y) z))) < -4.9999999999999998e-82

    1. Initial program 4.7

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

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

      \[\leadsto \color{blue}{x - \left(1 - y\right) \cdot \left(z \cdot x\right)} \]
      Proof
      (-.f64 x (*.f64 (-.f64 1 y) (*.f64 z x))): 0 points increase in error, 0 points decrease in error
      (-.f64 (Rewrite<= *-rgt-identity_binary64 (*.f64 x 1)) (*.f64 (-.f64 1 y) (*.f64 z x))): 0 points increase in error, 0 points decrease in error
      (-.f64 (*.f64 x 1) (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 (-.f64 1 y) z) x))): 22 points increase in error, 16 points decrease in error
      (-.f64 (*.f64 x 1) (Rewrite=> *-commutative_binary64 (*.f64 x (*.f64 (-.f64 1 y) z)))): 0 points increase in error, 0 points decrease in error
      (Rewrite=> distribute-lft-out--_binary64 (*.f64 x (-.f64 1 (*.f64 (-.f64 1 y) z)))): 4 points increase in error, 1 points decrease in error
      (Rewrite=> *-commutative_binary64 (*.f64 (-.f64 1 (*.f64 (-.f64 1 y) z)) x)): 0 points increase in error, 0 points decrease in error
      (*.f64 (-.f64 1 (Rewrite<= *-commutative_binary64 (*.f64 z (-.f64 1 y)))) x): 0 points increase in error, 0 points decrease in error
    4. Taylor expanded in x around 0 4.7

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(z \cdot x, -1 + y, x\right)} \]
      Proof
      (fma.f64 (*.f64 z x) (+.f64 -1 y) x): 0 points increase in error, 0 points decrease in error
      (fma.f64 (*.f64 z x) (+.f64 (Rewrite<= metadata-eval (-.f64 0 1)) y) x): 0 points increase in error, 0 points decrease in error
      (fma.f64 (*.f64 z x) (Rewrite<= associate--r-_binary64 (-.f64 0 (-.f64 1 y))) x): 0 points increase in error, 0 points decrease in error
      (fma.f64 (*.f64 z x) (Rewrite<= neg-sub0_binary64 (neg.f64 (-.f64 1 y))) x): 0 points increase in error, 0 points decrease in error
      (Rewrite=> fma-udef_binary64 (+.f64 (*.f64 (*.f64 z x) (neg.f64 (-.f64 1 y))) x)): 1 points increase in error, 2 points decrease in error
      (+.f64 (Rewrite=> *-commutative_binary64 (*.f64 (neg.f64 (-.f64 1 y)) (*.f64 z x))) x): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 (neg.f64 (-.f64 1 y)) z) x)) x): 22 points increase in error, 16 points decrease in error
      (+.f64 (*.f64 (Rewrite=> distribute-lft-neg-out_binary64 (neg.f64 (*.f64 (-.f64 1 y) z))) x) x): 0 points increase in error, 0 points decrease in error
      (Rewrite=> distribute-lft1-in_binary64 (*.f64 (+.f64 (neg.f64 (*.f64 (-.f64 1 y) z)) 1) x)): 4 points increase in error, 1 points decrease in error
      (*.f64 (Rewrite<= +-commutative_binary64 (+.f64 1 (neg.f64 (*.f64 (-.f64 1 y) z)))) x): 0 points increase in error, 0 points decrease in error
      (*.f64 (+.f64 1 (neg.f64 (Rewrite<= *-commutative_binary64 (*.f64 z (-.f64 1 y))))) x): 0 points increase in error, 0 points decrease in error
      (*.f64 (Rewrite<= sub-neg_binary64 (-.f64 1 (*.f64 z (-.f64 1 y)))) x): 0 points increase in error, 0 points decrease in error

    if -4.9999999999999998e-82 < (*.f64 x (-.f64 1 (*.f64 (-.f64 1 y) z))) < 4.00000000000000013e286

    1. Initial program 0.1

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(y, z, -z\right), x\right)} \]
      Proof
      (fma.f64 x (fma.f64 y z (neg.f64 z)) x): 0 points increase in error, 0 points decrease in error
      (fma.f64 x (Rewrite<= fma-def_binary64 (+.f64 (*.f64 y z) (neg.f64 z))) x): 0 points increase in error, 0 points decrease in error
      (fma.f64 x (+.f64 (Rewrite<= remove-double-neg_binary64 (neg.f64 (neg.f64 (*.f64 y z)))) (neg.f64 z)) x): 0 points increase in error, 0 points decrease in error
      (fma.f64 x (+.f64 (neg.f64 (Rewrite<= distribute-lft-neg-out_binary64 (*.f64 (neg.f64 y) z))) (neg.f64 z)) x): 0 points increase in error, 0 points decrease in error
      (fma.f64 x (+.f64 (Rewrite<= distribute-rgt-neg-out_binary64 (*.f64 (neg.f64 y) (neg.f64 z))) (neg.f64 z)) x): 0 points increase in error, 0 points decrease in error
      (fma.f64 x (Rewrite=> distribute-lft1-in_binary64 (*.f64 (+.f64 (neg.f64 y) 1) (neg.f64 z))) x): 1 points increase in error, 0 points decrease in error
      (fma.f64 x (*.f64 (Rewrite<= +-commutative_binary64 (+.f64 1 (neg.f64 y))) (neg.f64 z)) x): 0 points increase in error, 0 points decrease in error
      (fma.f64 x (*.f64 (Rewrite<= sub-neg_binary64 (-.f64 1 y)) (neg.f64 z)) x): 0 points increase in error, 0 points decrease in error
      (fma.f64 x (Rewrite<= distribute-rgt-neg-in_binary64 (neg.f64 (*.f64 (-.f64 1 y) z))) x): 0 points increase in error, 0 points decrease in error
      (fma.f64 x (Rewrite<= distribute-lft-neg-out_binary64 (*.f64 (neg.f64 (-.f64 1 y)) z)) x): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 x (*.f64 (neg.f64 (-.f64 1 y)) z)) x)): 1 points increase in error, 2 points decrease in error
      (+.f64 (*.f64 x (*.f64 (neg.f64 (-.f64 1 y)) z)) (Rewrite<= *-rgt-identity_binary64 (*.f64 x 1))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= distribute-lft-in_binary64 (*.f64 x (+.f64 (*.f64 (neg.f64 (-.f64 1 y)) z) 1))): 4 points increase in error, 1 points decrease in error
      (*.f64 x (Rewrite<= +-commutative_binary64 (+.f64 1 (*.f64 (neg.f64 (-.f64 1 y)) z)))): 0 points increase in error, 0 points decrease in error
      (*.f64 x (Rewrite<= cancel-sign-sub-inv_binary64 (-.f64 1 (*.f64 (-.f64 1 y) z)))): 0 points increase in error, 0 points decrease in error

    if 4.00000000000000013e286 < (*.f64 x (-.f64 1 (*.f64 (-.f64 1 y) z)))

    1. Initial program 38.3

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

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

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

Alternatives

Alternative 1
Error21.5
Cost980
\[\begin{array}{l} t_0 := x \cdot \left(-z\right)\\ \mathbf{if}\;z \leq -6.5 \cdot 10^{+177}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq -1.2 \cdot 10^{+40}:\\ \;\;\;\;y \cdot \left(x \cdot z\right)\\ \mathbf{elif}\;z \leq -1:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.6 \cdot 10^{-106}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 8.5 \cdot 10^{+20}:\\ \;\;\;\;x \cdot \left(y \cdot z\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error21.5
Cost980
\[\begin{array}{l} t_0 := x \cdot \left(-z\right)\\ \mathbf{if}\;z \leq -1.25 \cdot 10^{+178}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq -1.65 \cdot 10^{+40}:\\ \;\;\;\;z \cdot \left(x \cdot y\right)\\ \mathbf{elif}\;z \leq -1:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.1 \cdot 10^{-106}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 5.6 \cdot 10^{+20}:\\ \;\;\;\;x \cdot \left(y \cdot z\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error10.4
Cost976
\[\begin{array}{l} t_0 := z \cdot \left(x \cdot \left(y + -1\right)\right)\\ \mathbf{if}\;z \leq -1.3 \cdot 10^{-27}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.6 \cdot 10^{-106}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 3.6 \cdot 10^{-56}:\\ \;\;\;\;y \cdot \left(x \cdot z\right)\\ \mathbf{elif}\;z \leq 3300:\\ \;\;\;\;x - x \cdot z\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Error0.2
Cost840
\[\begin{array}{l} t_0 := z \cdot \left(x \cdot \left(y + -1\right)\right)\\ \mathbf{if}\;z \leq -1.1 \cdot 10^{+14}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 8200000:\\ \;\;\;\;x \cdot \left(1 + z \cdot \left(y + -1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error0.2
Cost840
\[\begin{array}{l} t_0 := x + z \cdot \left(x \cdot \left(y + -1\right)\right)\\ \mathbf{if}\;z \leq -4.6 \cdot 10^{-16}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 5 \cdot 10^{-86}:\\ \;\;\;\;x + x \cdot \left(y \cdot z\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error20.4
Cost716
\[\begin{array}{l} t_0 := x \cdot \left(-z\right)\\ \mathbf{if}\;z \leq -1:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.6 \cdot 10^{-106}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 5.7 \cdot 10^{+19}:\\ \;\;\;\;x \cdot \left(y \cdot z\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error1.0
Cost712
\[\begin{array}{l} t_0 := z \cdot \left(x \cdot \left(y + -1\right)\right)\\ \mathbf{if}\;z \leq -0.95:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 4.3 \cdot 10^{-10}:\\ \;\;\;\;x + x \cdot \left(y \cdot z\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error11.6
Cost584
\[\begin{array}{l} \mathbf{if}\;y \leq -3.2 \cdot 10^{+86}:\\ \;\;\;\;z \cdot \left(x \cdot y\right)\\ \mathbf{elif}\;y \leq 2.7 \cdot 10^{+19}:\\ \;\;\;\;x \cdot \left(1 - z\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(x \cdot z\right)\\ \end{array} \]
Alternative 9
Error19.1
Cost520
\[\begin{array}{l} t_0 := x \cdot \left(-z\right)\\ \mathbf{if}\;z \leq -1:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 4.3 \cdot 10^{-10}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 10
Error32.7
Cost64
\[x \]

Error

Reproduce

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