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

Percentage Accurate: 95.9% → 99.8%
Time: 4.6s
Alternatives: 4
Speedup: 0.3×

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 4 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.9% 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: 99.8% accurate, 0.3× speedup?

\[\begin{array}{l} [x, y, z] = \mathsf{sort}([x, y, z])\\ \\ \begin{array}{l} \mathbf{if}\;y \cdot z \leq -2 \cdot 10^{+211} \lor \neg \left(y \cdot z \leq 4 \cdot 10^{+236}\right):\\ \;\;\;\;\left(y \cdot x\right) \cdot \left(-z\right)\\ \mathbf{else}:\\ \;\;\;\;x - \left(y \cdot z\right) \cdot x\\ \end{array} \end{array} \]
NOTE: x, y, and z should be sorted in increasing order before calling this function.
(FPCore (x y z)
 :precision binary64
 (if (or (<= (* y z) -2e+211) (not (<= (* y z) 4e+236)))
   (* (* y x) (- z))
   (- x (* (* y z) x))))
assert(x < y && y < z);
double code(double x, double y, double z) {
	double tmp;
	if (((y * z) <= -2e+211) || !((y * z) <= 4e+236)) {
		tmp = (y * x) * -z;
	} else {
		tmp = x - ((y * z) * x);
	}
	return tmp;
}
NOTE: x, y, and z should be sorted in increasing order before calling this function.
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 * z) <= (-2d+211)) .or. (.not. ((y * z) <= 4d+236))) then
        tmp = (y * x) * -z
    else
        tmp = x - ((y * z) * x)
    end if
    code = tmp
end function
assert x < y && y < z;
public static double code(double x, double y, double z) {
	double tmp;
	if (((y * z) <= -2e+211) || !((y * z) <= 4e+236)) {
		tmp = (y * x) * -z;
	} else {
		tmp = x - ((y * z) * x);
	}
	return tmp;
}
[x, y, z] = sort([x, y, z])
def code(x, y, z):
	tmp = 0
	if ((y * z) <= -2e+211) or not ((y * z) <= 4e+236):
		tmp = (y * x) * -z
	else:
		tmp = x - ((y * z) * x)
	return tmp
x, y, z = sort([x, y, z])
function code(x, y, z)
	tmp = 0.0
	if ((Float64(y * z) <= -2e+211) || !(Float64(y * z) <= 4e+236))
		tmp = Float64(Float64(y * x) * Float64(-z));
	else
		tmp = Float64(x - Float64(Float64(y * z) * x));
	end
	return tmp
end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (((y * z) <= -2e+211) || ~(((y * z) <= 4e+236)))
		tmp = (y * x) * -z;
	else
		tmp = x - ((y * z) * x);
	end
	tmp_2 = tmp;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function.
