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

Percentage Accurate: 96.1% → 97.6%
Time: 6.8s
Alternatives: 6
Speedup: 0.6×

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 6 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: 96.1% 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: 97.6% accurate, 0.6× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ [x_m, y, z] = \mathsf{sort}([x_m, y, z])\\ \\ x_s \cdot \begin{array}{l} \mathbf{if}\;x_m \leq 5 \cdot 10^{-107}:\\ \;\;\;\;x_m - z \cdot \left(x_m \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;x_m \cdot \left(1 - z \cdot y\right)\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
(FPCore (x_s x_m y z)
 :precision binary64
 (* x_s (if (<= x_m 5e-107) (- x_m (* z (* x_m y))) (* x_m (- 1.0 (* z y))))))
x_m = fabs(x);
x_s = copysign(1.0, x);
assert(x_m < y && y < z);
double code(double x_s, double x_m, double y, double z) {
	double tmp;
	if (x_m <= 5e-107) {
		tmp = x_m - (z * (x_m * y));
	} else {
		tmp = x_m * (1.0 - (z * y));
	}
	return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
real(8) function code(x_s, x_m, y, z)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (x_m <= 5d-107) then
        tmp = x_m - (z * (x_m * y))
    else
        tmp = x_m * (1.0d0 - (z * y))
    end if
    code = x_s * tmp
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
assert x_m < y && y < z;
public static double code(double x_s, double x_m, double y, double z) {
	double tmp;
	if (x_m <= 5e-107) {
		tmp = x_m - (z * (x_m * y));
	} else {
		tmp = x_m * (1.0 - (z * y));
	}
	return x_s * tmp;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
[x_m, y, z] = sort([x_m, y, z])
def code(x_s, x_m, y, z):
	tmp = 0
	if x_m <= 5e-107:
		tmp = x_m - (z * (x_m * y))
	else:
		tmp = x_m * (1.0 - (z * y))
	return x_s * tmp
x_m = abs(x)
x_s = copysign(1.0, x)
x_m, y, z = sort([x_m, y, z])
function code(x_s, x_m, y, z)
	tmp = 0.0
	if (x_m <= 5e-107)
		tmp = Float64(x_m - Float64(z * Float64(x_m * y)));
	else
		tmp = Float64(x_m * Float64(1.0 - Float64(z * y)));
	end
	return Float64(x_s * tmp)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
x_m, y, z = num2cell(sort([x_m, y, z])){:}
function tmp_2 = code(x_s, x_m, y, z)
	tmp = 0.0;
	if (x_m <= 5e-107)
		tmp = x_m - (z * (x_m * y));
	else
		tmp = x_m * (1.0 - (z * y));
	end
	tmp_2 = x_s * tmp;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * If[LessEqual[x$95$m, 5e-107], N[(x$95$m - N[(z * N[(x$95$m * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x$95$m * N[(1.0 - N[(z * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
[x_m, y, z] = \mathsf{sort}([x_m, y, z])\\
\\
x_s \cdot \begin{array}{l}
\mathbf{if}\;x_m \leq 5 \cdot 10^{-107}:\\
\;\;\;\;x_m - z \cdot \left(x_m \cdot y\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 4.99999999999999971e-107

    1. Initial program 93.1%

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

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

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

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

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

      \[\leadsto \color{blue}{x + \left(y \cdot \left(-z\right)\right) \cdot x} \]
    5. Step-by-step derivation
      1. distribute-rgt-neg-out93.1%

        \[\leadsto x + \color{blue}{\left(-y \cdot z\right)} \cdot x \]
      2. distribute-lft-neg-out93.1%

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

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

        \[\leadsto x + \left(-y \cdot \color{blue}{\left(x \cdot z\right)}\right) \]
      5. distribute-lft-neg-in91.5%

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

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

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

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

        \[\leadsto x + \left(-y\right) \cdot \color{blue}{\left(\sqrt{-x \cdot z} \cdot \sqrt{-x \cdot z}\right)} \]
      10. add-sqr-sqrt50.0%

        \[\leadsto x + \left(-y\right) \cdot \color{blue}{\left(-x \cdot z\right)} \]
      11. *-commutative50.0%

        \[\leadsto x + \left(-y\right) \cdot \left(-\color{blue}{z \cdot x}\right) \]
      12. distribute-lft-neg-in50.0%

        \[\leadsto x + \left(-y\right) \cdot \color{blue}{\left(\left(-z\right) \cdot x\right)} \]
      13. cancel-sign-sub-inv50.0%

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

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

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

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

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

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

        \[\leadsto x - \sqrt{\color{blue}{z \cdot z}} \cdot \left(y \cdot x\right) \]
      20. sqrt-unprod51.2%

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

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

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

    if 4.99999999999999971e-107 < x

    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 simplification97.3%

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

Alternative 2: 96.3% accurate, 0.3× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ [x_m, y, z] = \mathsf{sort}([x_m, y, z])\\ \\ \begin{array}{l} t_0 := y \cdot \left(-x_m \cdot z\right)\\ x_s \cdot \begin{array}{l} \mathbf{if}\;z \cdot y \leq -100000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \cdot y \leq 0.0004:\\ \;\;\;\;x_m\\ \mathbf{elif}\;z \cdot y \leq 10^{+258}:\\ \;\;\;\;x_m \cdot \left(z \cdot \left(-y\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \end{array} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
(FPCore (x_s x_m y z)
 :precision binary64
 (let* ((t_0 (* y (- (* x_m z)))))
   (*
    x_s
    (if (<= (* z y) -100000.0)
      t_0
      (if (<= (* z y) 0.0004)
        x_m
        (if (<= (* z y) 1e+258) (* x_m (* z (- y))) t_0))))))
x_m = fabs(x);
x_s = copysign(1.0, x);
assert(x_m < y && y < z);
double code(double x_s, double x_m, double y, double z) {
	double t_0 = y * -(x_m * z);
	double tmp;
	if ((z * y) <= -100000.0) {
		tmp = t_0;
	} else if ((z * y) <= 0.0004) {
		tmp = x_m;
	} else if ((z * y) <= 1e+258) {
		tmp = x_m * (z * -y);
	} else {
		tmp = t_0;
	}
	return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
real(8) function code(x_s, x_m, y, z)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: t_0
    real(8) :: tmp
    t_0 = y * -(x_m * z)
    if ((z * y) <= (-100000.0d0)) then
        tmp = t_0
    else if ((z * y) <= 0.0004d0) then
        tmp = x_m
    else if ((z * y) <= 1d+258) then
        tmp = x_m * (z * -y)
    else
        tmp = t_0
    end if
    code = x_s * tmp
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
assert x_m < y && y < z;
public static double code(double x_s, double x_m, double y, double z) {
	double t_0 = y * -(x_m * z);
	double tmp;
	if ((z * y) <= -100000.0) {
		tmp = t_0;
	} else if ((z * y) <= 0.0004) {
		tmp = x_m;
	} else if ((z * y) <= 1e+258) {
		tmp = x_m * (z * -y);
	} else {
		tmp = t_0;
	}
	return x_s * tmp;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
[x_m, y, z] = sort([x_m, y, z])
def code(x_s, x_m, y, z):
	t_0 = y * -(x_m * z)
	tmp = 0
	if (z * y) <= -100000.0:
		tmp = t_0
	elif (z * y) <= 0.0004:
		tmp = x_m
	elif (z * y) <= 1e+258:
		tmp = x_m * (z * -y)
	else:
		tmp = t_0
	return x_s * tmp
x_m = abs(x)
x_s = copysign(1.0, x)
x_m, y, z = sort([x_m, y, z])
function code(x_s, x_m, y, z)
	t_0 = Float64(y * Float64(-Float64(x_m * z)))
	tmp = 0.0
	if (Float64(z * y) <= -100000.0)
		tmp = t_0;
	elseif (Float64(z * y) <= 0.0004)
		tmp = x_m;
	elseif (Float64(z * y) <= 1e+258)
		tmp = Float64(x_m * Float64(z * Float64(-y)));
	else
		tmp = t_0;
	end
	return Float64(x_s * tmp)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
x_m, y, z = num2cell(sort([x_m, y, z])){:}
function tmp_2 = code(x_s, x_m, y, z)
	t_0 = y * -(x_m * z);
	tmp = 0.0;
	if ((z * y) <= -100000.0)
		tmp = t_0;
	elseif ((z * y) <= 0.0004)
		tmp = x_m;
	elseif ((z * y) <= 1e+258)
		tmp = x_m * (z * -y);
	else
		tmp = t_0;
	end
	tmp_2 = x_s * tmp;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
code[x$95$s_, x$95$m_, y_, z_] := Block[{t$95$0 = N[(y * (-N[(x$95$m * z), $MachinePrecision])), $MachinePrecision]}, N[(x$95$s * If[LessEqual[N[(z * y), $MachinePrecision], -100000.0], t$95$0, If[LessEqual[N[(z * y), $MachinePrecision], 0.0004], x$95$m, If[LessEqual[N[(z * y), $MachinePrecision], 1e+258], N[(x$95$m * N[(z * (-y)), $MachinePrecision]), $MachinePrecision], t$95$0]]]), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
[x_m, y, z] = \mathsf{sort}([x_m, y, z])\\
\\
\begin{array}{l}
t_0 := y \cdot \left(-x_m \cdot z\right)\\
x_s \cdot \begin{array}{l}
\mathbf{if}\;z \cdot y \leq -100000:\\
\;\;\;\;t_0\\

\mathbf{elif}\;z \cdot y \leq 0.0004:\\
\;\;\;\;x_m\\

\mathbf{elif}\;z \cdot y \leq 10^{+258}:\\
\;\;\;\;x_m \cdot \left(z \cdot \left(-y\right)\right)\\

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


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 y z) < -1e5 or 1.00000000000000006e258 < (*.f64 y z)

    1. Initial program 85.2%

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

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

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

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

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

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

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

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

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

    if -1e5 < (*.f64 y z) < 4.00000000000000019e-4

    1. Initial program 100.0%

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

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

    if 4.00000000000000019e-4 < (*.f64 y z) < 1.00000000000000006e258

    1. Initial program 99.8%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \cdot y \leq -100000:\\ \;\;\;\;y \cdot \left(-x \cdot z\right)\\ \mathbf{elif}\;z \cdot y \leq 0.0004:\\ \;\;\;\;x\\ \mathbf{elif}\;z \cdot y \leq 10^{+258}:\\ \;\;\;\;x \cdot \left(z \cdot \left(-y\right)\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(-x \cdot z\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 94.2% accurate, 0.3× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ [x_m, y, z] = \mathsf{sort}([x_m, y, z])\\ \\ x_s \cdot \begin{array}{l} \mathbf{if}\;z \cdot y \leq -400 \lor \neg \left(z \cdot y \leq 0.0004\right):\\ \;\;\;\;x_m \cdot \left(z \cdot \left(-y\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x_m\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
(FPCore (x_s x_m y z)
 :precision binary64
 (*
  x_s
  (if (or (<= (* z y) -400.0) (not (<= (* z y) 0.0004)))
    (* x_m (* z (- y)))
    x_m)))
x_m = fabs(x);
x_s = copysign(1.0, x);
assert(x_m < y && y < z);
double code(double x_s, double x_m, double y, double z) {
	double tmp;
	if (((z * y) <= -400.0) || !((z * y) <= 0.0004)) {
		tmp = x_m * (z * -y);
	} else {
		tmp = x_m;
	}
	return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
real(8) function code(x_s, x_m, y, z)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (((z * y) <= (-400.0d0)) .or. (.not. ((z * y) <= 0.0004d0))) then
        tmp = x_m * (z * -y)
    else
        tmp = x_m
    end if
    code = x_s * tmp
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
assert x_m < y && y < z;
public static double code(double x_s, double x_m, double y, double z) {
	double tmp;
	if (((z * y) <= -400.0) || !((z * y) <= 0.0004)) {
		tmp = x_m * (z * -y);
	} else {
		tmp = x_m;
	}
	return x_s * tmp;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
[x_m, y, z] = sort([x_m, y, z])
def code(x_s, x_m, y, z):
	tmp = 0
	if ((z * y) <= -400.0) or not ((z * y) <= 0.0004):
		tmp = x_m * (z * -y)
	else:
		tmp = x_m
	return x_s * tmp
x_m = abs(x)
x_s = copysign(1.0, x)
x_m, y, z = sort([x_m, y, z])
function code(x_s, x_m, y, z)
	tmp = 0.0
	if ((Float64(z * y) <= -400.0) || !(Float64(z * y) <= 0.0004))
		tmp = Float64(x_m * Float64(z * Float64(-y)));
	else
		tmp = x_m;
	end
	return Float64(x_s * tmp)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
x_m, y, z = num2cell(sort([x_m, y, z])){:}
function tmp_2 = code(x_s, x_m, y, z)
	tmp = 0.0;
	if (((z * y) <= -400.0) || ~(((z * y) <= 0.0004)))
		tmp = x_m * (z * -y);
	else
		tmp = x_m;
	end
	tmp_2 = x_s * tmp;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * If[Or[LessEqual[N[(z * y), $MachinePrecision], -400.0], N[Not[LessEqual[N[(z * y), $MachinePrecision], 0.0004]], $MachinePrecision]], N[(x$95$m * N[(z * (-y)), $MachinePrecision]), $MachinePrecision], x$95$m]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
[x_m, y, z] = \mathsf{sort}([x_m, y, z])\\
\\
x_s \cdot \begin{array}{l}
\mathbf{if}\;z \cdot y \leq -400 \lor \neg \left(z \cdot y \leq 0.0004\right):\\
\;\;\;\;x_m \cdot \left(z \cdot \left(-y\right)\right)\\

\mathbf{else}:\\
\;\;\;\;x_m\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y z) < -400 or 4.00000000000000019e-4 < (*.f64 y z)

    1. Initial program 90.6%

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

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

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

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

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

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

    if -400 < (*.f64 y z) < 4.00000000000000019e-4

    1. Initial program 100.0%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \cdot y \leq -400 \lor \neg \left(z \cdot y \leq 0.0004\right):\\ \;\;\;\;x \cdot \left(z \cdot \left(-y\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 94.6% accurate, 0.3× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ [x_m, y, z] = \mathsf{sort}([x_m, y, z])\\ \\ x_s \cdot \begin{array}{l} \mathbf{if}\;z \cdot y \leq -100000:\\ \;\;\;\;y \cdot \left(-x_m \cdot z\right)\\ \mathbf{elif}\;z \cdot y \leq 0.0004:\\ \;\;\;\;x_m\\ \mathbf{else}:\\ \;\;\;\;z \cdot \left(x_m \cdot \left(-y\right)\right)\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
(FPCore (x_s x_m y z)
 :precision binary64
 (*
  x_s
  (if (<= (* z y) -100000.0)
    (* y (- (* x_m z)))
    (if (<= (* z y) 0.0004) x_m (* z (* x_m (- y)))))))
x_m = fabs(x);
x_s = copysign(1.0, x);
assert(x_m < y && y < z);
double code(double x_s, double x_m, double y, double z) {
	double tmp;
	if ((z * y) <= -100000.0) {
		tmp = y * -(x_m * z);
	} else if ((z * y) <= 0.0004) {
		tmp = x_m;
	} else {
		tmp = z * (x_m * -y);
	}
	return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
real(8) function code(x_s, x_m, y, z)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if ((z * y) <= (-100000.0d0)) then
        tmp = y * -(x_m * z)
    else if ((z * y) <= 0.0004d0) then
        tmp = x_m
    else
        tmp = z * (x_m * -y)
    end if
    code = x_s * tmp
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
assert x_m < y && y < z;
public static double code(double x_s, double x_m, double y, double z) {
	double tmp;
	if ((z * y) <= -100000.0) {
		tmp = y * -(x_m * z);
	} else if ((z * y) <= 0.0004) {
		tmp = x_m;
	} else {
		tmp = z * (x_m * -y);
	}
	return x_s * tmp;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
[x_m, y, z] = sort([x_m, y, z])
def code(x_s, x_m, y, z):
	tmp = 0
	if (z * y) <= -100000.0:
		tmp = y * -(x_m * z)
	elif (z * y) <= 0.0004:
		tmp = x_m
	else:
		tmp = z * (x_m * -y)
	return x_s * tmp
x_m = abs(x)
x_s = copysign(1.0, x)
x_m, y, z = sort([x_m, y, z])
function code(x_s, x_m, y, z)
	tmp = 0.0
	if (Float64(z * y) <= -100000.0)
		tmp = Float64(y * Float64(-Float64(x_m * z)));
	elseif (Float64(z * y) <= 0.0004)
		tmp = x_m;
	else
		tmp = Float64(z * Float64(x_m * Float64(-y)));
	end
	return Float64(x_s * tmp)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
x_m, y, z = num2cell(sort([x_m, y, z])){:}
function tmp_2 = code(x_s, x_m, y, z)
	tmp = 0.0;
	if ((z * y) <= -100000.0)
		tmp = y * -(x_m * z);
	elseif ((z * y) <= 0.0004)
		tmp = x_m;
	else
		tmp = z * (x_m * -y);
	end
	tmp_2 = x_s * tmp;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * If[LessEqual[N[(z * y), $MachinePrecision], -100000.0], N[(y * (-N[(x$95$m * z), $MachinePrecision])), $MachinePrecision], If[LessEqual[N[(z * y), $MachinePrecision], 0.0004], x$95$m, N[(z * N[(x$95$m * (-y)), $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
[x_m, y, z] = \mathsf{sort}([x_m, y, z])\\
\\
x_s \cdot \begin{array}{l}
\mathbf{if}\;z \cdot y \leq -100000:\\
\;\;\;\;y \cdot \left(-x_m \cdot z\right)\\

\mathbf{elif}\;z \cdot y \leq 0.0004:\\
\;\;\;\;x_m\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 y z) < -1e5

    1. Initial program 93.7%

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

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

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

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

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

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

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

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

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

    if -1e5 < (*.f64 y z) < 4.00000000000000019e-4

    1. Initial program 100.0%

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

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

    if 4.00000000000000019e-4 < (*.f64 y z)

    1. Initial program 87.2%

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

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

        \[\leadsto \color{blue}{\frac{x \cdot \left(1 \cdot 1 - \left(y \cdot z\right) \cdot \left(y \cdot z\right)\right)}{1 + y \cdot z}} \]
      3. metadata-eval52.7%

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

        \[\leadsto \frac{x \cdot \left(1 - \color{blue}{{\left(y \cdot z\right)}^{2}}\right)}{1 + y \cdot z} \]
      5. +-commutative52.7%

        \[\leadsto \frac{x \cdot \left(1 - {\left(y \cdot z\right)}^{2}\right)}{\color{blue}{y \cdot z + 1}} \]
      6. fma-def52.7%

        \[\leadsto \frac{x \cdot \left(1 - {\left(y \cdot z\right)}^{2}\right)}{\color{blue}{\mathsf{fma}\left(y, z, 1\right)}} \]
    4. Applied egg-rr52.7%

      \[\leadsto \color{blue}{\frac{x \cdot \left(1 - {\left(y \cdot z\right)}^{2}\right)}{\mathsf{fma}\left(y, z, 1\right)}} \]
    5. Step-by-step derivation
      1. associate-*l/47.5%

        \[\leadsto \color{blue}{\frac{x}{\mathsf{fma}\left(y, z, 1\right)} \cdot \left(1 - {\left(y \cdot z\right)}^{2}\right)} \]
    6. Simplified47.5%

      \[\leadsto \color{blue}{\frac{x}{\mathsf{fma}\left(y, z, 1\right)} \cdot \left(1 - {\left(y \cdot z\right)}^{2}\right)} \]
    7. Taylor expanded in y around inf 44.2%

      \[\leadsto \color{blue}{\frac{x}{y \cdot z}} \cdot \left(1 - {\left(y \cdot z\right)}^{2}\right) \]
    8. Taylor expanded in y around inf 83.9%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \cdot y \leq -100000:\\ \;\;\;\;y \cdot \left(-x \cdot z\right)\\ \mathbf{elif}\;z \cdot y \leq 0.0004:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;z \cdot \left(x \cdot \left(-y\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 98.2% accurate, 0.5× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ [x_m, y, z] = \mathsf{sort}([x_m, y, z])\\ \\ x_s \cdot \begin{array}{l} \mathbf{if}\;z \cdot y \leq 10^{+258}:\\ \;\;\;\;x_m \cdot \left(1 - z \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(-x_m \cdot z\right)\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
(FPCore (x_s x_m y z)
 :precision binary64
 (* x_s (if (<= (* z y) 1e+258) (* x_m (- 1.0 (* z y))) (* y (- (* x_m z))))))
x_m = fabs(x);
x_s = copysign(1.0, x);
assert(x_m < y && y < z);
double code(double x_s, double x_m, double y, double z) {
	double tmp;
	if ((z * y) <= 1e+258) {
		tmp = x_m * (1.0 - (z * y));
	} else {
		tmp = y * -(x_m * z);
	}
	return x_s * tmp;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
real(8) function code(x_s, x_m, y, z)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if ((z * y) <= 1d+258) then
        tmp = x_m * (1.0d0 - (z * y))
    else
        tmp = y * -(x_m * z)
    end if
    code = x_s * tmp
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
assert x_m < y && y < z;
public static double code(double x_s, double x_m, double y, double z) {
	double tmp;
	if ((z * y) <= 1e+258) {
		tmp = x_m * (1.0 - (z * y));
	} else {
		tmp = y * -(x_m * z);
	}
	return x_s * tmp;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
[x_m, y, z] = sort([x_m, y, z])
def code(x_s, x_m, y, z):
	tmp = 0
	if (z * y) <= 1e+258:
		tmp = x_m * (1.0 - (z * y))
	else:
		tmp = y * -(x_m * z)
	return x_s * tmp
x_m = abs(x)
x_s = copysign(1.0, x)
x_m, y, z = sort([x_m, y, z])
function code(x_s, x_m, y, z)
	tmp = 0.0
	if (Float64(z * y) <= 1e+258)
		tmp = Float64(x_m * Float64(1.0 - Float64(z * y)));
	else
		tmp = Float64(y * Float64(-Float64(x_m * z)));
	end
	return Float64(x_s * tmp)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
x_m, y, z = num2cell(sort([x_m, y, z])){:}
function tmp_2 = code(x_s, x_m, y, z)
	tmp = 0.0;
	if ((z * y) <= 1e+258)
		tmp = x_m * (1.0 - (z * y));
	else
		tmp = y * -(x_m * z);
	end
	tmp_2 = x_s * tmp;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * If[LessEqual[N[(z * y), $MachinePrecision], 1e+258], N[(x$95$m * N[(1.0 - N[(z * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * (-N[(x$95$m * z), $MachinePrecision])), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
[x_m, y, z] = \mathsf{sort}([x_m, y, z])\\
\\
x_s \cdot \begin{array}{l}
\mathbf{if}\;z \cdot y \leq 10^{+258}:\\
\;\;\;\;x_m \cdot \left(1 - z \cdot y\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y z) < 1.00000000000000006e258

    1. Initial program 98.3%

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

    if 1.00000000000000006e258 < (*.f64 y z)

    1. Initial program 52.7%

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

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

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

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

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

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

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

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

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

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

Alternative 6: 51.1% accurate, 7.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x_s = \mathsf{copysign}\left(1, x\right) \\ [x_m, y, z] = \mathsf{sort}([x_m, y, z])\\ \\ x_s \cdot x_m \end{array} \]
x_m = (fabs.f64 x)
x_s = (copysign.f64 1 x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
(FPCore (x_s x_m y z) :precision binary64 (* x_s x_m))
x_m = fabs(x);
x_s = copysign(1.0, x);
assert(x_m < y && y < z);
double code(double x_s, double x_m, double y, double z) {
	return x_s * x_m;
}
x_m = abs(x)
x_s = copysign(1.0d0, x)
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
real(8) function code(x_s, x_m, y, z)
    real(8), intent (in) :: x_s
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x_s * x_m
end function
x_m = Math.abs(x);
x_s = Math.copySign(1.0, x);
assert x_m < y && y < z;
public static double code(double x_s, double x_m, double y, double z) {
	return x_s * x_m;
}
x_m = math.fabs(x)
x_s = math.copysign(1.0, x)
[x_m, y, z] = sort([x_m, y, z])
def code(x_s, x_m, y, z):
	return x_s * x_m
x_m = abs(x)
x_s = copysign(1.0, x)
x_m, y, z = sort([x_m, y, z])
function code(x_s, x_m, y, z)
	return Float64(x_s * x_m)
end
x_m = abs(x);
x_s = sign(x) * abs(1.0);
x_m, y, z = num2cell(sort([x_m, y, z])){:}
function tmp = code(x_s, x_m, y, z)
	tmp = x_s * x_m;
end
x_m = N[Abs[x], $MachinePrecision]
x_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: x_m, y, and z should be sorted in increasing order before calling this function.
code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * x$95$m), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_s = \mathsf{copysign}\left(1, x\right)
\\
[x_m, y, z] = \mathsf{sort}([x_m, y, z])\\
\\
x_s \cdot x_m
\end{array}
Derivation
  1. Initial program 95.5%

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

    \[\leadsto \color{blue}{x} \]
  4. Final simplification53.3%

    \[\leadsto x \]
  5. Add Preprocessing

Reproduce

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