Diagrams.Backend.Rasterific:$crender from diagrams-rasterific-1.3.1.3

Percentage Accurate: 97.7% → 100.0%
Time: 3.6s
Alternatives: 8
Speedup: 1.3×

Specification

?
\[\begin{array}{l} \\ x \cdot y + \left(1 - x\right) \cdot z \end{array} \]
(FPCore (x y z) :precision binary64 (+ (* x y) (* (- 1.0 x) z)))
double code(double x, double y, double z) {
	return (x * y) + ((1.0 - x) * 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 * y) + ((1.0d0 - x) * z)
end function
public static double code(double x, double y, double z) {
	return (x * y) + ((1.0 - x) * z);
}
def code(x, y, z):
	return (x * y) + ((1.0 - x) * z)
function code(x, y, z)
	return Float64(Float64(x * y) + Float64(Float64(1.0 - x) * z))
end
function tmp = code(x, y, z)
	tmp = (x * y) + ((1.0 - x) * z);
end
code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] + N[(N[(1.0 - x), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot y + \left(1 - x\right) \cdot z
\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 8 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: 97.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x \cdot y + \left(1 - x\right) \cdot z \end{array} \]
(FPCore (x y z) :precision binary64 (+ (* x y) (* (- 1.0 x) z)))
double code(double x, double y, double z) {
	return (x * y) + ((1.0 - x) * 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 * y) + ((1.0d0 - x) * z)
end function
public static double code(double x, double y, double z) {
	return (x * y) + ((1.0 - x) * z);
}
def code(x, y, z):
	return (x * y) + ((1.0 - x) * z)
function code(x, y, z)
	return Float64(Float64(x * y) + Float64(Float64(1.0 - x) * z))
end
function tmp = code(x, y, z)
	tmp = (x * y) + ((1.0 - x) * z);
end
code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] + N[(N[(1.0 - x), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Alternative 1: 100.0% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(x, y - z, z\right) \end{array} \]
(FPCore (x y z) :precision binary64 (fma x (- y z) z))
double code(double x, double y, double z) {
	return fma(x, (y - z), z);
}
function code(x, y, z)
	return fma(x, Float64(y - z), z)
end
code[x_, y_, z_] := N[(x * N[(y - z), $MachinePrecision] + z), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(x, y - z, z\right)
\end{array}
Derivation
  1. Initial program 99.2%

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

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

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

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

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

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

      \[\leadsto \left(\color{blue}{z \cdot \left(-x\right)} + x \cdot y\right) + z \]
    7. neg-mul-199.2%

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

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

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

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

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

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{y + z \cdot -1}, z\right) \]
    13. *-commutative100.0%

      \[\leadsto \mathsf{fma}\left(x, y + \color{blue}{-1 \cdot z}, z\right) \]
    14. neg-mul-1100.0%

      \[\leadsto \mathsf{fma}\left(x, y + \color{blue}{\left(-z\right)}, z\right) \]
    15. unsub-neg100.0%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{y - z}, z\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, y - z, z\right)} \]
  4. Final simplification100.0%

    \[\leadsto \mathsf{fma}\left(x, y - z, z\right) \]

Alternative 2: 61.2% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := x \cdot \left(-z\right)\\ \mathbf{if}\;x \leq -1.4 \cdot 10^{+55}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -2.3 \cdot 10^{-29}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq 6200000000:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq 5.1 \cdot 10^{+206} \lor \neg \left(x \leq 3.9 \cdot 10^{+251}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* x (- z))))
   (if (<= x -1.4e+55)
     t_0
     (if (<= x -2.3e-29)
       (* x y)
       (if (<= x 6200000000.0)
         z
         (if (or (<= x 5.1e+206) (not (<= x 3.9e+251))) t_0 (* x y)))))))