code[x_, y_, z_] := If[Or[LessEqual[N[(y * z), $MachinePrecision], -2e+211], N[Not[LessEqual[N[(y * z), $MachinePrecision], 4e+236]], $MachinePrecision]], N[(N[(y * x), $MachinePrecision] * (-z)), $MachinePrecision], N[(x - N[(N[(y * z), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\begin{array}{l}
\mathbf{if}\;y \cdot z \leq -2 \cdot 10^{+211} \lor \neg \left(y \cdot z \leq 4 \cdot 10^{+236}\right):\\
\;\;\;\;\left(y \cdot x\right) \cdot \left(-z\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y z) < -1.9999999999999999e211 or 4.00000000000000021e236 < (*.f64 y z)

    1. Initial program 72.8%

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

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

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

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

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

    if -1.9999999999999999e211 < (*.f64 y z) < 4.00000000000000021e236

    1. Initial program 99.9%

      \[x \cdot \left(1 - y \cdot z\right) \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. sub-neg99.9%

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

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

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

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

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

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

        \[\leadsto x + \color{blue}{\left(-z\right) \cdot \left(y \cdot x\right)} \]
      3. add-sqr-sqrt42.9%

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

        \[\leadsto x + \color{blue}{\sqrt{\left(-z\right) \cdot \left(-z\right)}} \cdot \left(y \cdot x\right) \]
      5. sqr-neg66.6%

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

        \[\leadsto x + \color{blue}{\left(\sqrt{z} \cdot \sqrt{z}\right)} \cdot \left(y \cdot x\right) \]
      7. add-sqr-sqrt58.5%

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

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

        \[\leadsto x + \color{blue}{\left(y \cdot z\right)} \cdot x \]
      10. cancel-sign-sub61.2%

        \[\leadsto \color{blue}{x - \left(-y \cdot z\right) \cdot x} \]
      11. distribute-rgt-neg-out61.2%

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

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

        \[\leadsto x - \color{blue}{\left(x \cdot y\right) \cdot \left(-z\right)} \]
      14. add-sqr-sqrt27.9%

        \[\leadsto x - \left(x \cdot y\right) \cdot \color{blue}{\left(\sqrt{-z} \cdot \sqrt{-z}\right)} \]
      15. sqrt-unprod68.5%

        \[\leadsto x - \left(x \cdot y\right) \cdot \color{blue}{\sqrt{\left(-z\right) \cdot \left(-z\right)}} \]
      16. sqr-neg68.5%

        \[\leadsto x - \left(x \cdot y\right) \cdot \sqrt{\color{blue}{z \cdot z}} \]
      17. sqrt-unprod48.9%

        \[\leadsto x - \left(x \cdot y\right) \cdot \color{blue}{\left(\sqrt{z} \cdot \sqrt{z}\right)} \]
      18. add-sqr-sqrt91.9%

        \[\leadsto x - \left(x \cdot y\right) \cdot \color{blue}{z} \]
      19. associate-*l*99.9%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot z \leq -2 \cdot 10^{+211} \lor \neg \left(y \cdot z \leq 4 \cdot 10^{+236}\right):\\ \;\;\;\;\left(y \cdot x\right) \cdot \left(-z\right)\\ \mathbf{else}:\\ \;\;\;\;x - \left(y \cdot z\right) \cdot x\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 99.8% accurate, 0.3× speedup?

\[\begin{array}{l} [x, y, z] = \mathsf{sort}([x, y, z])\\ \\ \begin{array}{l} \mathbf{if}\;y \cdot z \leq -2 \cdot 10^{+211} \lor \neg \left(y \cdot z \leq 4 \cdot 10^{+236}\right):\\ \;\;\;\;\left(y \cdot x\right) \cdot \left(-z\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(1 - y \cdot z\right)\\ \end{array} \end{array} \]
NOTE: x, y, and z should be sorted in increasing order before calling this function.
(FPCore (x y z)
 :precision binary64
 (if (or (<= (* y z) -2e+211) (not (<= (* y z) 4e+236)))
   (* (* y x) (- z))
   (* x (- 1.0 (* y z)))))
assert(x < y && y < z);
double code(double x, double y, double z) {
	double tmp;
	if (((y * z) <= -2e+211) || !((y * z) <= 4e+236)) {
		tmp = (y * x) * -z;
	} else {
		tmp = x * (1.0 - (y * z));
	}
	return tmp;
}
NOTE: x, y, and z should be sorted in increasing order before calling this function.
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 * z) <= (-2d+211)) .or. (.not. ((y * z) <= 4d+236))) then
        tmp = (y * x) * -z
    else
        tmp = x * (1.0d0 - (y * z))
    end if
    code = tmp
end function
assert x < y && y < z;
public static double code(double x, double y, double z) {
	double tmp;
	if (((y * z) <= -2e+211) || !((y * z) <= 4e+236)) {
		tmp = (y * x) * -z;
	} else {
		tmp = x * (1.0 - (y * z));
	}
	return tmp;
}
[x, y, z] = sort([x, y, z])
def code(x, y, z):
	tmp = 0
	if ((y * z) <= -2e+211) or not ((y * z) <= 4e+236):
		tmp = (y * x) * -z
	else:
		tmp = x * (1.0 - (y * z))
	return tmp
x, y, z = sort([x, y, z])
function code(x, y, z)
	tmp = 0.0
	if ((Float64(y * z) <= -2e+211) || !(Float64(y * z) <= 4e+236))
		tmp = Float64(Float64(y * x) * Float64(-z));
	else
		tmp = Float64(x * Float64(1.0 - Float64(y * z)));
	end
	return tmp
end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (((y * z) <= -2e+211) || ~(((y * z) <= 4e+236)))
		tmp = (y * x) * -z;
	else
		tmp = x * (1.0 - (y * z));
	end
	tmp_2 = tmp;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function.
code[x_, y_, z_] := If[Or[LessEqual[N[(y * z), $MachinePrecision], -2e+211], N[Not[LessEqual[N[(y * z), $MachinePrecision], 4e+236]], $MachinePrecision]], N[(N[(y * x), $MachinePrecision] * (-z)), $MachinePrecision], N[(x * N[(1.0 - N[(y * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\begin{array}{l}
\mathbf{if}\;y \cdot z \leq -2 \cdot 10^{+211} \lor \neg \left(y \cdot z \leq 4 \cdot 10^{+236}\right):\\
\;\;\;\;\left(y \cdot x\right) \cdot \left(-z\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y z) < -1.9999999999999999e211 or 4.00000000000000021e236 < (*.f64 y z)

    1. Initial program 72.8%

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

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

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

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

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

    if -1.9999999999999999e211 < (*.f64 y z) < 4.00000000000000021e236

    1. Initial program 99.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot z \leq -2 \cdot 10^{+211} \lor \neg \left(y \cdot z \leq 4 \cdot 10^{+236}\right):\\ \;\;\;\;\left(y \cdot x\right) \cdot \left(-z\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(1 - y \cdot z\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 75.5% accurate, 0.4× speedup?

\[\begin{array}{l} [x, y, z] = \mathsf{sort}([x, y, z])\\ \\ \begin{array}{l} \mathbf{if}\;z \leq -7 \cdot 10^{-138} \lor \neg \left(z \leq 1.75 \cdot 10^{+87}\right):\\ \;\;\;\;\left(y \cdot x\right) \cdot \left(-z\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \end{array} \]
NOTE: x, y, and z should be sorted in increasing order before calling this function.
(FPCore (x y z)
 :precision binary64
 (if (or (<= z -7e-138) (not (<= z 1.75e+87))) (* (* y x) (- z)) x))
assert(x < y && y < z);
double code(double x, double y, double z) {
	double tmp;
	if ((z <= -7e-138) || !(z <= 1.75e+87)) {
		tmp = (y * x) * -z;
	} else {
		tmp = x;
	}
	return tmp;
}
NOTE: x, y, and z should be sorted in increasing order before calling this function.
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 ((z <= (-7d-138)) .or. (.not. (z <= 1.75d+87))) then
        tmp = (y * x) * -z
    else
        tmp = x
    end if
    code = tmp
end function
assert x < y && y < z;
public static double code(double x, double y, double z) {
	double tmp;
	if ((z <= -7e-138) || !(z <= 1.75e+87)) {
		tmp = (y * x) * -z;
	} else {
		tmp = x;
	}
	return tmp;
}
[x, y, z] = sort([x, y, z])
def code(x, y, z):
	tmp = 0
	if (z <= -7e-138) or not (z <= 1.75e+87):
		tmp = (y * x) * -z
	else:
		tmp = x
	return tmp
x, y, z = sort([x, y, z])
function code(x, y, z)
	tmp = 0.0
	if ((z <= -7e-138) || !(z <= 1.75e+87))
		tmp = Float64(Float64(y * x) * Float64(-z));
	else
		tmp = x;
	end
	return tmp
end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((z <= -7e-138) || ~((z <= 1.75e+87)))
		tmp = (y * x) * -z;
	else
		tmp = x;
	end
	tmp_2 = tmp;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function.
code[x_, y_, z_] := If[Or[LessEqual[z, -7e-138], N[Not[LessEqual[z, 1.75e+87]], $MachinePrecision]], N[(N[(y * x), $MachinePrecision] * (-z)), $MachinePrecision], x]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\begin{array}{l}
\mathbf{if}\;z \leq -7 \cdot 10^{-138} \lor \neg \left(z \leq 1.75 \cdot 10^{+87}\right):\\
\;\;\;\;\left(y \cdot x\right) \cdot \left(-z\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -6.9999999999999997e-138 or 1.74999999999999993e87 < z

    1. Initial program 91.9%

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

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

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

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

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

    if -6.9999999999999997e-138 < z < 1.74999999999999993e87

    1. Initial program 99.1%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -7 \cdot 10^{-138} \lor \neg \left(z \leq 1.75 \cdot 10^{+87}\right):\\ \;\;\;\;\left(y \cdot x\right) \cdot \left(-z\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 50.5% accurate, 7.0× speedup?

\[\begin{array}{l} [x, y, z] = \mathsf{sort}([x, y, z])\\ \\ x \end{array} \]
NOTE: x, y, and z should be sorted in increasing order before calling this function.
(FPCore (x y z) :precision binary64 x)
assert(x < y && y < z);
double code(double x, double y, double z) {
	return x;
}
NOTE: x, y, and z should be sorted in increasing order before calling this function.
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
assert x < y && y < z;
public static double code(double x, double y, double z) {
	return x;
}
[x, y, z] = sort([x, y, z])
def code(x, y, z):
	return x
x, y, z = sort([x, y, z])
function code(x, y, z)
	return x
end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp = code(x, y, z)
	tmp = x;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function.
code[x_, y_, z_] := x
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
x
\end{array}
Derivation
  1. Initial program 95.2%

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

    \[\leadsto \color{blue}{x} \]
  4. Final simplification51.9%

    \[\leadsto x \]
  5. Add Preprocessing

Reproduce

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