Diagrams.ThreeD.Shapes:frustum from diagrams-lib-1.3.0.3, B

Percentage Accurate: 100.0% → 100.0%
Time: 6.0s
Alternatives: 8
Speedup: 1.0×

Specification

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

\\
x + \left(y - 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: 100.0% accurate, 1.0× speedup?

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

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

Alternative 1: 100.0% accurate, 0.1× speedup?

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

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

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

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

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

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

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

Alternative 2: 60.7% accurate, 0.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := x \cdot \left(-z\right)\\ \mathbf{if}\;z \leq -1.55 \cdot 10^{+269}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;z \leq -2.15 \cdot 10^{+242}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z \leq -1.65 \cdot 10^{+175}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;z \leq -6.2 \cdot 10^{+79}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z \leq -3 \cdot 10^{-20}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;z \leq 1:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 1.35 \cdot 10^{+142}:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;y \cdot z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* x (- z))))
   (if (<= z -1.55e+269)
     (* y z)
     (if (<= z -2.15e+242)
       t_0
       (if (<= z -1.65e+175)
         (* y z)
         (if (<= z -6.2e+79)
           t_0
           (if (<= z -3e-20)
             (* y z)
             (if (<= z 1.0) x (if (<= z 1.35e+142) t_0 (* y z))))))))))
