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

Percentage Accurate: 100.0% → 100.0%
Time: 1.3s
Alternatives: 6
Speedup: 1.1×

Specification

?
\[x + \left(y - x\right) \cdot z \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (+ x (* (- y x) z)))
double code(double x, double y, double z) {
	return x + ((y - x) * z);
}
real(8) function code(x, y, z)
use fmin_fmax_functions
    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]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	x + ((y - x) * z)
END code
x + \left(y - x\right) \cdot z

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 6 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?

\[x + \left(y - x\right) \cdot z \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (+ x (* (- y x) z)))
double code(double x, double y, double z) {
	return x + ((y - x) * z);
}
real(8) function code(x, y, z)
use fmin_fmax_functions
    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]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	x + ((y - x) * z)
END code
x + \left(y - x\right) \cdot z

Alternative 1: 100.0% accurate, 1.1× speedup?

\[\mathsf{fma}\left(z, y - x, x\right) \]
(FPCore (x y z)
  :precision binary64
  :pre TRUE
  (fma z (- y x) x))
double code(double x, double y, double z) {
	return fma(z, (y - x), x);
}
function code(x, y, z)
	return fma(z, Float64(y - x), x)
end
code[x_, y_, z_] := N[(z * N[(y - x), $MachinePrecision] + x), $MachinePrecision]
f(x, y, z):
	x in [-inf, +inf],
	y in [-inf, +inf],
	z in [-inf, +inf]
code: THEORY
BEGIN
f(x, y, z: real): real =
	(z * (y - x)) + x
