Statistics.Sample:$swelfordMean from math-functions-0.1.5.2

Percentage Accurate: 100.0% → 100.0%
Time: 3.8s
Alternatives: 5
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ x + \frac{y - x}{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 + \frac{y - x}{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 + \frac{y - x}{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 + \frac{y - x}{z}
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x + \frac{y - x}{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 + \frac{y - x}{z}
\end{array}
Derivation
  1. Initial program 100.0%

    \[x + \frac{y - x}{z} \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 84.8% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;z \leq -6.2 \cdot 10^{+18} \lor \neg \left(z \leq 9 \cdot 10^{+14}\right):\\
\;\;\;\;x - \frac{x}{z}\\

\mathbf{else}:\\
\;\;\;\;\frac{y - x}{z}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -6.2e18 or 9e14 < z

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{x \cdot \left(1 - \frac{1}{z}\right)} \]
    4. Step-by-step derivation
      1. distribute-lft-out--N/A

        \[\leadsto \color{blue}{x \cdot 1 - x \cdot \frac{1}{z}} \]
      2. *-rgt-identityN/A

        \[\leadsto \color{blue}{x} - x \cdot \frac{1}{z} \]
      3. associate-*r/N/A

        \[\leadsto x - \color{blue}{\frac{x \cdot 1}{z}} \]
      4. *-rgt-identityN/A

        \[\leadsto x - \frac{\color{blue}{x}}{z} \]
      5. lower--.f64N/A

        \[\leadsto \color{blue}{x - \frac{x}{z}} \]
      6. lower-/.f6479.6

        \[\leadsto x - \color{blue}{\frac{x}{z}} \]
    5. Applied rewrites79.6%

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

    if -6.2e18 < z < 9e14

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Add Preprocessing
    3. Taylor expanded in z around 0

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

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

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

      \[\leadsto \color{blue}{\frac{y - x}{z}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification89.4%

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

Alternative 3: 75.0% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;y \leq -8.4 \cdot 10^{+76} \lor \neg \left(y \leq 1.88\right):\\
\;\;\;\;\frac{y}{z}\\

\mathbf{else}:\\
\;\;\;\;x - \frac{x}{z}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -8.40000000000000027e76 or 1.8799999999999999 < y

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{y}{z}} \]
    4. Step-by-step derivation
      1. lower-/.f6469.8

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

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

    if -8.40000000000000027e76 < y < 1.8799999999999999

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{x \cdot \left(1 - \frac{1}{z}\right)} \]
    4. Step-by-step derivation
      1. distribute-lft-out--N/A

        \[\leadsto \color{blue}{x \cdot 1 - x \cdot \frac{1}{z}} \]
      2. *-rgt-identityN/A

        \[\leadsto \color{blue}{x} - x \cdot \frac{1}{z} \]
      3. associate-*r/N/A

        \[\leadsto x - \color{blue}{\frac{x \cdot 1}{z}} \]
      4. *-rgt-identityN/A

        \[\leadsto x - \frac{\color{blue}{x}}{z} \]
      5. lower--.f64N/A

        \[\leadsto \color{blue}{x - \frac{x}{z}} \]
      6. lower-/.f6485.0

        \[\leadsto x - \color{blue}{\frac{x}{z}} \]
    5. Applied rewrites85.0%

      \[\leadsto \color{blue}{x - \frac{x}{z}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification78.0%

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

Alternative 4: 50.8% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;y \leq -4.6 \cdot 10^{+46} \lor \neg \left(y \leq 10^{-54}\right):\\
\;\;\;\;\frac{y}{z}\\

\mathbf{else}:\\
\;\;\;\;\frac{-x}{z}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -4.6000000000000001e46 or 1e-54 < y

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{y}{z}} \]
    4. Step-by-step derivation
      1. lower-/.f6466.2

        \[\leadsto \color{blue}{\frac{y}{z}} \]
    5. Applied rewrites66.2%

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

    if -4.6000000000000001e46 < y < 1e-54

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Add Preprocessing
    3. Taylor expanded in z around 0

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

        \[\leadsto \color{blue}{\frac{y - x}{z}} \]
      2. lower--.f6456.3

        \[\leadsto \frac{\color{blue}{y - x}}{z} \]
    5. Applied rewrites56.3%

      \[\leadsto \color{blue}{\frac{y - x}{z}} \]
    6. Taylor expanded in x around inf

      \[\leadsto \frac{-1 \cdot x}{z} \]
    7. Step-by-step derivation
      1. Applied rewrites47.6%

        \[\leadsto \frac{-x}{z} \]
    8. Recombined 2 regimes into one program.
    9. Final simplification57.8%

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

    Alternative 5: 41.5% accurate, 1.5× speedup?

    \[\begin{array}{l} \\ \frac{y}{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}
    
    \\
    \frac{y}{z}
    \end{array}
    
    Derivation
    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\frac{y}{z}} \]
    4. Step-by-step derivation
      1. lower-/.f6440.8

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

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

    Reproduce

    ?
    herbie shell --seed 2024337 
    (FPCore (x y z)
      :name "Statistics.Sample:$swelfordMean from math-functions-0.1.5.2"
      :precision binary64
      (+ x (/ (- y x) z)))