Numeric.SpecFunctions:choose from math-functions-0.1.5.2

Percentage Accurate: 84.3% → 96.6%
Time: 7.3s
Alternatives: 5
Speedup: 0.5×

Specification

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

\\
\frac{x \cdot \left(y + z\right)}{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: 84.3% accurate, 1.0× speedup?

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

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

Alternative 1: 96.6% accurate, 0.4× speedup?

\[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ x\_s \cdot \begin{array}{l} \mathbf{if}\;\frac{\left(z + y\right) \cdot x\_m}{z} \leq -2 \cdot 10^{+27}:\\ \;\;\;\;\frac{y \cdot x\_m}{z}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{z} \cdot y, x\_m, x\_m\right)\\ \end{array} \end{array} \]
x\_m = (fabs.f64 x)
x\_s = (copysign.f64 #s(literal 1 binary64) x)
(FPCore (x_s x_m y z)
 :precision binary64
 (*
  x_s
  (if (<= (/ (* (+ z y) x_m) z) -2e+27)
    (/ (* y x_m) z)
    (fma (* (/ 1.0 z) y) x_m x_m))))
x\_m = fabs(x);
x\_s = copysign(1.0, x);
double code(double x_s, double x_m, double y, double z) {
	double tmp;
	if ((((z + y) * x_m) / z) <= -2e+27) {
		tmp = (y * x_m) / z;
	} else {
		tmp = fma(((1.0 / z) * y), x_m, x_m);
	}
	return x_s * tmp;
}
x\_m = abs(x)
x\_s = copysign(1.0, x)
function code(x_s, x_m, y, z)
	tmp = 0.0
	if (Float64(Float64(Float64(z + y) * x_m) / z) <= -2e+27)
		tmp = Float64(Float64(y * x_m) / z);
	else
		tmp = fma(Float64(Float64(1.0 / z) * y), x_m, x_m);
	end
	return Float64(x_s * tmp)
end
x\_m = N[Abs[x], $MachinePrecision]
x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[x$95$s_, x$95$m_, y_, z_] := N[(x$95$s * If[LessEqual[N[(N[(N[(z + y), $MachinePrecision] * x$95$m), $MachinePrecision] / z), $MachinePrecision], -2e+27], N[(N[(y * x$95$m), $MachinePrecision] / z), $MachinePrecision], N[(N[(N[(1.0 / z), $MachinePrecision] * y), $MachinePrecision] * x$95$m + x$95$m), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
x\_m = \left|x\right|
\\
x\_s = \mathsf{copysign}\left(1, x\right)

\\
x\_s \cdot \begin{array}{l}
\mathbf{if}\;\frac{\left(z + y\right) \cdot x\_m}{z} \leq -2 \cdot 10^{+27}:\\
\;\;\;\;\frac{y \cdot x\_m}{z}\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{z} \cdot y, x\_m, x\_m\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (*.f64 x (+.f64 y z)) z) < -2e27

    1. Initial program 97.9%

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

      \[\leadsto \frac{\color{blue}{x \cdot y}}{z} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{\color{blue}{y \cdot x}}{z} \]
      2. lower-*.f6497.9

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

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

    if -2e27 < (/.f64 (*.f64 x (+.f64 y z)) z)

    1. Initial program 83.6%

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

      \[\leadsto \color{blue}{\frac{x \cdot \left(y + z\right)}{z}} \]
    4. Step-by-step derivation
      1. associate-/l*N/A

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

        \[\leadsto x \cdot \frac{\color{blue}{z + y}}{z} \]
      3. *-lft-identityN/A

        \[\leadsto x \cdot \frac{z + \color{blue}{1 \cdot y}}{z} \]
      4. metadata-evalN/A

        \[\leadsto x \cdot \frac{z + \color{blue}{\left(\mathsf{neg}\left(-1\right)\right)} \cdot y}{z} \]
      5. cancel-sign-sub-invN/A

        \[\leadsto x \cdot \frac{\color{blue}{z - -1 \cdot y}}{z} \]
      6. div-subN/A

        \[\leadsto x \cdot \color{blue}{\left(\frac{z}{z} - \frac{-1 \cdot y}{z}\right)} \]
      7. *-inversesN/A

        \[\leadsto x \cdot \left(\color{blue}{1} - \frac{-1 \cdot y}{z}\right) \]
      8. associate-*r/N/A

        \[\leadsto x \cdot \left(1 - \color{blue}{-1 \cdot \frac{y}{z}}\right) \]
      9. distribute-rgt-out--N/A

        \[\leadsto \color{blue}{1 \cdot x - \left(-1 \cdot \frac{y}{z}\right) \cdot x} \]
      10. *-lft-identityN/A

        \[\leadsto \color{blue}{x} - \left(-1 \cdot \frac{y}{z}\right) \cdot x \]
      11. mul-1-negN/A

        \[\leadsto x - \color{blue}{\left(\mathsf{neg}\left(\frac{y}{z}\right)\right)} \cdot x \]
      12. cancel-sign-subN/A

        \[\leadsto \color{blue}{x + \frac{y}{z} \cdot x} \]
      13. distribute-rgt1-inN/A

        \[\leadsto \color{blue}{\left(\frac{y}{z} + 1\right) \cdot x} \]
      14. distribute-lft1-inN/A

        \[\leadsto \color{blue}{\frac{y}{z} \cdot x + x} \]
      15. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{y}{z}, x, x\right)} \]
      16. lower-/.f6498.5

        \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{y}{z}}, x, x\right) \]
    5. Applied rewrites98.5%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{y}{z}, x, x\right)} \]
    6. Step-by-step derivation
      1. Applied rewrites98.5%

        \[\leadsto \mathsf{fma}\left(\frac{1}{z} \cdot y, x, x\right) \]
    7. Recombined 2 regimes into one program.
    8. Final simplification98.4%

      \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{\left(z + y\right) \cdot x}{z} \leq -2 \cdot 10^{+27}:\\ \;\;\;\;\frac{y \cdot x}{z}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{1}{z} \cdot y, x, x\right)\\ \end{array} \]
    9. Add Preprocessing

    Alternative 2: 70.8% accurate, 0.3× speedup?

    \[\begin{array}{l} x\_m = \left|x\right| \\ x\_s = \mathsf{copysign}\left(1, x\right) \\ \begin{array}{l} t_0 := \frac{\left(z + y\right) \cdot x\_m}{z}\\ t_1 := \frac{y \cdot x\_m}{z}\\ x\_s \cdot \begin{array}{l} \mathbf{if}\;t\_0 \leq -2 \cdot 10^{-234}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;t\_0 \leq 10^{+220}:\\ \;\;\;\;-1 \cdot \left(-x\_m\right)\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \end{array} \]
    x\_m = (fabs.f64 x)
    x\_s = (copysign.f64 #s(literal 1 binary64) x)
    (FPCore (x_s x_m y z)
     :precision binary64
     (let* ((t_0 (/ (* (+ z y) x_m) z)) (t_1 (/ (* y x_m) z)))
       (*
        x_s
        (if (<= t_0 -2e-234) t_1 (if (<= t_0 1e+220) (* -1.0 (- x_m)) t_1)))))
    x\_m = fabs(x);
    x\_s = copysign(1.0, x);
    double code(double x_s, double x_m, double y, double z) {
    	double t_0 = ((z + y) * x_m) / z;
    	double t_1 = (y * x_m) / z;
    	double tmp;
    	if (t_0 <= -2e-234) {
    		tmp = t_1;
    	} else if (t_0 <= 1e+220) {
    		tmp = -1.0 * -x_m;
    	} else {
    		tmp = t_1;
    	}
    	return x_s * tmp;
    }
    
    x\_m = abs(x)
    x\_s = copysign(1.0d0, x)
    real(8) function code(x_s, x_m, y, z)
        real(8), intent (in) :: x_s
        real(8), intent (in) :: x_m
        real(8), intent (in) :: y
        real(8), intent (in) :: z
        real(8) :: t_0
        real(8) :: t_1
        real(8) :: tmp
        t_0 = ((z + y) * x_m) / z
        t_1 = (y * x_m) / z
        if (t_0 <= (-2d-234)) then
            tmp = t_1
        else if (t_0 <= 1d+220) then
            tmp = (-1.0d0) * -x_m
        else
            tmp = t_1
        end if
        code = x_s * tmp
    end function
    
    x\_m = Math.abs(x);
    x\_s = Math.copySign(1.0, x);
    public static double code(double x_s, double x_m, double y, double z) {
    	double t_0 = ((z + y) * x_m) / z;
    	double t_1 = (y * x_m) / z;
    	double tmp;
    	if (t_0 <= -2e-234) {
    		tmp = t_1;
    	} else if (t_0 <= 1e+220) {
    		tmp = -1.0 * -x_m;
    	} else {
    		tmp = t_1;
    	}
    	return x_s * tmp;
    }
    
    x\_m = math.fabs(x)
    x\_s = math.copysign(1.0, x)
    def code(x_s, x_m, y, z):
    	t_0 = ((z + y) * x_m) / z
    	t_1 = (y * x_m) / z
    	tmp = 0
    	if t_0 <= -2e-234:
    		tmp = t_1
    	elif t_0 <= 1e+220:
    		tmp = -1.0 * -x_m
    	else:
    		tmp = t_1
    	return x_s * tmp
    
    x\_m = abs(x)
    x\_s = copysign(1.0, x)
    function code(x_s, x_m, y, z)
    	t_0 = Float64(Float64(Float64(z + y) * x_m) / z)
    	t_1 = Float64(Float64(y * x_m) / z)
    	tmp = 0.0
    	if (t_0 <= -2e-234)
    		tmp = t_1;
    	elseif (t_0 <= 1e+220)
    		tmp = Float64(-1.0 * Float64(-x_m));
    	else
    		tmp = t_1;
    	end
    	return Float64(x_s * tmp)
    end
    
    x\_m = abs(x);
    x\_s = sign(x) * abs(1.0);
    function tmp_2 = code(x_s, x_m, y, z)
    	t_0 = ((z + y) * x_m) / z;
    	t_1 = (y * x_m) / z;
    	tmp = 0.0;
    	if (t_0 <= -2e-234)
    		tmp = t_1;
    	elseif (t_0 <= 1e+220)
    		tmp = -1.0 * -x_m;
    	else
    		tmp = t_1;
    	end
    	tmp_2 = x_s * tmp;
    end
    
    x\_m = N[Abs[x], $MachinePrecision]
    x\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[x]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
    code[x$95$s_, x$95$m_, y_, z_] := Block[{t$95$0 = N[(N[(N[(z + y), $MachinePrecision] * x$95$m), $MachinePrecision] / z), $MachinePrecision]}, Block[{t$95$1 = N[(N[(y * x$95$m), $MachinePrecision] / z), $MachinePrecision]}, N[(x$95$s * If[LessEqual[t$95$0, -2e-234], t$95$1, If[LessEqual[t$95$0, 1e+220], N[(-1.0 * (-x$95$m)), $MachinePrecision], t$95$1]]), $MachinePrecision]]]
    
    \begin{array}{l}
    x\_m = \left|x\right|
    \\
    x\_s = \mathsf{copysign}\left(1, x\right)
    
    \\
    \begin{array}{l}
    t_0 := \frac{\left(z + y\right) \cdot x\_m}{z}\\
    t_1 := \frac{y \cdot x\_m}{z}\\
    x\_s \cdot \begin{array}{l}
    \mathbf{if}\;t\_0 \leq -2 \cdot 10^{-234}:\\
    \;\;\;\;t\_1\\
    
    \mathbf{elif}\;t\_0 \leq 10^{+220}:\\
    \;\;\;\;-1 \cdot \left(-x\_m\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;t\_1\\
    
    
    \end{array}
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (/.f64 (*.f64 x (+.f64 y z)) z) < -1.9999999999999999e-234 or 1e220 < (/.f64 (*.f64 x (+.f64 y z)) z)

      1. Initial program 75.6%

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

        \[\leadsto \frac{\color{blue}{x \cdot y}}{z} \]
      4. Step-by-step derivation
        1. *-commutativeN/A

          \[\leadsto \frac{\color{blue}{y \cdot x}}{z} \]
        2. lower-*.f6468.8

          \[\leadsto \frac{\color{blue}{y \cdot x}}{z} \]
      5. Applied rewrites68.8%

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

      if -1.9999999999999999e-234 < (/.f64 (*.f64 x (+.f64 y z)) z) < 1e220

      1. Initial program 92.6%

        \[\frac{x \cdot \left(y + z\right)}{z} \]
      2. Add Preprocessing
      3. Step-by-step derivation
        1. lift-/.f64N/A

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

          \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(x \cdot \left(y + z\right)\right)}{\mathsf{neg}\left(z\right)}} \]
        3. div-invN/A

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

          \[\leadsto \left(\mathsf{neg}\left(\color{blue}{x \cdot \left(y + z\right)}\right)\right) \cdot \frac{1}{\mathsf{neg}\left(z\right)} \]
        5. distribute-lft-neg-inN/A

          \[\leadsto \color{blue}{\left(\left(\mathsf{neg}\left(x\right)\right) \cdot \left(y + z\right)\right)} \cdot \frac{1}{\mathsf{neg}\left(z\right)} \]
        6. associate-*l*N/A

          \[\leadsto \color{blue}{\left(\mathsf{neg}\left(x\right)\right) \cdot \left(\left(y + z\right) \cdot \frac{1}{\mathsf{neg}\left(z\right)}\right)} \]
        7. lower-*.f64N/A

          \[\leadsto \color{blue}{\left(\mathsf{neg}\left(x\right)\right) \cdot \left(\left(y + z\right) \cdot \frac{1}{\mathsf{neg}\left(z\right)}\right)} \]
        8. lower-neg.f64N/A

          \[\leadsto \color{blue}{\left(\mathsf{neg}\left(x\right)\right)} \cdot \left(\left(y + z\right) \cdot \frac{1}{\mathsf{neg}\left(z\right)}\right) \]
        9. lower-*.f64N/A

          \[\leadsto \left(\mathsf{neg}\left(x\right)\right) \cdot \color{blue}{\left(\left(y + z\right) \cdot \frac{1}{\mathsf{neg}\left(z\right)}\right)} \]
        10. lift-+.f64N/A

          \[\leadsto \left(\mathsf{neg}\left(x\right)\right) \cdot \left(\color{blue}{\left(y + z\right)} \cdot \frac{1}{\mathsf{neg}\left(z\right)}\right) \]
        11. +-commutativeN/A

          \[\leadsto \left(\mathsf{neg}\left(x\right)\right) \cdot \left(\color{blue}{\left(z + y\right)} \cdot \frac{1}{\mathsf{neg}\left(z\right)}\right) \]
        12. lower-+.f64N/A

          \[\leadsto \left(\mathsf{neg}\left(x\right)\right) \cdot \left(\color{blue}{\left(z + y\right)} \cdot \frac{1}{\mathsf{neg}\left(z\right)}\right) \]
        13. metadata-evalN/A

          \[\leadsto \left(\mathsf{neg}\left(x\right)\right) \cdot \left(\left(z + y\right) \cdot \frac{\color{blue}{\mathsf{neg}\left(-1\right)}}{\mathsf{neg}\left(z\right)}\right) \]
        14. frac-2negN/A

          \[\leadsto \left(\mathsf{neg}\left(x\right)\right) \cdot \left(\left(z + y\right) \cdot \color{blue}{\frac{-1}{z}}\right) \]
        15. lower-/.f6497.5

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

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

        \[\leadsto \left(\mathsf{neg}\left(x\right)\right) \cdot \color{blue}{-1} \]
      6. Step-by-step derivation
        1. Applied rewrites72.6%

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

        \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{\left(z + y\right) \cdot x}{z} \leq -2 \cdot 10^{-234}:\\ \;\;\;\;\frac{y \cdot x}{z}\\ \mathbf{elif}\;\frac{\left(z + y\right) \cdot x}{z} \leq 10^{+220}:\\ \;\;\;\;-1 \cdot \left(-x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{y \cdot x}{z}\\ \end{array} \]
      9. Add Preprocessing

      Developer Target 1: 96.2% accurate, 0.8× speedup?

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

      Reproduce

      ?
      herbie shell --seed 2024230 
      (FPCore (x y z)
        :name "Numeric.SpecFunctions:choose from math-functions-0.1.5.2"
        :precision binary64
      
        :alt
        (! :herbie-platform default (/ x (/ z (+ y z))))
      
        (/ (* x (+ y z)) z))