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

Percentage Accurate: 97.9% → 100.0%
Time: 4.2s
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.9% 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 98.0%

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

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

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

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

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

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

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

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

      \[\leadsto \left(\color{blue}{\left(z \cdot -1\right) \cdot x} + x \cdot y\right) + z \]
    9. *-commutative98.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. Final simplification100.0%

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

Alternative 2: 61.3% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := z \cdot \left(-x\right)\\ \mathbf{if}\;x \leq -1.46 \cdot 10^{-9}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 2.5 \cdot 10^{-43}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq 3.5 \cdot 10^{+37}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq 5.2 \cdot 10^{+238}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* z (- x))))
   (if (<= x -1.46e-9)
     t_0
     (if (<= x 2.5e-43)
       z
       (if (<= x 3.5e+37) (* x y) (if (<= x 5.2e+238) t_0 (* x y)))))))
double code(double x, double y, double z) {
	double t_0 = z * -x;
	double tmp;
	if (x <= -1.46e-9) {
		tmp = t_0;
	} else if (x <= 2.5e-43) {
		tmp = z;
	} else if (x <= 3.5e+37) {
		tmp = x * y;
	} else if (x <= 5.2e+238) {
		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 = z * -x
    if (x <= (-1.46d-9)) then
        tmp = t_0
    else if (x <= 2.5d-43) then
        tmp = z
    else if (x <= 3.5d+37) then
        tmp = x * y
    else if (x <= 5.2d+238) 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 = z * -x;
	double tmp;
	if (x <= -1.46e-9) {
		tmp = t_0;
	} else if (x <= 2.5e-43) {
		tmp = z;
	} else if (x <= 3.5e+37) {
		tmp = x * y;
	} else if (x <= 5.2e+238) {
		tmp = t_0;
	} else {
		tmp = x * y;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = z * -x
	tmp = 0
	if x <= -1.46e-9:
		tmp = t_0
	elif x <= 2.5e-43:
		tmp = z
	elif x <= 3.5e+37:
		tmp = x * y
	elif x <= 5.2e+238:
		tmp = t_0
	else:
		tmp = x * y
	return tmp
function code(x, y, z)
	t_0 = Float64(z * Float64(-x))
	tmp = 0.0
	if (x <= -1.46e-9)
		tmp = t_0;
	elseif (x <= 2.5e-43)
		tmp = z;
	elseif (x <= 3.5e+37)
		tmp = Float64(x * y);
	elseif (x <= 5.2e+238)
		tmp = t_0;
	else
		tmp = Float64(x * y);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = z * -x;
	tmp = 0.0;
	if (x <= -1.46e-9)
		tmp = t_0;
	elseif (x <= 2.5e-43)
		tmp = z;
	elseif (x <= 3.5e+37)
		tmp = x * y;
	elseif (x <= 5.2e+238)
		tmp = t_0;
	else
		tmp = x * y;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(z * (-x)), $MachinePrecision]}, If[LessEqual[x, -1.46e-9], t$95$0, If[LessEqual[x, 2.5e-43], z, If[LessEqual[x, 3.5e+37], N[(x * y), $MachinePrecision], If[LessEqual[x, 5.2e+238], t$95$0, N[(x * y), $MachinePrecision]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;x \leq 2.5 \cdot 10^{-43}:\\
\;\;\;\;z\\

\mathbf{elif}\;x \leq 3.5 \cdot 10^{+37}:\\
\;\;\;\;x \cdot y\\

\mathbf{elif}\;x \leq 5.2 \cdot 10^{+238}:\\
\;\;\;\;t_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if x < -1.4599999999999999e-9 or 3.5e37 < x < 5.1999999999999999e238

    1. Initial program 96.8%

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

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

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

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

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

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

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

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

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

        \[\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.5%

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

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

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

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

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

    if -1.4599999999999999e-9 < x < 2.50000000000000009e-43

    1. Initial program 99.9%

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

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

    if 2.50000000000000009e-43 < x < 3.5e37 or 5.1999999999999999e238 < x

    1. Initial program 93.3%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1.46 \cdot 10^{-9}:\\ \;\;\;\;z \cdot \left(-x\right)\\ \mathbf{elif}\;x \leq 2.5 \cdot 10^{-43}:\\ \;\;\;\;z\\ \mathbf{elif}\;x \leq 3.5 \cdot 10^{+37}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq 5.2 \cdot 10^{+238}:\\ \;\;\;\;z \cdot \left(-x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]

Alternative 3: 78.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -1.5 \cdot 10^{+72} \lor \neg \left(y \leq 4.4 \cdot 10^{-17}\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 (<= y -1.5e+72) (not (<= y 4.4e-17))) (* x (- y z)) (* z (- 1.0 x))))
double code(double x, double y, double z) {
	double tmp;
	if ((y <= -1.5e+72) || !(y <= 4.4e-17)) {
		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 ((y <= (-1.5d+72)) .or. (.not. (y <= 4.4d-17))) 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 ((y <= -1.5e+72) || !(y <= 4.4e-17)) {
		tmp = x * (y - z);
	} else {
		tmp = z * (1.0 - x);
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (y <= -1.5e+72) or not (y <= 4.4e-17):
		tmp = x * (y - z)
	else:
		tmp = z * (1.0 - x)
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((y <= -1.5e+72) || !(y <= 4.4e-17))
		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 ((y <= -1.5e+72) || ~((y <= 4.4e-17)))
		tmp = x * (y - z);
	else
		tmp = z * (1.0 - x);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[y, -1.5e+72], N[Not[LessEqual[y, 4.4e-17]], $MachinePrecision]], N[(x * N[(y - z), $MachinePrecision]), $MachinePrecision], N[(z * N[(1.0 - x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.5 \cdot 10^{+72} \lor \neg \left(y \leq 4.4 \cdot 10^{-17}\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 y < -1.50000000000000001e72 or 4.4e-17 < y

    1. Initial program 95.7%

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

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

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

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

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

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

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

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

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

        \[\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 76.7%

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

    if -1.50000000000000001e72 < y < 4.4e-17

    1. Initial program 100.0%

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

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

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

Alternative 4: 98.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -17000000 \lor \neg \left(x \leq 0.52\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 -17000000.0) (not (<= x 0.52))) (* x (- y z)) (+ z (* x y))))
double code(double x, double y, double z) {
	double tmp;
	if ((x <= -17000000.0) || !(x <= 0.52)) {
		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 <= (-17000000.0d0)) .or. (.not. (x <= 0.52d0))) 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 <= -17000000.0) || !(x <= 0.52)) {
		tmp = x * (y - z);
	} else {
		tmp = z + (x * y);
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (x <= -17000000.0) or not (x <= 0.52):
		tmp = x * (y - z)
	else:
		tmp = z + (x * y)
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((x <= -17000000.0) || !(x <= 0.52))
		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 <= -17000000.0) || ~((x <= 0.52)))
		tmp = x * (y - z);
	else
		tmp = z + (x * y);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[x, -17000000.0], N[Not[LessEqual[x, 0.52]], $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 -17000000 \lor \neg \left(x \leq 0.52\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.7e7 or 0.52000000000000002 < x

    1. Initial program 95.5%

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

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

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

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

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

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

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

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

        \[\leadsto \left(\color{blue}{\left(z \cdot -1\right) \cdot x} + x \cdot y\right) + z \]
      9. *-commutative95.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 99.1%

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

    if -1.7e7 < x < 0.52000000000000002

    1. Initial program 99.9%

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

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

        \[\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.4%

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

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

Alternative 5: 73.4% accurate, 1.0× speedup?

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

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

\mathbf{elif}\;y \leq 7.8 \cdot 10^{-16}:\\
\;\;\;\;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 < -3.39999999999999993e161 or 7.79999999999999954e-16 < y

    1. Initial program 95.9%

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

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

    if -3.39999999999999993e161 < y < 7.79999999999999954e-16

    1. Initial program 99.3%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -3.4 \cdot 10^{+161}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;y \leq 7.8 \cdot 10^{-16}:\\ \;\;\;\;z \cdot \left(1 - x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]

Alternative 6: 61.7% accurate, 1.3× speedup?

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

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

\mathbf{elif}\;x \leq 8 \cdot 10^{-44}:\\
\;\;\;\;z\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -6.8000000000000001e-12 or 7.99999999999999962e-44 < x

    1. Initial program 95.9%

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

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

    if -6.8000000000000001e-12 < x < 7.99999999999999962e-44

    1. Initial program 99.9%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -6.8 \cdot 10^{-12}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq 8 \cdot 10^{-44}:\\ \;\;\;\;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 98.0%

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

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

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

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

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

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

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

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

      \[\leadsto \left(\color{blue}{\left(z \cdot -1\right) \cdot x} + x \cdot y\right) + z \]
    9. *-commutative98.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. Final simplification100.0%

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

Alternative 8: 36.3% 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 98.0%

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

    \[\leadsto \color{blue}{z} \]
  3. Final simplification39.9%

    \[\leadsto z \]

Reproduce

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