Data.Colour.RGBSpace.HSV:hsv from colour-2.3.3, J

?

Percentage Accurate: 96.0% → 99.9%
Time: 10.7s
Precision: binary64
Cost: 8137

?

\[x \cdot \left(1 - \left(1 - y\right) \cdot z\right) \]
\[\begin{array}{l} t_0 := x \cdot \left(1 - \left(1 - y\right) \cdot z\right)\\ \mathbf{if}\;t_0 \leq -1 \cdot 10^{-29} \lor \neg \left(t_0 \leq 10^{+226}\right):\\ \;\;\;\;\mathsf{fma}\left(y + -1, x \cdot z, 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 (* x (- 1.0 (* (- 1.0 y) z)))))
   (if (or (<= t_0 -1e-29) (not (<= t_0 1e+226)))
     (fma (+ y -1.0) (* x 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 = x * (1.0 - ((1.0 - y) * z));
	double tmp;
	if ((t_0 <= -1e-29) || !(t_0 <= 1e+226)) {
		tmp = fma((y + -1.0), (x * 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(x * Float64(1.0 - Float64(Float64(1.0 - y) * z)))
	tmp = 0.0
	if ((t_0 <= -1e-29) || !(t_0 <= 1e+226))
		tmp = fma(Float64(y + -1.0), Float64(x * 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[(x * N[(1.0 - N[(N[(1.0 - y), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -1e-29], N[Not[LessEqual[t$95$0, 1e+226]], $MachinePrecision]], N[(N[(y + -1.0), $MachinePrecision] * N[(x * z), $MachinePrecision] + x), $MachinePrecision], t$95$0]]
x \cdot \left(1 - \left(1 - y\right) \cdot z\right)
\begin{array}{l}
t_0 := x \cdot \left(1 - \left(1 - y\right) \cdot z\right)\\
\mathbf{if}\;t_0 \leq -1 \cdot 10^{-29} \lor \neg \left(t_0 \leq 10^{+226}\right):\\
\;\;\;\;\mathsf{fma}\left(y + -1, x \cdot z, x\right)\\

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


\end{array}

Local Percentage Accuracy?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Herbie found 10 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Target

Original96.0%
Target99.7%
Herbie99.9%
\[\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))) < -9.99999999999999943e-30 or 9.99999999999999961e225 < (*.f64 x (-.f64 1 (*.f64 (-.f64 1 y) z)))

    1. Initial program 92.4%

      \[x \cdot \left(1 - \left(1 - y\right) \cdot z\right) \]
    2. Simplified99.9%

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

      [Start]92.4

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

      distribute-rgt-out-- [<=]92.4

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

      *-lft-identity [=>]92.4

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

      cancel-sign-sub-inv [=>]92.4

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

      +-commutative [=>]92.4

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

      distribute-lft-neg-in [=>]92.4

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

      associate-*l* [=>]99.9

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

      fma-def [=>]99.9

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

      neg-sub0 [=>]99.9

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

      associate--r- [=>]99.9

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

      metadata-eval [=>]99.9

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

      +-commutative [=>]99.9

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

      *-commutative [<=]99.9

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

    if -9.99999999999999943e-30 < (*.f64 x (-.f64 1 (*.f64 (-.f64 1 y) z))) < 9.99999999999999961e225

    1. Initial program 99.9%

      \[x \cdot \left(1 - \left(1 - y\right) \cdot z\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.9%

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

Alternatives

Alternative 1
Accuracy99.7%
Cost1353
\[\begin{array}{l} t_0 := \left(1 - y\right) \cdot z\\ \mathbf{if}\;t_0 \leq -\infty \lor \neg \left(t_0 \leq 4 \cdot 10^{+303}\right):\\ \;\;\;\;y \cdot \left(x \cdot z\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(1 - t_0\right)\\ \end{array} \]
Alternative 2
Accuracy99.7%
Cost1352
\[\begin{array}{l} t_0 := \left(1 - y\right) \cdot z\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;y \cdot \left(x \cdot z\right)\\ \mathbf{elif}\;t_0 \leq 5 \cdot 10^{+128}:\\ \;\;\;\;x \cdot \left(1 - t_0\right)\\ \mathbf{else}:\\ \;\;\;\;x - z \cdot \left(x \cdot \left(1 - y\right)\right)\\ \end{array} \]
Alternative 3
Accuracy84.5%
Cost848
\[\begin{array}{l} t_0 := y \cdot \left(x \cdot z\right)\\ t_1 := x - x \cdot z\\ \mathbf{if}\;y \leq -1.85 \cdot 10^{+137}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq -1.3 \cdot 10^{+94}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq -3.3 \cdot 10^{+40}:\\ \;\;\;\;x \cdot \left(y \cdot z\right)\\ \mathbf{elif}\;y \leq 3.5 \cdot 10^{+18}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Accuracy66.6%
Cost716
\[\begin{array}{l} t_0 := y \cdot \left(x \cdot z\right)\\ \mathbf{if}\;z \leq -5.15 \cdot 10^{-11}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.8 \cdot 10^{-18}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 7.8 \cdot 10^{+104}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(-z\right)\\ \end{array} \]
Alternative 5
Accuracy66.4%
Cost716
\[\begin{array}{l} \mathbf{if}\;z \leq -1.38 \cdot 10^{-11}:\\ \;\;\;\;y \cdot \left(x \cdot z\right)\\ \mathbf{elif}\;z \leq 5.5 \cdot 10^{-17}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 7 \cdot 10^{+104}:\\ \;\;\;\;z \cdot \left(x \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(-z\right)\\ \end{array} \]
Alternative 6
Accuracy95.0%
Cost713
\[\begin{array}{l} \mathbf{if}\;y \leq -440000000 \lor \neg \left(y \leq 1\right):\\ \;\;\;\;x + z \cdot \left(x \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;x - x \cdot z\\ \end{array} \]
Alternative 7
Accuracy95.1%
Cost712
\[\begin{array}{l} \mathbf{if}\;y \leq -440000000:\\ \;\;\;\;x + z \cdot \left(x \cdot y\right)\\ \mathbf{elif}\;y \leq 1:\\ \;\;\;\;x - x \cdot z\\ \mathbf{else}:\\ \;\;\;\;x + x \cdot \left(y \cdot z\right)\\ \end{array} \]
Alternative 8
Accuracy65.2%
Cost521
\[\begin{array}{l} \mathbf{if}\;z \leq -100000000 \lor \neg \left(z \leq 1.12 \cdot 10^{+15}\right):\\ \;\;\;\;x \cdot \left(-z\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 9
Accuracy39.5%
Cost64
\[x \]

Reproduce?

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