END code
\mathsf{fma}\left(z, y - x, x\right)
Derivation
  1. Initial program 100.0%

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

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

    Alternative 2: 98.6% accurate, 0.6× speedup?

    \[\begin{array}{l} t_0 := z \cdot \left(y - x\right)\\ \mathbf{if}\;z \leq -28165673201.033638:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z \leq 7.845250112058621 \cdot 10^{-9}:\\ \;\;\;\;\mathsf{fma}\left(z, y, x\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \]
    (FPCore (x y z)
      :precision binary64
      :pre TRUE
      (let* ((t_0 (* z (- y x))))
      (if (<= z -28165673201.033638)
        t_0
        (if (<= z 7.845250112058621e-9) (fma z y x) t_0))))
    double code(double x, double y, double z) {
    	double t_0 = z * (y - x);
    	double tmp;
    	if (z <= -28165673201.033638) {
    		tmp = t_0;
    	} else if (z <= 7.845250112058621e-9) {
    		tmp = fma(z, y, x);
    	} else {
    		tmp = t_0;
    	}
    	return tmp;
    }
    
    function code(x, y, z)
    	t_0 = Float64(z * Float64(y - x))
    	tmp = 0.0
    	if (z <= -28165673201.033638)
    		tmp = t_0;
    	elseif (z <= 7.845250112058621e-9)
    		tmp = fma(z, y, x);
    	else
    		tmp = t_0;
    	end
    	return tmp
    end
    
    code[x_, y_, z_] := Block[{t$95$0 = N[(z * N[(y - x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, -28165673201.033638], t$95$0, If[LessEqual[z, 7.845250112058621e-9], N[(z * y + x), $MachinePrecision], t$95$0]]]
    
    f(x, y, z):
    	x in [-inf, +inf],
    	y in [-inf, +inf],
    	z in [-inf, +inf]
    code: THEORY
    BEGIN
    f(x, y, z: real): real =
    	LET t_0 = (z * (y - x)) IN
    		LET tmp_1 = IF (z <= (78452501120586211901078397558915999976392185999429784715175628662109375e-79)) THEN ((z * y) + x) ELSE t_0 ENDIF IN
    		LET tmp = IF (z <= (-2816567320103363800048828125e-17)) THEN t_0 ELSE tmp_1 ENDIF IN
    	tmp
    END code
    \begin{array}{l}
    t_0 := z \cdot \left(y - x\right)\\
    \mathbf{if}\;z \leq -28165673201.033638:\\
    \;\;\;\;t\_0\\
    
    \mathbf{elif}\;z \leq 7.845250112058621 \cdot 10^{-9}:\\
    \;\;\;\;\mathsf{fma}\left(z, y, x\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;t\_0\\
    
    
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if z < -28165673201.033638 or 7.8452501120586212e-9 < z

      1. Initial program 100.0%

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

          \[\leadsto \mathsf{fma}\left(z, y - x, x\right) \]
        2. Taylor expanded in x around 0

          \[\leadsto y \cdot z \]
        3. Step-by-step derivation
          1. Applied rewrites41.4%

            \[\leadsto y \cdot z \]
          2. Taylor expanded in z around inf

            \[\leadsto z \cdot \left(y - x\right) \]
          3. Step-by-step derivation
            1. Applied rewrites65.0%

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

            if -28165673201.033638 < z < 7.8452501120586212e-9

            1. Initial program 100.0%

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

                \[\leadsto \mathsf{fma}\left(z, y - x, x\right) \]
              2. Taylor expanded in x around 0

                \[\leadsto \mathsf{fma}\left(z, y, x\right) \]
              3. Step-by-step derivation
                1. Applied rewrites75.7%

                  \[\leadsto \mathsf{fma}\left(z, y, x\right) \]
              4. Recombined 2 regimes into one program.
              5. Add Preprocessing

              Alternative 3: 86.0% accurate, 0.6× speedup?

              \[\begin{array}{l} t_0 := x \cdot \left(1 - z\right)\\ \mathbf{if}\;x \leq -1.9574573609995736 \cdot 10^{+91}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;x \leq 0.0005660629841236394:\\ \;\;\;\;\mathsf{fma}\left(z, y, x\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \]
              (FPCore (x y z)
                :precision binary64
                :pre TRUE
                (let* ((t_0 (* x (- 1.0 z))))
                (if (<= x -1.9574573609995736e+91)
                  t_0
                  (if (<= x 0.0005660629841236394) (fma z y x) t_0))))
              double code(double x, double y, double z) {
              	double t_0 = x * (1.0 - z);
              	double tmp;
              	if (x <= -1.9574573609995736e+91) {
              		tmp = t_0;
              	} else if (x <= 0.0005660629841236394) {
              		tmp = fma(z, y, x);
              	} else {
              		tmp = t_0;
              	}
              	return tmp;
              }
              
              function code(x, y, z)
              	t_0 = Float64(x * Float64(1.0 - z))
              	tmp = 0.0
              	if (x <= -1.9574573609995736e+91)
              		tmp = t_0;
              	elseif (x <= 0.0005660629841236394)
              		tmp = fma(z, y, x);
              	else
              		tmp = t_0;
              	end
              	return tmp
              end
              
              code[x_, y_, z_] := Block[{t$95$0 = N[(x * N[(1.0 - z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -1.9574573609995736e+91], t$95$0, If[LessEqual[x, 0.0005660629841236394], N[(z * y + x), $MachinePrecision], t$95$0]]]
              
              f(x, y, z):
              	x in [-inf, +inf],
              	y in [-inf, +inf],
              	z in [-inf, +inf]
              code: THEORY
              BEGIN
              f(x, y, z: real): real =
              	LET t_0 = (x * ((1) - z)) IN
              		LET tmp_1 = IF (x <= (566062984123639448448550570702764161978848278522491455078125e-63)) THEN ((z * y) + x) ELSE t_0 ENDIF IN
              		LET tmp = IF (x <= (-19574573609995735600635967846953792076791839711159263895675501474379448978998529820238807040)) THEN t_0 ELSE tmp_1 ENDIF IN
              	tmp
              END code
              \begin{array}{l}
              t_0 := x \cdot \left(1 - z\right)\\
              \mathbf{if}\;x \leq -1.9574573609995736 \cdot 10^{+91}:\\
              \;\;\;\;t\_0\\
              
              \mathbf{elif}\;x \leq 0.0005660629841236394:\\
              \;\;\;\;\mathsf{fma}\left(z, y, x\right)\\
              
              \mathbf{else}:\\
              \;\;\;\;t\_0\\
              
              
              \end{array}
              
              Derivation
              1. Split input into 2 regimes
              2. if x < -1.9574573609995736e91 or 5.6606298412363945e-4 < x

                1. Initial program 100.0%

                  \[x + \left(y - x\right) \cdot z \]
                2. Applied rewrites98.9%

                  \[\leadsto \mathsf{fma}\left(x, 1 - z, z \cdot y\right) \]
                3. Taylor expanded in x around inf

                  \[\leadsto x \cdot \left(1 - z\right) \]
                4. Step-by-step derivation
                  1. Applied rewrites62.4%

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

                  if -1.9574573609995736e91 < x < 5.6606298412363945e-4

                  1. Initial program 100.0%

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

                      \[\leadsto \mathsf{fma}\left(z, y - x, x\right) \]
                    2. Taylor expanded in x around 0

                      \[\leadsto \mathsf{fma}\left(z, y, x\right) \]
                    3. Step-by-step derivation
                      1. Applied rewrites75.7%

                        \[\leadsto \mathsf{fma}\left(z, y, x\right) \]
                    4. Recombined 2 regimes into one program.
                    5. Add Preprocessing

                    Alternative 4: 75.7% accurate, 1.5× speedup?

                    \[\mathsf{fma}\left(z, y, x\right) \]
                    (FPCore (x y z)
                      :precision binary64
                      :pre TRUE
                      (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]
                    
                    f(x, y, z):
                    	x in [-inf, +inf],
                    	y in [-inf, +inf],
                    	z in [-inf, +inf]
                    code: THEORY
                    BEGIN
                    f(x, y, z: real): real =
                    	(z * y) + x
                    END code
                    \mathsf{fma}\left(z, y, x\right)
                    
                    Derivation
                    1. Initial program 100.0%

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

                        \[\leadsto \mathsf{fma}\left(z, y - x, x\right) \]
                      2. Taylor expanded in x around 0

                        \[\leadsto \mathsf{fma}\left(z, y, x\right) \]
                      3. Step-by-step derivation
                        1. Applied rewrites75.7%

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

                        Alternative 5: 60.1% accurate, 0.8× speedup?

                        \[\begin{array}{l} \mathbf{if}\;z \leq -1.5998878487717716 \cdot 10^{-87}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;z \leq 1.4132947115915006 \cdot 10^{-40}:\\ \;\;\;\;x \cdot 1\\ \mathbf{else}:\\ \;\;\;\;y \cdot z\\ \end{array} \]
                        (FPCore (x y z)
                          :precision binary64
                          :pre TRUE
                          (if (<= z -1.5998878487717716e-87)
                          (* y z)
                          (if (<= z 1.4132947115915006e-40) (* x 1.0) (* y z))))
                        double code(double x, double y, double z) {
                        	double tmp;
                        	if (z <= -1.5998878487717716e-87) {
                        		tmp = y * z;
                        	} else if (z <= 1.4132947115915006e-40) {
                        		tmp = x * 1.0;
                        	} else {
                        		tmp = y * z;
                        	}
                        	return tmp;
                        }
                        
                        real(8) function code(x, y, z)
                        use fmin_fmax_functions
                            real(8), intent (in) :: x
                            real(8), intent (in) :: y
                            real(8), intent (in) :: z
                            real(8) :: tmp
                            if (z <= (-1.5998878487717716d-87)) then
                                tmp = y * z
                            else if (z <= 1.4132947115915006d-40) then
                                tmp = x * 1.0d0
                            else
                                tmp = y * z
                            end if
                            code = tmp
                        end function
                        
                        public static double code(double x, double y, double z) {
                        	double tmp;
                        	if (z <= -1.5998878487717716e-87) {
                        		tmp = y * z;
                        	} else if (z <= 1.4132947115915006e-40) {
                        		tmp = x * 1.0;
                        	} else {
                        		tmp = y * z;
                        	}
                        	return tmp;
                        }
                        
                        def code(x, y, z):
                        	tmp = 0
                        	if z <= -1.5998878487717716e-87:
                        		tmp = y * z
                        	elif z <= 1.4132947115915006e-40:
                        		tmp = x * 1.0
                        	else:
                        		tmp = y * z
                        	return tmp
                        
                        function code(x, y, z)
                        	tmp = 0.0
                        	if (z <= -1.5998878487717716e-87)
                        		tmp = Float64(y * z);
                        	elseif (z <= 1.4132947115915006e-40)
                        		tmp = Float64(x * 1.0);
                        	else
                        		tmp = Float64(y * z);
                        	end
                        	return tmp
                        end
                        
                        function tmp_2 = code(x, y, z)
                        	tmp = 0.0;
                        	if (z <= -1.5998878487717716e-87)
                        		tmp = y * z;
                        	elseif (z <= 1.4132947115915006e-40)
                        		tmp = x * 1.0;
                        	else
                        		tmp = y * z;
                        	end
                        	tmp_2 = tmp;
                        end
                        
                        code[x_, y_, z_] := If[LessEqual[z, -1.5998878487717716e-87], N[(y * z), $MachinePrecision], If[LessEqual[z, 1.4132947115915006e-40], N[(x * 1.0), $MachinePrecision], N[(y * z), $MachinePrecision]]]
                        
                        f(x, y, z):
                        	x in [-inf, +inf],
                        	y in [-inf, +inf],
                        	z in [-inf, +inf]
                        code: THEORY
                        BEGIN
                        f(x, y, z: real): real =
                        	LET tmp_1 = IF (z <= (14132947115915006468182910235642061199906833362565967651395812855413318177857125598375794078304482317867511731446228395725484006106853485107421875e-185)) THEN (x * (1)) ELSE (y * z) ENDIF IN
                        	LET tmp = IF (z <= (-159988784877177164685523371169830722870522374072384409119026858684832480050318864005140114871679013601761552483193181404403016128515199488354968070059220853336754264519220820635743157089849151568897638446024723319315030689580225953250192105770111083984375e-341)) THEN (y * z) ELSE tmp_1 ENDIF IN
                        	tmp
                        END code
                        \begin{array}{l}
                        \mathbf{if}\;z \leq -1.5998878487717716 \cdot 10^{-87}:\\
                        \;\;\;\;y \cdot z\\
                        
                        \mathbf{elif}\;z \leq 1.4132947115915006 \cdot 10^{-40}:\\
                        \;\;\;\;x \cdot 1\\
                        
                        \mathbf{else}:\\
                        \;\;\;\;y \cdot z\\
                        
                        
                        \end{array}
                        
                        Derivation
                        1. Split input into 2 regimes
                        2. if z < -1.5998878487717716e-87 or 1.4132947115915006e-40 < z

                          1. Initial program 100.0%

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

                              \[\leadsto \mathsf{fma}\left(z, y - x, x\right) \]
                            2. Taylor expanded in x around 0

                              \[\leadsto y \cdot z \]
                            3. Step-by-step derivation
                              1. Applied rewrites41.4%

                                \[\leadsto y \cdot z \]

                              if -1.5998878487717716e-87 < z < 1.4132947115915006e-40

                              1. Initial program 100.0%

                                \[x + \left(y - x\right) \cdot z \]
                              2. Applied rewrites98.9%

                                \[\leadsto \mathsf{fma}\left(x, 1 - z, z \cdot y\right) \]
                              3. Taylor expanded in x around inf

                                \[\leadsto x \cdot \left(1 - z\right) \]
                              4. Step-by-step derivation
                                1. Applied rewrites62.4%

                                  \[\leadsto x \cdot \left(1 - z\right) \]
                                2. Taylor expanded in z around 0

                                  \[\leadsto x \cdot 1 \]
                                3. Step-by-step derivation
                                  1. Applied rewrites36.6%

                                    \[\leadsto x \cdot 1 \]
                                4. Recombined 2 regimes into one program.
                                5. Add Preprocessing

                                Alternative 6: 36.6% accurate, 2.3× speedup?

                                \[x \cdot 1 \]
                                (FPCore (x y z)
                                  :precision binary64
                                  :pre TRUE
                                  (* x 1.0))
                                double code(double x, double y, double z) {
                                	return x * 1.0;
                                }
                                
                                real(8) function code(x, y, z)
                                use fmin_fmax_functions
                                    real(8), intent (in) :: x
                                    real(8), intent (in) :: y
                                    real(8), intent (in) :: z
                                    code = x * 1.0d0
                                end function
                                
                                public static double code(double x, double y, double z) {
                                	return x * 1.0;
                                }
                                
                                def code(x, y, z):
                                	return x * 1.0
                                
                                function code(x, y, z)
                                	return Float64(x * 1.0)
                                end
                                
                                function tmp = code(x, y, z)
                                	tmp = x * 1.0;
                                end
                                
                                code[x_, y_, z_] := N[(x * 1.0), $MachinePrecision]
                                
                                f(x, y, z):
                                	x in [-inf, +inf],
                                	y in [-inf, +inf],
                                	z in [-inf, +inf]
                                code: THEORY
                                BEGIN
                                f(x, y, z: real): real =
                                	x * (1)
                                END code
                                x \cdot 1
                                
                                Derivation
                                1. Initial program 100.0%

                                  \[x + \left(y - x\right) \cdot z \]
                                2. Applied rewrites98.9%

                                  \[\leadsto \mathsf{fma}\left(x, 1 - z, z \cdot y\right) \]
                                3. Taylor expanded in x around inf

                                  \[\leadsto x \cdot \left(1 - z\right) \]
                                4. Step-by-step derivation
                                  1. Applied rewrites62.4%

                                    \[\leadsto x \cdot \left(1 - z\right) \]
                                  2. Taylor expanded in z around 0

                                    \[\leadsto x \cdot 1 \]
                                  3. Step-by-step derivation
                                    1. Applied rewrites36.6%

                                      \[\leadsto x \cdot 1 \]
                                    2. Add Preprocessing

                                    Reproduce

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