double code(double x, double y, double z) {
	double t_0 = x * -z;
	double tmp;
	if (x <= -1.4e+55) {
		tmp = t_0;
	} else if (x <= -2.3e-29) {
		tmp = x * y;
	} else if (x <= 6200000000.0) {
		tmp = z;
	} else if ((x <= 5.1e+206) || !(x <= 3.9e+251)) {
		tmp = t_0;
	} else {
		tmp = x * y;
	}
	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) :: t_0
    real(8) :: tmp
    t_0 = x * -z
    if (x <= (-1.4d+55)) then
        tmp = t_0
    else if (x <= (-2.3d-29)) then
        tmp = x * y
    else if (x <= 6200000000.0d0) then
        tmp = z
    else if ((x <= 5.1d+206) .or. (.not. (x <= 3.9d+251))) then
        tmp = t_0
    else
        tmp = x * y
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = x * -z;
	double tmp;
	if (x <= -1.4e+55) {
		tmp = t_0;
	} else if (x <= -2.3e-29) {
		tmp = x * y;
	} else if (x <= 6200000000.0) {
		tmp = z;
	} else if ((x <= 5.1e+206) || !(x <= 3.9e+251)) {
		tmp = t_0;
	} else {
		tmp = x * y;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = x * -z
	tmp = 0
	if x <= -1.4e+55:
		tmp = t_0
	elif x <= -2.3e-29:
		tmp = x * y
	elif x <= 6200000000.0:
		tmp = z
	elif (x <= 5.1e+206) or not (x <= 3.9e+251):
		tmp = t_0
	else:
		tmp = x * y
	return tmp
function code(x, y, z)
	t_0 = Float64(x * Float64(-z))
	tmp = 0.0
	if (x <= -1.4e+55)
		tmp = t_0;
	elseif (x <= -2.3e-29)
		tmp = Float64(x * y);
	elseif (x <= 6200000000.0)
		tmp = z;
	elseif ((x <= 5.1e+206) || !(x <= 3.9e+251))
		tmp = t_0;
	else
		tmp = Float64(x * y);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = x * -z;
	tmp = 0.0;
	if (x <= -1.4e+55)
		tmp = t_0;
	elseif (x <= -2.3e-29)
		tmp = x * y;
	elseif (x <= 6200000000.0)
		tmp = z;
	elseif ((x <= 5.1e+206) || ~((x <= 3.9e+251)))
		tmp = t_0;
	else
		tmp = x * y;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(x * (-z)), $MachinePrecision]}, If[LessEqual[x, -1.4e+55], t$95$0, If[LessEqual[x, -2.3e-29], N[(x * y), $MachinePrecision], If[LessEqual[x, 6200000000.0], z, If[Or[LessEqual[x, 5.1e+206], N[Not[LessEqual[x, 3.9e+251]], $MachinePrecision]], t$95$0, N[(x * y), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := x \cdot \left(-z\right)\\
\mathbf{if}\;x \leq -1.4 \cdot 10^{+55}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq -2.3 \cdot 10^{-29}:\\
\;\;\;\;x \cdot y\\

\mathbf{elif}\;x \leq 6200000000:\\
\;\;\;\;z\\

\mathbf{elif}\;x \leq 5.1 \cdot 10^{+206} \lor \neg \left(x \leq 3.9 \cdot 10^{+251}\right):\\
\;\;\;\;t_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if x < -1.4e55 or 6.2e9 < x < 5.1000000000000003e206 or 3.89999999999999976e251 < x

    1. Initial program 98.2%

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

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

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

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

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

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

        \[\leadsto \left(\color{blue}{z \cdot \left(-x\right)} + x \cdot y\right) + z \]
      7. neg-mul-198.2%

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{y + z \cdot -1}, z\right) \]
      13. *-commutative100.0%

        \[\leadsto \mathsf{fma}\left(x, y + \color{blue}{-1 \cdot z}, z\right) \]
      14. neg-mul-1100.0%

        \[\leadsto \mathsf{fma}\left(x, y + \color{blue}{\left(-z\right)}, z\right) \]
      15. unsub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{y - z}, z\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, y - z, z\right)} \]
    4. Taylor expanded in x around inf 99.7%

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

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

        \[\leadsto \color{blue}{-z \cdot x} \]
      2. distribute-rgt-neg-in60.9%

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

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

    if -1.4e55 < x < -2.29999999999999991e-29 or 5.1000000000000003e206 < x < 3.89999999999999976e251

    1. Initial program 100.0%

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

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

    if -2.29999999999999991e-29 < x < 6.2e9

    1. Initial program 100.0%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1.4 \cdot 10^{+55}:\\ \;\;\;\;x \cdot \left(-z\right)\\ \mathbf{elif}\;x \leq -2.3 \cdot 10^{-29}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq 6200000000:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq 5.1 \cdot 10^{+206} \lor \neg \left(x \leq 3.9 \cdot 10^{+251}\right):\\ \;\;\;\;x \cdot \left(-z\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]

Alternative 3: 75.0% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -5 \cdot 10^{+55}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;y \leq 2 \cdot 10^{+19} \lor \neg \left(y \leq 9.5 \cdot 10^{+69}\right) \land y \leq 5 \cdot 10^{+130}:\\ \;\;\;\;z \cdot \left(1 - x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= y -5e+55)
   (* x y)
   (if (or (<= y 2e+19) (and (not (<= y 9.5e+69)) (<= y 5e+130)))
     (* z (- 1.0 x))
     (* x y))))
