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

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

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}

Error

Target

Original3.4
Target0.3
Herbie0.1
\[\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 z < -4.99999999999999973e48 or 1.00000000000000009e25 < z

    1. Initial program 9.9

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

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

    if -4.99999999999999973e48 < z < 1.00000000000000009e25

    1. Initial program 0.2

      \[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)): 3 points increase in error, 0 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, 2 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
  3. Recombined 2 regimes into one program.
  4. Final simplification0.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -5 \cdot 10^{+48}:\\ \;\;\;\;z \cdot \left(x \cdot \left(y + -1\right)\right)\\ \mathbf{elif}\;z \leq 10^{+25}:\\ \;\;\;\;\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
Error20.2
Cost916
\[\begin{array}{l} t_0 := -z \cdot x\\ t_1 := x \cdot \left(z \cdot y\right)\\ \mathbf{if}\;z \leq -1.45 \cdot 10^{+55}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq -3.847471682216432 \cdot 10^{-35}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 1.0496899812816544 \cdot 10^{-64}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 2.9926808351893587 \cdot 10^{-51}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 0.47:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error12.8
Cost848
\[\begin{array}{l} t_0 := z \cdot \left(y \cdot x\right)\\ t_1 := x \cdot \left(1 - z\right)\\ \mathbf{if}\;y \leq -4.8 \cdot 10^{+155}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq -3.2 \cdot 10^{+104}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq -1886200314205661.5:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 1.2 \cdot 10^{+106}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(z \cdot y\right)\\ \end{array} \]
Alternative 3
Error12.8
Cost848
\[\begin{array}{l} t_0 := x \cdot \left(1 - z\right)\\ \mathbf{if}\;y \leq -4.8 \cdot 10^{+155}:\\ \;\;\;\;z \cdot \left(y \cdot x\right)\\ \mathbf{elif}\;y \leq -2.4 \cdot 10^{+111}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq -1886200314205661.5:\\ \;\;\;\;y \cdot \left(z \cdot x\right)\\ \mathbf{elif}\;y \leq 1.2 \cdot 10^{+106}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(z \cdot y\right)\\ \end{array} \]
Alternative 4
Error0.1
Cost840
\[\begin{array}{l} t_0 := x + \left(z \cdot x\right) \cdot \left(y + -1\right)\\ \mathbf{if}\;z \leq -1 \cdot 10^{-10}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.3017795830433852 \cdot 10^{-57}:\\ \;\;\;\;x + x \cdot \left(z \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error0.1
Cost840
\[\begin{array}{l} t_0 := x - z \cdot \left(x \cdot \left(1 - y\right)\right)\\ \mathbf{if}\;z \leq -1 \cdot 10^{-10}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 10^{-22}:\\ \;\;\;\;x + x \cdot \left(z \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error0.1
Cost840
\[\begin{array}{l} \mathbf{if}\;z \leq -1 \cdot 10^{-5}:\\ \;\;\;\;x - z \cdot \left(x \cdot \left(1 - y\right)\right)\\ \mathbf{elif}\;z \leq 10^{+25}:\\ \;\;\;\;x \cdot \left(1 + z \cdot \left(y + -1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;z \cdot \left(x \cdot \left(y + -1\right)\right)\\ \end{array} \]
Alternative 7
Error0.1
Cost840
\[\begin{array}{l} t_0 := z \cdot \left(x \cdot \left(y + -1\right)\right)\\ \mathbf{if}\;z \leq -5 \cdot 10^{+48}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 10^{+25}:\\ \;\;\;\;x + x \cdot \left(z \cdot \left(y + -1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error22.0
Cost784
\[\begin{array}{l} \mathbf{if}\;z \leq -3.847471682216432 \cdot 10^{-35}:\\ \;\;\;\;z \cdot \left(y \cdot x\right)\\ \mathbf{elif}\;z \leq 1.0496899812816544 \cdot 10^{-64}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 2.9926808351893587 \cdot 10^{-51}:\\ \;\;\;\;x \cdot \left(z \cdot y\right)\\ \mathbf{elif}\;z \leq 0.47:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;-z \cdot x\\ \end{array} \]
Alternative 9
Error4.1
Cost712
\[\begin{array}{l} t_0 := x \cdot \left(1 + z \cdot y\right)\\ \mathbf{if}\;y \leq -104174.89782619808:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 0.28591349727266757:\\ \;\;\;\;x \cdot \left(1 - z\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 10
Error4.1
Cost712
\[\begin{array}{l} \mathbf{if}\;y \leq -104174.89782619808:\\ \;\;\;\;x \cdot \left(1 + z \cdot y\right)\\ \mathbf{elif}\;y \leq 0.28591349727266757:\\ \;\;\;\;x \cdot \left(1 - z\right)\\ \mathbf{else}:\\ \;\;\;\;x + x \cdot \left(z \cdot y\right)\\ \end{array} \]
Alternative 11
Error0.8
Cost712
\[\begin{array}{l} t_0 := z \cdot \left(x \cdot \left(y + -1\right)\right)\\ \mathbf{if}\;z \leq -1:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 1:\\ \;\;\;\;x + x \cdot \left(z \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 12
Error19.8
Cost520
\[\begin{array}{l} t_0 := -z \cdot x\\ \mathbf{if}\;z \leq -5.5 \cdot 10^{+14}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 0.47:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 13
Error33.7
Cost64
\[x \]

Error

Reproduce

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