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

Percentage Accurate: 95.5% → 95.5%
Time: 4.5s
Alternatives: 5
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ x \cdot \left(1 - y \cdot z\right) \end{array} \]
(FPCore (x y z) :precision binary64 (* x (- 1.0 (* y z))))
double code(double x, double y, double z) {
	return x * (1.0 - (y * z));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x * (1.0d0 - (y * z))
end function
public static double code(double x, double y, double z) {
	return x * (1.0 - (y * z));
}
def code(x, y, z):
	return x * (1.0 - (y * z))
function code(x, y, z)
	return Float64(x * Float64(1.0 - Float64(y * z)))
end
function tmp = code(x, y, z)
	tmp = x * (1.0 - (y * z));
end
code[x_, y_, z_] := N[(x * N[(1.0 - N[(y * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \left(1 - y \cdot z\right)
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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.

Accuracy vs Speed?

Herbie found 5 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 95.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x \cdot \left(1 - y \cdot z\right) \end{array} \]
(FPCore (x y z) :precision binary64 (* x (- 1.0 (* y z))))
double code(double x, double y, double z) {
	return x * (1.0 - (y * z));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x * (1.0d0 - (y * z))
end function
public static double code(double x, double y, double z) {
	return x * (1.0 - (y * z));
}
def code(x, y, z):
	return x * (1.0 - (y * z))
function code(x, y, z)
	return Float64(x * Float64(1.0 - Float64(y * z)))
end
function tmp = code(x, y, z)
	tmp = x * (1.0 - (y * z));
end
code[x_, y_, z_] := N[(x * N[(1.0 - N[(y * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \left(1 - y \cdot z\right)
\end{array}

Alternative 1: 95.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x - x \cdot \left(y \cdot z\right) \end{array} \]
(FPCore (x y z) :precision binary64 (- x (* x (* y z))))
double code(double x, double y, double z) {
	return x - (x * (y * z));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x - (x * (y * z))
end function
public static double code(double x, double y, double z) {
	return x - (x * (y * z));
}
def code(x, y, z):
	return x - (x * (y * z))
function code(x, y, z)
	return Float64(x - Float64(x * Float64(y * z)))
end
function tmp = code(x, y, z)
	tmp = x - (x * (y * z));
end
code[x_, y_, z_] := N[(x - N[(x * N[(y * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - x \cdot \left(y \cdot z\right)
\end{array}
Derivation
  1. Initial program 97.0%

    \[x \cdot \left(1 - y \cdot z\right) \]
  2. Step-by-step derivation
    1. sub-neg97.0%

      \[\leadsto x \cdot \color{blue}{\left(1 + \left(-y \cdot z\right)\right)} \]
    2. distribute-rgt-in97.0%

      \[\leadsto \color{blue}{1 \cdot x + \left(-y \cdot z\right) \cdot x} \]
    3. *-un-lft-identity97.0%

      \[\leadsto \color{blue}{x} + \left(-y \cdot z\right) \cdot x \]
    4. distribute-rgt-neg-in97.0%

      \[\leadsto x + \color{blue}{\left(y \cdot \left(-z\right)\right)} \cdot x \]
  3. Applied egg-rr97.0%

    \[\leadsto \color{blue}{x + \left(y \cdot \left(-z\right)\right) \cdot x} \]
  4. Step-by-step derivation
    1. *-commutative97.0%

      \[\leadsto x + \color{blue}{x \cdot \left(y \cdot \left(-z\right)\right)} \]
    2. associate-*r*92.6%

      \[\leadsto x + \color{blue}{\left(x \cdot y\right) \cdot \left(-z\right)} \]
    3. distribute-rgt-neg-in92.6%

      \[\leadsto x + \color{blue}{\left(-\left(x \cdot y\right) \cdot z\right)} \]
    4. unsub-neg92.6%

      \[\leadsto \color{blue}{x - \left(x \cdot y\right) \cdot z} \]
    5. *-commutative92.6%

      \[\leadsto x - \color{blue}{\left(y \cdot x\right)} \cdot z \]
    6. associate-*r*92.9%

      \[\leadsto x - \color{blue}{y \cdot \left(x \cdot z\right)} \]
    7. *-commutative92.9%

      \[\leadsto x - y \cdot \color{blue}{\left(z \cdot x\right)} \]
  5. Applied egg-rr92.9%

    \[\leadsto \color{blue}{x - y \cdot \left(z \cdot x\right)} \]
  6. Taylor expanded in y around 0 97.0%

    \[\leadsto x - \color{blue}{x \cdot \left(y \cdot z\right)} \]
  7. Final simplification97.0%

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

Alternative 2: 71.0% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -1.85 \cdot 10^{+139} \lor \neg \left(y \leq -1.25 \cdot 10^{+132} \lor \neg \left(y \leq -1.4 \cdot 10^{+75}\right) \land y \leq 1.3 \cdot 10^{-98}\right):\\ \;\;\;\;-z \cdot \left(x \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= y -1.85e+139)
         (not
          (or (<= y -1.25e+132) (and (not (<= y -1.4e+75)) (<= y 1.3e-98)))))
   (- (* z (* x y)))
   x))
double code(double x, double y, double z) {
	double tmp;
	if ((y <= -1.85e+139) || !((y <= -1.25e+132) || (!(y <= -1.4e+75) && (y <= 1.3e-98)))) {
		tmp = -(z * (x * y));
	} else {
		tmp = x;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if ((y <= (-1.85d+139)) .or. (.not. (y <= (-1.25d+132)) .or. (.not. (y <= (-1.4d+75))) .and. (y <= 1.3d-98))) then
        tmp = -(z * (x * y))
    else
        tmp = x
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if ((y <= -1.85e+139) || !((y <= -1.25e+132) || (!(y <= -1.4e+75) && (y <= 1.3e-98)))) {
		tmp = -(z * (x * y));
	} else {
		tmp = x;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (y <= -1.85e+139) or not ((y <= -1.25e+132) or (not (y <= -1.4e+75) and (y <= 1.3e-98))):
		tmp = -(z * (x * y))
	else:
		tmp = x
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((y <= -1.85e+139) || !((y <= -1.25e+132) || (!(y <= -1.4e+75) && (y <= 1.3e-98))))
		tmp = Float64(-Float64(z * Float64(x * y)));
	else
		tmp = x;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((y <= -1.85e+139) || ~(((y <= -1.25e+132) || (~((y <= -1.4e+75)) && (y <= 1.3e-98)))))
		tmp = -(z * (x * y));
	else
		tmp = x;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[y, -1.85e+139], N[Not[Or[LessEqual[y, -1.25e+132], And[N[Not[LessEqual[y, -1.4e+75]], $MachinePrecision], LessEqual[y, 1.3e-98]]]], $MachinePrecision]], (-N[(z * N[(x * y), $MachinePrecision]), $MachinePrecision]), x]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.85 \cdot 10^{+139} \lor \neg \left(y \leq -1.25 \cdot 10^{+132} \lor \neg \left(y \leq -1.4 \cdot 10^{+75}\right) \land y \leq 1.3 \cdot 10^{-98}\right):\\
\;\;\;\;-z \cdot \left(x \cdot y\right)\\

\mathbf{else}:\\
\;\;\;\;x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1.84999999999999996e139 or -1.25e132 < y < -1.40000000000000006e75 or 1.30000000000000007e-98 < y

    1. Initial program 94.9%

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

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot \left(y \cdot z\right)\right)} \]
    3. Step-by-step derivation
      1. mul-1-neg68.4%

        \[\leadsto \color{blue}{-x \cdot \left(y \cdot z\right)} \]
      2. associate-*r*67.8%

        \[\leadsto -\color{blue}{\left(x \cdot y\right) \cdot z} \]
    4. Simplified67.8%

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

    if -1.84999999999999996e139 < y < -1.25e132 or -1.40000000000000006e75 < y < 1.30000000000000007e-98

    1. Initial program 99.2%

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

      \[\leadsto \color{blue}{x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification71.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.85 \cdot 10^{+139} \lor \neg \left(y \leq -1.25 \cdot 10^{+132} \lor \neg \left(y \leq -1.4 \cdot 10^{+75}\right) \land y \leq 1.3 \cdot 10^{-98}\right):\\ \;\;\;\;-z \cdot \left(x \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]

Alternative 3: 70.9% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -2.5 \cdot 10^{+139}:\\ \;\;\;\;-z \cdot \left(x \cdot y\right)\\ \mathbf{elif}\;y \leq -1.25 \cdot 10^{+132} \lor \neg \left(y \leq -6.2 \cdot 10^{+75}\right) \land y \leq 1.8 \cdot 10^{-100}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(z \cdot \left(-x\right)\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= y -2.5e+139)
   (- (* z (* x y)))
   (if (or (<= y -1.25e+132) (and (not (<= y -6.2e+75)) (<= y 1.8e-100)))
     x
     (* y (* z (- x))))))
double code(double x, double y, double z) {
	double tmp;
	if (y <= -2.5e+139) {
		tmp = -(z * (x * y));
	} else if ((y <= -1.25e+132) || (!(y <= -6.2e+75) && (y <= 1.8e-100))) {
		tmp = x;
	} else {
		tmp = y * (z * -x);
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (y <= (-2.5d+139)) then
        tmp = -(z * (x * y))
    else if ((y <= (-1.25d+132)) .or. (.not. (y <= (-6.2d+75))) .and. (y <= 1.8d-100)) then
        tmp = x
    else
        tmp = y * (z * -x)
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (y <= -2.5e+139) {
		tmp = -(z * (x * y));
	} else if ((y <= -1.25e+132) || (!(y <= -6.2e+75) && (y <= 1.8e-100))) {
		tmp = x;
	} else {
		tmp = y * (z * -x);
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if y <= -2.5e+139:
		tmp = -(z * (x * y))
	elif (y <= -1.25e+132) or (not (y <= -6.2e+75) and (y <= 1.8e-100)):
		tmp = x
	else:
		tmp = y * (z * -x)
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (y <= -2.5e+139)
		tmp = Float64(-Float64(z * Float64(x * y)));
	elseif ((y <= -1.25e+132) || (!(y <= -6.2e+75) && (y <= 1.8e-100)))
		tmp = x;
	else
		tmp = Float64(y * Float64(z * Float64(-x)));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y <= -2.5e+139)
		tmp = -(z * (x * y));
	elseif ((y <= -1.25e+132) || (~((y <= -6.2e+75)) && (y <= 1.8e-100)))
		tmp = x;
	else
		tmp = y * (z * -x);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[y, -2.5e+139], (-N[(z * N[(x * y), $MachinePrecision]), $MachinePrecision]), If[Or[LessEqual[y, -1.25e+132], And[N[Not[LessEqual[y, -6.2e+75]], $MachinePrecision], LessEqual[y, 1.8e-100]]], x, N[(y * N[(z * (-x)), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.5 \cdot 10^{+139}:\\
\;\;\;\;-z \cdot \left(x \cdot y\right)\\

\mathbf{elif}\;y \leq -1.25 \cdot 10^{+132} \lor \neg \left(y \leq -6.2 \cdot 10^{+75}\right) \land y \leq 1.8 \cdot 10^{-100}:\\
\;\;\;\;x\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if y < -2.50000000000000015e139

    1. Initial program 93.1%

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

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot \left(y \cdot z\right)\right)} \]
    3. Step-by-step derivation
      1. mul-1-neg88.5%

        \[\leadsto \color{blue}{-x \cdot \left(y \cdot z\right)} \]
      2. associate-*r*91.7%

        \[\leadsto -\color{blue}{\left(x \cdot y\right) \cdot z} \]
    4. Simplified91.7%

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

    if -2.50000000000000015e139 < y < -1.25e132 or -6.2000000000000002e75 < y < 1.7999999999999999e-100

    1. Initial program 99.2%

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

      \[\leadsto \color{blue}{x} \]

    if -1.25e132 < y < -6.2000000000000002e75 or 1.7999999999999999e-100 < y

    1. Initial program 95.3%

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

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot \left(y \cdot z\right)\right)} \]
    3. Step-by-step derivation
      1. mul-1-neg63.1%

        \[\leadsto \color{blue}{-x \cdot \left(y \cdot z\right)} \]
      2. associate-*r*61.4%

        \[\leadsto -\color{blue}{\left(x \cdot y\right) \cdot z} \]
      3. distribute-rgt-neg-in61.4%

        \[\leadsto \color{blue}{\left(x \cdot y\right) \cdot \left(-z\right)} \]
      4. *-commutative61.4%

        \[\leadsto \color{blue}{\left(y \cdot x\right)} \cdot \left(-z\right) \]
      5. associate-*r*65.7%

        \[\leadsto \color{blue}{y \cdot \left(x \cdot \left(-z\right)\right)} \]
      6. distribute-rgt-neg-out65.7%

        \[\leadsto y \cdot \color{blue}{\left(-x \cdot z\right)} \]
    4. Simplified65.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -2.5 \cdot 10^{+139}:\\ \;\;\;\;-z \cdot \left(x \cdot y\right)\\ \mathbf{elif}\;y \leq -1.25 \cdot 10^{+132} \lor \neg \left(y \leq -6.2 \cdot 10^{+75}\right) \land y \leq 1.8 \cdot 10^{-100}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(z \cdot \left(-x\right)\right)\\ \end{array} \]

Alternative 4: 95.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x \cdot \left(1 - y \cdot z\right) \end{array} \]
(FPCore (x y z) :precision binary64 (* x (- 1.0 (* y z))))
double code(double x, double y, double z) {
	return x * (1.0 - (y * z));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x * (1.0d0 - (y * z))
end function
public static double code(double x, double y, double z) {
	return x * (1.0 - (y * z));
}
def code(x, y, z):
	return x * (1.0 - (y * z))
function code(x, y, z)
	return Float64(x * Float64(1.0 - Float64(y * z)))
end
function tmp = code(x, y, z)
	tmp = x * (1.0 - (y * z));
end
code[x_, y_, z_] := N[(x * N[(1.0 - N[(y * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \left(1 - y \cdot z\right)
\end{array}
Derivation
  1. Initial program 97.0%

    \[x \cdot \left(1 - y \cdot z\right) \]
  2. Final simplification97.0%

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

Alternative 5: 49.8% accurate, 7.0× speedup?

\[\begin{array}{l} \\ x \end{array} \]
(FPCore (x y z) :precision binary64 x)
double code(double x, double y, double z) {
	return x;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x
end function
public static double code(double x, double y, double z) {
	return x;
}
def code(x, y, z):
	return x
function code(x, y, z)
	return x
end
function tmp = code(x, y, z)
	tmp = x;
end
code[x_, y_, z_] := x
\begin{array}{l}

\\
x
\end{array}
Derivation
  1. Initial program 97.0%

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

    \[\leadsto \color{blue}{x} \]
  3. Final simplification50.9%

    \[\leadsto x \]

Reproduce

?
herbie shell --seed 2023308 
(FPCore (x y z)
  :name "Data.Colour.RGBSpace.HSV:hsv from colour-2.3.3, I"
  :precision binary64
  (* x (- 1.0 (* y z))))