double code(double x, double y, double z) {
	double tmp;
	if (y <= -5e+55) {
		tmp = x * y;
	} else if ((y <= 2e+19) || (!(y <= 9.5e+69) && (y <= 5e+130))) {
		tmp = z * (1.0 - x);
	} else {
		tmp = x * y;
	}
	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 <= (-5d+55)) then
        tmp = x * y
    else if ((y <= 2d+19) .or. (.not. (y <= 9.5d+69)) .and. (y <= 5d+130)) then
        tmp = z * (1.0d0 - x)
    else
        tmp = x * y
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (y <= -5e+55) {
		tmp = x * y;
	} else if ((y <= 2e+19) || (!(y <= 9.5e+69) && (y <= 5e+130))) {
		tmp = z * (1.0 - x);
	} else {
		tmp = x * y;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if y <= -5e+55:
		tmp = x * y
	elif (y <= 2e+19) or (not (y <= 9.5e+69) and (y <= 5e+130)):
		tmp = z * (1.0 - x)
	else:
		tmp = x * y
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (y <= -5e+55)
		tmp = Float64(x * y);
	elseif ((y <= 2e+19) || (!(y <= 9.5e+69) && (y <= 5e+130)))
		tmp = Float64(z * Float64(1.0 - x));
	else
		tmp = Float64(x * y);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y <= -5e+55)
		tmp = x * y;
	elseif ((y <= 2e+19) || (~((y <= 9.5e+69)) && (y <= 5e+130)))
		tmp = z * (1.0 - x);
	else
		tmp = x * y;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[y, -5e+55], N[(x * y), $MachinePrecision], If[Or[LessEqual[y, 2e+19], And[N[Not[LessEqual[y, 9.5e+69]], $MachinePrecision], LessEqual[y, 5e+130]]], N[(z * N[(1.0 - x), $MachinePrecision]), $MachinePrecision], N[(x * y), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -5 \cdot 10^{+55}:\\
\;\;\;\;x \cdot y\\

\mathbf{elif}\;y \leq 2 \cdot 10^{+19} \lor \neg \left(y \leq 9.5 \cdot 10^{+69}\right) \land y \leq 5 \cdot 10^{+130}:\\
\;\;\;\;z \cdot \left(1 - x\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -5.00000000000000046e55 or 2e19 < y < 9.4999999999999995e69 or 4.9999999999999996e130 < y

    1. Initial program 97.7%

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

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

    if -5.00000000000000046e55 < y < 2e19 or 9.4999999999999995e69 < y < 4.9999999999999996e130

    1. Initial program 100.0%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -5 \cdot 10^{+55}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;y \leq 2 \cdot 10^{+19} \lor \neg \left(y \leq 9.5 \cdot 10^{+69}\right) \land y \leq 5 \cdot 10^{+130}:\\ \;\;\;\;z \cdot \left(1 - x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]

Alternative 4: 85.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -2.5 \cdot 10^{-42} \lor \neg \left(x \leq 9 \cdot 10^{-6}\right):\\ \;\;\;\;x \cdot \left(y - z\right)\\ \mathbf{else}:\\ \;\;\;\;z \cdot \left(1 - x\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= x -2.5e-42) (not (<= x 9e-6))) (* x (- y z)) (* z (- 1.0 x))))
double code(double x, double y, double z) {
	double tmp;
	if ((x <= -2.5e-42) || !(x <= 9e-6)) {
		tmp = x * (y - z);
	} else {
		tmp = z * (1.0 - 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 ((x <= (-2.5d-42)) .or. (.not. (x <= 9d-6))) then
        tmp = x * (y - z)
    else
        tmp = z * (1.0d0 - x)
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if ((x <= -2.5e-42) || !(x <= 9e-6)) {
		tmp = x * (y - z);
	} else {
		tmp = z * (1.0 - x);
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (x <= -2.5e-42) or not (x <= 9e-6):
		tmp = x * (y - z)
	else:
		tmp = z * (1.0 - x)
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((x <= -2.5e-42) || !(x <= 9e-6))
		tmp = Float64(x * Float64(y - z));
	else
		tmp = Float64(z * Float64(1.0 - x));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((x <= -2.5e-42) || ~((x <= 9e-6)))
		tmp = x * (y - z);
	else
		tmp = z * (1.0 - x);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[x, -2.5e-42], N[Not[LessEqual[x, 9e-6]], $MachinePrecision]], N[(x * N[(y - z), $MachinePrecision]), $MachinePrecision], N[(z * N[(1.0 - x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.5 \cdot 10^{-42} \lor \neg \left(x \leq 9 \cdot 10^{-6}\right):\\
\;\;\;\;x \cdot \left(y - z\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -2.50000000000000001e-42 or 9.00000000000000023e-6 < x

    1. Initial program 98.6%

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

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

        \[\leadsto x \cdot y + \color{blue}{\left(\left(-x\right) + 1\right)} \cdot z \]
      3. distribute-lft1-in98.6%

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

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

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

        \[\leadsto \left(\color{blue}{z \cdot \left(-x\right)} + x \cdot y\right) + z \]
      7. neg-mul-198.6%

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{y + z \cdot -1}, z\right) \]
      13. *-commutative100.0%

        \[\leadsto \mathsf{fma}\left(x, y + \color{blue}{-1 \cdot z}, z\right) \]
      14. neg-mul-1100.0%

        \[\leadsto \mathsf{fma}\left(x, y + \color{blue}{\left(-z\right)}, z\right) \]
      15. unsub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{y - z}, z\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, y - z, z\right)} \]
    4. Taylor expanded in x around inf 96.7%

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

    if -2.50000000000000001e-42 < x < 9.00000000000000023e-6

    1. Initial program 100.0%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -2.5 \cdot 10^{-42} \lor \neg \left(x \leq 9 \cdot 10^{-6}\right):\\ \;\;\;\;x \cdot \left(y - z\right)\\ \mathbf{else}:\\ \;\;\;\;z \cdot \left(1 - x\right)\\ \end{array} \]

Alternative 5: 98.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1\right):\\ \;\;\;\;x \cdot \left(y - z\right)\\ \mathbf{else}:\\ \;\;\;\;z + x \cdot y\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= x -1.0) (not (<= x 1.0))) (* x (- y z)) (+ z (* x y))))
double code(double x, double y, double z) {
	double tmp;
	if ((x <= -1.0) || !(x <= 1.0)) {
		tmp = x * (y - z);
	} else {
		tmp = z + (x * y);
	}
	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 ((x <= (-1.0d0)) .or. (.not. (x <= 1.0d0))) then
        tmp = x * (y - z)
    else
        tmp = z + (x * y)
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if ((x <= -1.0) || !(x <= 1.0)) {
		tmp = x * (y - z);
	} else {
		tmp = z + (x * y);
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (x <= -1.0) or not (x <= 1.0):
		tmp = x * (y - z)
	else:
		tmp = z + (x * y)
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((x <= -1.0) || !(x <= 1.0))
		tmp = Float64(x * Float64(y - z));
	else
		tmp = Float64(z + Float64(x * y));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((x <= -1.0) || ~((x <= 1.0)))
		tmp = x * (y - z);
	else
		tmp = z + (x * y);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 1.0]], $MachinePrecision]], N[(x * N[(y - z), $MachinePrecision]), $MachinePrecision], N[(z + N[(x * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1\right):\\
\;\;\;\;x \cdot \left(y - z\right)\\

\mathbf{else}:\\
\;\;\;\;z + x \cdot y\\


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

    1. Initial program 98.5%

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

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

        \[\leadsto x \cdot y + \color{blue}{\left(\left(-x\right) + 1\right)} \cdot z \]
      3. distribute-lft1-in98.5%

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

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

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

        \[\leadsto \left(\color{blue}{z \cdot \left(-x\right)} + x \cdot y\right) + z \]
      7. neg-mul-198.5%

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{y + z \cdot -1}, z\right) \]
      13. *-commutative100.0%

        \[\leadsto \mathsf{fma}\left(x, y + \color{blue}{-1 \cdot z}, z\right) \]
      14. neg-mul-1100.0%

        \[\leadsto \mathsf{fma}\left(x, y + \color{blue}{\left(-z\right)}, z\right) \]
      15. unsub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{y - z}, z\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, y - z, z\right)} \]
    4. Taylor expanded in x around inf 98.6%

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

    if -1 < x < 1

    1. Initial program 100.0%

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

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

        \[\leadsto x \cdot y + \color{blue}{\left(\left(-x\right) + 1\right)} \cdot z \]
      3. distribute-lft1-in100.0%

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

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

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

        \[\leadsto \left(\color{blue}{z \cdot \left(-x\right)} + x \cdot y\right) + z \]
      7. neg-mul-1100.0%

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{y + z \cdot -1}, z\right) \]
      13. *-commutative100.0%

        \[\leadsto \mathsf{fma}\left(x, y + \color{blue}{-1 \cdot z}, z\right) \]
      14. neg-mul-1100.0%

        \[\leadsto \mathsf{fma}\left(x, y + \color{blue}{\left(-z\right)}, z\right) \]
      15. unsub-neg100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{y - z}, z\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, y - z, z\right)} \]
    4. Taylor expanded in x around 0 100.0%

      \[\leadsto \color{blue}{\left(y - z\right) \cdot x + z} \]
    5. Taylor expanded in y around inf 99.2%

      \[\leadsto \color{blue}{y \cdot x} + z \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.9%

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

Alternative 6: 61.9% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -3.2 \cdot 10^{-24}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq 3.5 \cdot 10^{-18}:\\ \;\;\;\;z\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= x -3.2e-24) (* x y) (if (<= x 3.5e-18) z (* x y))))
double code(double x, double y, double z) {
	double tmp;
	if (x <= -3.2e-24) {
		tmp = x * y;
	} else if (x <= 3.5e-18) {
		tmp = z;
	} else {
		tmp = x * y;
	}
	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 (x <= (-3.2d-24)) then
        tmp = x * y
    else if (x <= 3.5d-18) then
        tmp = z
    else
        tmp = x * y
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (x <= -3.2e-24) {
		tmp = x * y;
	} else if (x <= 3.5e-18) {
		tmp = z;
	} else {
		tmp = x * y;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if x <= -3.2e-24:
		tmp = x * y
	elif x <= 3.5e-18:
		tmp = z
	else:
		tmp = x * y
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (x <= -3.2e-24)
		tmp = Float64(x * y);
	elseif (x <= 3.5e-18)
		tmp = z;
	else
		tmp = Float64(x * y);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (x <= -3.2e-24)
		tmp = x * y;
	elseif (x <= 3.5e-18)
		tmp = z;
	else
		tmp = x * y;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[x, -3.2e-24], N[(x * y), $MachinePrecision], If[LessEqual[x, 3.5e-18], z, N[(x * y), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.2 \cdot 10^{-24}:\\
\;\;\;\;x \cdot y\\

\mathbf{elif}\;x \leq 3.5 \cdot 10^{-18}:\\
\;\;\;\;z\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -3.20000000000000012e-24 or 3.4999999999999999e-18 < x

    1. Initial program 98.6%

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

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

    if -3.20000000000000012e-24 < x < 3.4999999999999999e-18

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{z} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification61.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -3.2 \cdot 10^{-24}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq 3.5 \cdot 10^{-18}:\\ \;\;\;\;z\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]

Alternative 7: 100.0% accurate, 1.3× speedup?

\[\begin{array}{l} \\ z + x \cdot \left(y - z\right) \end{array} \]
(FPCore (x y z) :precision binary64 (+ z (* x (- y z))))
double code(double x, double y, double z) {
	return z + (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 = z + (x * (y - z))
end function
public static double code(double x, double y, double z) {
	return z + (x * (y - z));
}
def code(x, y, z):
	return z + (x * (y - z))
function code(x, y, z)
	return Float64(z + Float64(x * Float64(y - z)))
end
function tmp = code(x, y, z)
	tmp = z + (x * (y - z));
end
code[x_, y_, z_] := N[(z + N[(x * N[(y - z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

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

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

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

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

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

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

      \[\leadsto \left(\color{blue}{z \cdot \left(-x\right)} + x \cdot y\right) + z \]
    7. neg-mul-199.2%

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

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

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

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

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

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{y + z \cdot -1}, z\right) \]
    13. *-commutative100.0%

      \[\leadsto \mathsf{fma}\left(x, y + \color{blue}{-1 \cdot z}, z\right) \]
    14. neg-mul-1100.0%

      \[\leadsto \mathsf{fma}\left(x, y + \color{blue}{\left(-z\right)}, z\right) \]
    15. unsub-neg100.0%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{y - z}, z\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, y - z, z\right)} \]
  4. Taylor expanded in x around 0 100.0%

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

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

Alternative 8: 36.6% accurate, 9.0× speedup?

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

\\
z
\end{array}
Derivation
  1. Initial program 99.2%

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

    \[\leadsto \color{blue}{z} \]
  3. Final simplification37.5%

    \[\leadsto z \]

Reproduce

?
herbie shell --seed 2023195 
(FPCore (x y z)
  :name "Diagrams.Backend.Rasterific:$crender from diagrams-rasterific-1.3.1.3"
  :precision binary64
  (+ (* x y) (* (- 1.0 x) z)))