double code(double x, double y, double z) {
	double t_0 = x * -z;
	double tmp;
	if (z <= -1.55e+269) {
		tmp = y * z;
	} else if (z <= -2.15e+242) {
		tmp = t_0;
	} else if (z <= -1.65e+175) {
		tmp = y * z;
	} else if (z <= -6.2e+79) {
		tmp = t_0;
	} else if (z <= -3e-20) {
		tmp = y * z;
	} else if (z <= 1.0) {
		tmp = x;
	} else if (z <= 1.35e+142) {
		tmp = t_0;
	} else {
		tmp = y * z;
	}
	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 (z <= (-1.55d+269)) then
        tmp = y * z
    else if (z <= (-2.15d+242)) then
        tmp = t_0
    else if (z <= (-1.65d+175)) then
        tmp = y * z
    else if (z <= (-6.2d+79)) then
        tmp = t_0
    else if (z <= (-3d-20)) then
        tmp = y * z
    else if (z <= 1.0d0) then
        tmp = x
    else if (z <= 1.35d+142) then
        tmp = t_0
    else
        tmp = y * z
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = x * -z;
	double tmp;
	if (z <= -1.55e+269) {
		tmp = y * z;
	} else if (z <= -2.15e+242) {
		tmp = t_0;
	} else if (z <= -1.65e+175) {
		tmp = y * z;
	} else if (z <= -6.2e+79) {
		tmp = t_0;
	} else if (z <= -3e-20) {
		tmp = y * z;
	} else if (z <= 1.0) {
		tmp = x;
	} else if (z <= 1.35e+142) {
		tmp = t_0;
	} else {
		tmp = y * z;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = x * -z
	tmp = 0
	if z <= -1.55e+269:
		tmp = y * z
	elif z <= -2.15e+242:
		tmp = t_0
	elif z <= -1.65e+175:
		tmp = y * z
	elif z <= -6.2e+79:
		tmp = t_0
	elif z <= -3e-20:
		tmp = y * z
	elif z <= 1.0:
		tmp = x
	elif z <= 1.35e+142:
		tmp = t_0
	else:
		tmp = y * z
	return tmp
function code(x, y, z)
	t_0 = Float64(x * Float64(-z))
	tmp = 0.0
	if (z <= -1.55e+269)
		tmp = Float64(y * z);
	elseif (z <= -2.15e+242)
		tmp = t_0;
	elseif (z <= -1.65e+175)
		tmp = Float64(y * z);
	elseif (z <= -6.2e+79)
		tmp = t_0;
	elseif (z <= -3e-20)
		tmp = Float64(y * z);
	elseif (z <= 1.0)
		tmp = x;
	elseif (z <= 1.35e+142)
		tmp = t_0;
	else
		tmp = Float64(y * z);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = x * -z;
	tmp = 0.0;
	if (z <= -1.55e+269)
		tmp = y * z;
	elseif (z <= -2.15e+242)
		tmp = t_0;
	elseif (z <= -1.65e+175)
		tmp = y * z;
	elseif (z <= -6.2e+79)
		tmp = t_0;
	elseif (z <= -3e-20)
		tmp = y * z;
	elseif (z <= 1.0)
		tmp = x;
	elseif (z <= 1.35e+142)
		tmp = t_0;
	else
		tmp = y * z;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(x * (-z)), $MachinePrecision]}, If[LessEqual[z, -1.55e+269], N[(y * z), $MachinePrecision], If[LessEqual[z, -2.15e+242], t$95$0, If[LessEqual[z, -1.65e+175], N[(y * z), $MachinePrecision], If[LessEqual[z, -6.2e+79], t$95$0, If[LessEqual[z, -3e-20], N[(y * z), $MachinePrecision], If[LessEqual[z, 1.0], x, If[LessEqual[z, 1.35e+142], t$95$0, N[(y * z), $MachinePrecision]]]]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;z \leq -2.15 \cdot 10^{+242}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;z \leq -1.65 \cdot 10^{+175}:\\
\;\;\;\;y \cdot z\\

\mathbf{elif}\;z \leq -6.2 \cdot 10^{+79}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;z \leq -3 \cdot 10^{-20}:\\
\;\;\;\;y \cdot z\\

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

\mathbf{elif}\;z \leq 1.35 \cdot 10^{+142}:\\
\;\;\;\;t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if z < -1.55000000000000001e269 or -2.1500000000000002e242 < z < -1.6500000000000001e175 or -6.1999999999999998e79 < z < -3.00000000000000029e-20 or 1.34999999999999991e142 < z

    1. Initial program 100.0%

      \[x + \left(y - x\right) \cdot z \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-commutative100.0%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{y \cdot z} \]
    8. Step-by-step derivation
      1. *-commutative70.9%

        \[\leadsto \color{blue}{z \cdot y} \]
    9. Simplified70.9%

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

    if -1.55000000000000001e269 < z < -2.1500000000000002e242 or -1.6500000000000001e175 < z < -6.1999999999999998e79 or 1 < z < 1.34999999999999991e142

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{x - z \cdot x} \]
    7. Applied egg-rr65.5%

      \[\leadsto \color{blue}{x - z \cdot x} \]
    8. Taylor expanded in z around inf 64.0%

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

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

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

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

    if -3.00000000000000029e-20 < z < 1

    1. Initial program 100.0%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -1.55 \cdot 10^{+269}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;z \leq -2.15 \cdot 10^{+242}:\\ \;\;\;\;x \cdot \left(-z\right)\\ \mathbf{elif}\;z \leq -1.65 \cdot 10^{+175}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;z \leq -6.2 \cdot 10^{+79}:\\ \;\;\;\;x \cdot \left(-z\right)\\ \mathbf{elif}\;z \leq -3 \cdot 10^{-20}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;z \leq 1:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 1.35 \cdot 10^{+142}:\\ \;\;\;\;x \cdot \left(-z\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot z\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 73.0% accurate, 0.3× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.28 \cdot 10^{+109} \lor \neg \left(y \leq -5.4 \cdot 10^{+74} \lor \neg \left(y \leq -1.8 \cdot 10^{+24}\right) \land y \leq 1.02 \cdot 10^{-20}\right):\\
\;\;\;\;y \cdot z\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1.28e109 or -5.3999999999999996e74 < y < -1.79999999999999992e24 or 1.02000000000000001e-20 < y

    1. Initial program 100.0%

      \[x + \left(y - x\right) \cdot z \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-commutative100.0%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{y \cdot z} \]
    8. Step-by-step derivation
      1. *-commutative76.4%

        \[\leadsto \color{blue}{z \cdot y} \]
    9. Simplified76.4%

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

    if -1.28e109 < y < -5.3999999999999996e74 or -1.79999999999999992e24 < y < 1.02000000000000001e-20

    1. Initial program 100.0%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.28 \cdot 10^{+109} \lor \neg \left(y \leq -5.4 \cdot 10^{+74} \lor \neg \left(y \leq -1.8 \cdot 10^{+24}\right) \land y \leq 1.02 \cdot 10^{-20}\right):\\ \;\;\;\;y \cdot z\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(1 - z\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 83.2% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.5 \cdot 10^{-108} \lor \neg \left(z \leq 62\right):\\
\;\;\;\;\left(y - x\right) \cdot z\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -1.49999999999999996e-108 or 62 < z

    1. Initial program 100.0%

      \[x + \left(y - x\right) \cdot z \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-commutative100.0%

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

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

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

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

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

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

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

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

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

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

    if -1.49999999999999996e-108 < z < 62

    1. Initial program 100.0%

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

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

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

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

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

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

Alternative 5: 98.1% accurate, 0.5× speedup?

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

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

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


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

    1. Initial program 100.0%

      \[x + \left(y - x\right) \cdot z \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-commutative100.0%

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

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

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

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

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

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

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

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

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

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

    if -1.49999999999999997e24 < z < 1

    1. Initial program 100.0%

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

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

        \[\leadsto x + \color{blue}{z \cdot y} \]
    5. Simplified97.8%

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

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

Alternative 6: 61.5% accurate, 0.5× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -6.00000000000000057e-20 or 1.00000000000000007e-37 < z

    1. Initial program 100.0%

      \[x + \left(y - x\right) \cdot z \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-commutative100.0%

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

        \[\leadsto x + z \cdot \color{blue}{\left(y + \left(-x\right)\right)} \]
      3. distribute-lft-in97.9%

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{y \cdot z} \]
    8. Step-by-step derivation
      1. *-commutative53.3%

        \[\leadsto \color{blue}{z \cdot y} \]
    9. Simplified53.3%

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

    if -6.00000000000000057e-20 < z < 1.00000000000000007e-37

    1. Initial program 100.0%

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

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

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

Alternative 7: 100.0% accurate, 1.0× speedup?

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

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

    \[x + \left(y - x\right) \cdot z \]
  2. Add Preprocessing
  3. Final simplification100.0%

    \[\leadsto x + \left(y - x\right) \cdot z \]
  4. Add Preprocessing

Alternative 8: 35.5% accurate, 7.0× speedup?

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

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

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

    \[\leadsto \color{blue}{x} \]
  4. Final simplification33.7%

    \[\leadsto x \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2024054 
(FPCore (x y z)
  :name "Diagrams.ThreeD.Shapes:frustum from diagrams-lib-1.3.0.3, B"
  :precision binary64
  (+ x (* (- y x) z)))