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

Percentage Accurate: 100.0% → 100.0%
Time: 5.1s
Alternatives: 5
Speedup: 1.2×

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.2× 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. Add Preprocessing
  3. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto x + \color{blue}{\left(y - x\right)} \cdot z \]
    2. lift-*.f64N/A

      \[\leadsto x + \color{blue}{\left(y - x\right) \cdot z} \]
    3. +-commutativeN/A

      \[\leadsto \color{blue}{\left(y - x\right) \cdot z + x} \]
    4. lift-*.f64N/A

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

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

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

Alternative 2: 98.4% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(y - x\right) \cdot z\\ \mathbf{if}\;z \leq -1.32 \cdot 10^{+19}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z \leq 0.16:\\ \;\;\;\;\mathsf{fma}\left(z, y, x\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* (- y x) z)))
   (if (<= z -1.32e+19) t_0 (if (<= z 0.16) (fma z y x) t_0))))
double code(double x, double y, double z) {
	double t_0 = (y - x) * z;
	double tmp;
	if (z <= -1.32e+19) {
		tmp = t_0;
	} else if (z <= 0.16) {
		tmp = fma(z, y, x);
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(x, y, z)
	t_0 = Float64(Float64(y - x) * z)
	tmp = 0.0
	if (z <= -1.32e+19)
		tmp = t_0;
	elseif (z <= 0.16)
		tmp = fma(z, y, x);
	else
		tmp = t_0;
	end
	return tmp
end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(y - x), $MachinePrecision] * z), $MachinePrecision]}, If[LessEqual[z, -1.32e+19], t$95$0, If[LessEqual[z, 0.16], N[(z * y + x), $MachinePrecision], t$95$0]]]
\begin{array}{l}

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

\mathbf{elif}\;z \leq 0.16:\\
\;\;\;\;\mathsf{fma}\left(z, y, x\right)\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -1.32e19 or 0.160000000000000003 < z

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{z \cdot \left(y - x\right)} \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \color{blue}{z \cdot \left(y - x\right)} \]
      2. lower--.f6499.6

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

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

    if -1.32e19 < z < 0.160000000000000003

    1. Initial program 100.0%

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

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

        \[\leadsto x + \color{blue}{y \cdot z} \]
    5. Applied rewrites99.3%

      \[\leadsto x + \color{blue}{y \cdot z} \]
    6. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto x + \color{blue}{y \cdot z} \]
      2. +-commutativeN/A

        \[\leadsto \color{blue}{y \cdot z + x} \]
      3. lift-*.f64N/A

        \[\leadsto \color{blue}{y \cdot z} + x \]
      4. *-commutativeN/A

        \[\leadsto \color{blue}{z \cdot y} + x \]
      5. lower-fma.f6499.3

        \[\leadsto \color{blue}{\mathsf{fma}\left(z, y, x\right)} \]
    7. Applied rewrites99.3%

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, y, x\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -1.32 \cdot 10^{+19}:\\ \;\;\;\;\left(y - x\right) \cdot z\\ \mathbf{elif}\;z \leq 0.16:\\ \;\;\;\;\mathsf{fma}\left(z, y, x\right)\\ \mathbf{else}:\\ \;\;\;\;\left(y - x\right) \cdot z\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 76.5% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := -x \cdot z\\ \mathbf{if}\;z \leq -3.8 \cdot 10^{+169}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z \leq 2.3 \cdot 10^{+14}:\\ \;\;\;\;\mathsf{fma}\left(z, y, x\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (- (* x z))))
   (if (<= z -3.8e+169) t_0 (if (<= z 2.3e+14) (fma z y x) t_0))))
double code(double x, double y, double z) {
	double t_0 = -(x * z);
	double tmp;
	if (z <= -3.8e+169) {
		tmp = t_0;
	} else if (z <= 2.3e+14) {
		tmp = fma(z, y, x);
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(x, y, z)
	t_0 = Float64(-Float64(x * z))
	tmp = 0.0
	if (z <= -3.8e+169)
		tmp = t_0;
	elseif (z <= 2.3e+14)
		tmp = fma(z, y, x);
	else
		tmp = t_0;
	end
	return tmp
end
code[x_, y_, z_] := Block[{t$95$0 = (-N[(x * z), $MachinePrecision])}, If[LessEqual[z, -3.8e+169], t$95$0, If[LessEqual[z, 2.3e+14], N[(z * y + x), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := -x \cdot z\\
\mathbf{if}\;z \leq -3.8 \cdot 10^{+169}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;z \leq 2.3 \cdot 10^{+14}:\\
\;\;\;\;\mathsf{fma}\left(z, y, x\right)\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -3.79999999999999992e169 or 2.3e14 < z

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{z \cdot \left(y - x\right)} \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \color{blue}{z \cdot \left(y - x\right)} \]
      2. lower--.f64100.0

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

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

      \[\leadsto z \cdot \color{blue}{\left(-1 \cdot x\right)} \]
    7. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto z \cdot \color{blue}{\left(\mathsf{neg}\left(x\right)\right)} \]
      2. lower-neg.f6462.8

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

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

    if -3.79999999999999992e169 < z < 2.3e14

    1. Initial program 100.0%

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

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

        \[\leadsto x + \color{blue}{y \cdot z} \]
    5. Applied rewrites89.1%

      \[\leadsto x + \color{blue}{y \cdot z} \]
    6. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto x + \color{blue}{y \cdot z} \]
      2. +-commutativeN/A

        \[\leadsto \color{blue}{y \cdot z + x} \]
      3. lift-*.f64N/A

        \[\leadsto \color{blue}{y \cdot z} + x \]
      4. *-commutativeN/A

        \[\leadsto \color{blue}{z \cdot y} + x \]
      5. lower-fma.f6489.1

        \[\leadsto \color{blue}{\mathsf{fma}\left(z, y, x\right)} \]
    7. Applied rewrites89.1%

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, y, x\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification79.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -3.8 \cdot 10^{+169}:\\ \;\;\;\;-x \cdot z\\ \mathbf{elif}\;z \leq 2.3 \cdot 10^{+14}:\\ \;\;\;\;\mathsf{fma}\left(z, y, x\right)\\ \mathbf{else}:\\ \;\;\;\;-x \cdot z\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 75.4% accurate, 1.7× speedup?

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

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

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

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

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

    \[\leadsto x + \color{blue}{y \cdot z} \]
  6. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto x + \color{blue}{y \cdot z} \]
    2. +-commutativeN/A

      \[\leadsto \color{blue}{y \cdot z + x} \]
    3. lift-*.f64N/A

      \[\leadsto \color{blue}{y \cdot z} + x \]
    4. *-commutativeN/A

      \[\leadsto \color{blue}{z \cdot y} + x \]
    5. lower-fma.f6473.2

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, y, x\right)} \]
  7. Applied rewrites73.2%

    \[\leadsto \color{blue}{\mathsf{fma}\left(z, y, x\right)} \]
  8. Add Preprocessing

Alternative 5: 41.2% accurate, 2.0× speedup?

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

\\
y \cdot z
\end{array}
Derivation
  1. Initial program 100.0%

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

    \[\leadsto \color{blue}{y \cdot z} \]
  4. Step-by-step derivation
    1. lower-*.f6438.9

      \[\leadsto \color{blue}{y \cdot z} \]
  5. Applied rewrites38.9%

    \[\leadsto \color{blue}{y \cdot z} \]
  6. Add Preprocessing

Reproduce

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