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

Percentage Accurate: 100.0% → 100.0%
Time: 3.5s
Alternatives: 5
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 5 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, 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. Add Preprocessing

Alternative 2: 76.3% accurate, 0.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq -5.8 \cdot 10^{+257} \lor \neg \left(z \leq -3.9 \cdot 10^{+200} \lor \neg \left(z \leq -1.2 \cdot 10^{+169}\right) \land \left(z \leq -2.25 \cdot 10^{+115} \lor \neg \left(z \leq 4.1 \cdot 10^{+18}\right) \land z \leq 5 \cdot 10^{+122}\right)\right):\\ \;\;\;\;x + y \cdot z\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(-z\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= z -5.8e+257)
         (not
          (or (<= z -3.9e+200)
              (and (not (<= z -1.2e+169))
                   (or (<= z -2.25e+115)
                       (and (not (<= z 4.1e+18)) (<= z 5e+122)))))))
   (+ x (* y z))
   (* x (- z))))
double code(double x, double y, double z) {
	double tmp;
	if ((z <= -5.8e+257) || !((z <= -3.9e+200) || (!(z <= -1.2e+169) && ((z <= -2.25e+115) || (!(z <= 4.1e+18) && (z <= 5e+122)))))) {
		tmp = x + (y * z);
	} else {
		tmp = x * -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 <= (-5.8d+257)) .or. (.not. (z <= (-3.9d+200)) .or. (.not. (z <= (-1.2d+169))) .and. (z <= (-2.25d+115)) .or. (.not. (z <= 4.1d+18)) .and. (z <= 5d+122))) then
        tmp = x + (y * z)
    else
        tmp = x * -z
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if ((z <= -5.8e+257) || !((z <= -3.9e+200) || (!(z <= -1.2e+169) && ((z <= -2.25e+115) || (!(z <= 4.1e+18) && (z <= 5e+122)))))) {
		tmp = x + (y * z);
	} else {
		tmp = x * -z;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (z <= -5.8e+257) or not ((z <= -3.9e+200) or (not (z <= -1.2e+169) and ((z <= -2.25e+115) or (not (z <= 4.1e+18) and (z <= 5e+122))))):
		tmp = x + (y * z)
	else:
		tmp = x * -z
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((z <= -5.8e+257) || !((z <= -3.9e+200) || (!(z <= -1.2e+169) && ((z <= -2.25e+115) || (!(z <= 4.1e+18) && (z <= 5e+122))))))
		tmp = Float64(x + Float64(y * z));
	else
		tmp = Float64(x * Float64(-z));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((z <= -5.8e+257) || ~(((z <= -3.9e+200) || (~((z <= -1.2e+169)) && ((z <= -2.25e+115) || (~((z <= 4.1e+18)) && (z <= 5e+122)))))))
		tmp = x + (y * z);
	else
		tmp = x * -z;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[z, -5.8e+257], N[Not[Or[LessEqual[z, -3.9e+200], And[N[Not[LessEqual[z, -1.2e+169]], $MachinePrecision], Or[LessEqual[z, -2.25e+115], And[N[Not[LessEqual[z, 4.1e+18]], $MachinePrecision], LessEqual[z, 5e+122]]]]]], $MachinePrecision]], N[(x + N[(y * z), $MachinePrecision]), $MachinePrecision], N[(x * (-z)), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;z \leq -5.8 \cdot 10^{+257} \lor \neg \left(z \leq -3.9 \cdot 10^{+200} \lor \neg \left(z \leq -1.2 \cdot 10^{+169}\right) \land \left(z \leq -2.25 \cdot 10^{+115} \lor \neg \left(z \leq 4.1 \cdot 10^{+18}\right) \land z \leq 5 \cdot 10^{+122}\right)\right):\\
\;\;\;\;x + y \cdot z\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -5.7999999999999998e257 or -3.90000000000000019e200 < z < -1.1999999999999999e169 or -2.24999999999999982e115 < z < 4.1e18 or 4.99999999999999989e122 < z

    1. Initial program 100.0%

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

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

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

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

    if -5.7999999999999998e257 < z < -3.90000000000000019e200 or -1.1999999999999999e169 < z < -2.24999999999999982e115 or 4.1e18 < z < 4.99999999999999989e122

    1. Initial program 100.0%

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

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

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

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

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

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

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot z\right)} \]
    7. Step-by-step derivation
      1. associate-*r*70.8%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -5.8 \cdot 10^{+257} \lor \neg \left(z \leq -3.9 \cdot 10^{+200} \lor \neg \left(z \leq -1.2 \cdot 10^{+169}\right) \land \left(z \leq -2.25 \cdot 10^{+115} \lor \neg \left(z \leq 4.1 \cdot 10^{+18}\right) \land z \leq 5 \cdot 10^{+122}\right)\right):\\ \;\;\;\;x + y \cdot z\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(-z\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 85.3% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.8 \cdot 10^{+132} \lor \neg \left(x \leq 1.1 \cdot 10^{+182}\right):\\
\;\;\;\;x - x \cdot z\\

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


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

    1. Initial program 100.0%

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{x - z \cdot x} \]
      3. *-commutative93.4%

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

      \[\leadsto \color{blue}{x - x \cdot z} \]

    if -3.80000000000000006e132 < x < 1.09999999999999998e182

    1. Initial program 100.0%

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

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

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

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

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

Alternative 4: 60.1% accurate, 0.5× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -5.20000000000000018 or 6.8e12 < z

    1. Initial program 100.0%

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

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

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

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

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

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

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot z\right)} \]
    7. Step-by-step derivation
      1. associate-*r*50.1%

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

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

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

    if -5.20000000000000018 < z < 6.8e12

    1. Initial program 100.0%

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

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

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

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

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

      \[\leadsto x + \color{blue}{z \cdot \left(-x\right)} \]
    6. Taylor expanded in z around 0 60.7%

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

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

Alternative 5: 36.4% 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 y around 0 55.7%

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

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

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

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

    \[\leadsto x + \color{blue}{z \cdot \left(-x\right)} \]
  6. Taylor expanded in z around 0 32.7%

    \[\leadsto \color{blue}{x} \]
  7. Add Preprocessing

Reproduce

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