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

Percentage Accurate: 100.0% → 100.0%
Time: 4.6s
Alternatives: 7
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 7 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. Final simplification100.0%

    \[\leadsto x + \frac{y - x}{z} \]

Alternative 2: 61.3% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{-x}{z}\\ \mathbf{if}\;z \leq -1.12 \cdot 10^{-9}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq -1.4 \cdot 10^{-127}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq -6.8 \cdot 10^{-165}:\\ \;\;\;\;\frac{y}{z}\\ \mathbf{elif}\;z \leq -5.6 \cdot 10^{-253}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.55 \cdot 10^{+36}:\\ \;\;\;\;\frac{y}{z}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (/ (- x) z)))
   (if (<= z -1.12e-9)
     x
     (if (<= z -1.4e-127)
       t_0
       (if (<= z -6.8e-165)
         (/ y z)
         (if (<= z -5.6e-253) t_0 (if (<= z 2.55e+36) (/ y z) x)))))))
double code(double x, double y, double z) {
	double t_0 = -x / z;
	double tmp;
	if (z <= -1.12e-9) {
		tmp = x;
	} else if (z <= -1.4e-127) {
		tmp = t_0;
	} else if (z <= -6.8e-165) {
		tmp = y / z;
	} else if (z <= -5.6e-253) {
		tmp = t_0;
	} else if (z <= 2.55e+36) {
		tmp = y / 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) :: t_0
    real(8) :: tmp
    t_0 = -x / z
    if (z <= (-1.12d-9)) then
        tmp = x
    else if (z <= (-1.4d-127)) then
        tmp = t_0
    else if (z <= (-6.8d-165)) then
        tmp = y / z
    else if (z <= (-5.6d-253)) then
        tmp = t_0
    else if (z <= 2.55d+36) then
        tmp = y / z
    else
        tmp = x
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = -x / z;
	double tmp;
	if (z <= -1.12e-9) {
		tmp = x;
	} else if (z <= -1.4e-127) {
		tmp = t_0;
	} else if (z <= -6.8e-165) {
		tmp = y / z;
	} else if (z <= -5.6e-253) {
		tmp = t_0;
	} else if (z <= 2.55e+36) {
		tmp = y / z;
	} else {
		tmp = x;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = -x / z
	tmp = 0
	if z <= -1.12e-9:
		tmp = x
	elif z <= -1.4e-127:
		tmp = t_0
	elif z <= -6.8e-165:
		tmp = y / z
	elif z <= -5.6e-253:
		tmp = t_0
	elif z <= 2.55e+36:
		tmp = y / z
	else:
		tmp = x
	return tmp
function code(x, y, z)
	t_0 = Float64(Float64(-x) / z)
	tmp = 0.0
	if (z <= -1.12e-9)
		tmp = x;
	elseif (z <= -1.4e-127)
		tmp = t_0;
	elseif (z <= -6.8e-165)
		tmp = Float64(y / z);
	elseif (z <= -5.6e-253)
		tmp = t_0;
	elseif (z <= 2.55e+36)
		tmp = Float64(y / z);
	else
		tmp = x;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = -x / z;
	tmp = 0.0;
	if (z <= -1.12e-9)
		tmp = x;
	elseif (z <= -1.4e-127)
		tmp = t_0;
	elseif (z <= -6.8e-165)
		tmp = y / z;
	elseif (z <= -5.6e-253)
		tmp = t_0;
	elseif (z <= 2.55e+36)
		tmp = y / z;
	else
		tmp = x;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[((-x) / z), $MachinePrecision]}, If[LessEqual[z, -1.12e-9], x, If[LessEqual[z, -1.4e-127], t$95$0, If[LessEqual[z, -6.8e-165], N[(y / z), $MachinePrecision], If[LessEqual[z, -5.6e-253], t$95$0, If[LessEqual[z, 2.55e+36], N[(y / z), $MachinePrecision], x]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{-x}{z}\\
\mathbf{if}\;z \leq -1.12 \cdot 10^{-9}:\\
\;\;\;\;x\\

\mathbf{elif}\;z \leq -1.4 \cdot 10^{-127}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;z \leq -6.8 \cdot 10^{-165}:\\
\;\;\;\;\frac{y}{z}\\

\mathbf{elif}\;z \leq -5.6 \cdot 10^{-253}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;z \leq 2.55 \cdot 10^{+36}:\\
\;\;\;\;\frac{y}{z}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if z < -1.12000000000000006e-9 or 2.54999999999999986e36 < z

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Taylor expanded in z around inf 73.0%

      \[\leadsto \color{blue}{x} \]

    if -1.12000000000000006e-9 < z < -1.4e-127 or -6.8e-165 < z < -5.60000000000000011e-253

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Taylor expanded in y around 0 66.4%

      \[\leadsto \color{blue}{x - \frac{x}{z}} \]
    3. Taylor expanded in z around 0 66.4%

      \[\leadsto \color{blue}{-1 \cdot \frac{x}{z}} \]
    4. Step-by-step derivation
      1. neg-mul-166.4%

        \[\leadsto \color{blue}{-\frac{x}{z}} \]
      2. distribute-frac-neg66.4%

        \[\leadsto \color{blue}{\frac{-x}{z}} \]
    5. Simplified66.4%

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

    if -1.4e-127 < z < -6.8e-165 or -5.60000000000000011e-253 < z < 2.54999999999999986e36

    1. Initial program 99.9%

      \[x + \frac{y - x}{z} \]
    2. Step-by-step derivation
      1. +-commutative99.9%

        \[\leadsto \color{blue}{\frac{y - x}{z} + x} \]
      2. clear-num99.7%

        \[\leadsto \color{blue}{\frac{1}{\frac{z}{y - x}}} + x \]
      3. associate-/r/99.6%

        \[\leadsto \color{blue}{\frac{1}{z} \cdot \left(y - x\right)} + x \]
      4. fma-def99.6%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{z}, y - x, x\right)} \]
    3. Applied egg-rr99.6%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{z}, y - x, x\right)} \]
    4. Taylor expanded in y around inf 64.7%

      \[\leadsto \color{blue}{\frac{y}{z}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification68.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -1.12 \cdot 10^{-9}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq -1.4 \cdot 10^{-127}:\\ \;\;\;\;\frac{-x}{z}\\ \mathbf{elif}\;z \leq -6.8 \cdot 10^{-165}:\\ \;\;\;\;\frac{y}{z}\\ \mathbf{elif}\;z \leq -5.6 \cdot 10^{-253}:\\ \;\;\;\;\frac{-x}{z}\\ \mathbf{elif}\;z \leq 2.55 \cdot 10^{+36}:\\ \;\;\;\;\frac{y}{z}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]

Alternative 3: 76.2% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;z \leq -4.4 \cdot 10^{-97} \lor \neg \left(z \leq -7.6 \cdot 10^{-128} \lor \neg \left(z \leq -4.7 \cdot 10^{-177}\right) \land z \leq -3.2 \cdot 10^{-215}\right):\\
\;\;\;\;x + \frac{y}{z}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -4.3999999999999998e-97 or -7.6000000000000005e-128 < z < -4.69999999999999967e-177 or -3.2000000000000001e-215 < z

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Taylor expanded in y around inf 81.0%

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

    if -4.3999999999999998e-97 < z < -7.6000000000000005e-128 or -4.69999999999999967e-177 < z < -3.2000000000000001e-215

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Taylor expanded in y around 0 87.5%

      \[\leadsto \color{blue}{x - \frac{x}{z}} \]
    3. Taylor expanded in z around 0 87.5%

      \[\leadsto \color{blue}{-1 \cdot \frac{x}{z}} \]
    4. Step-by-step derivation
      1. neg-mul-187.5%

        \[\leadsto \color{blue}{-\frac{x}{z}} \]
      2. distribute-frac-neg87.5%

        \[\leadsto \color{blue}{\frac{-x}{z}} \]
    5. Simplified87.5%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -4.4 \cdot 10^{-97} \lor \neg \left(z \leq -7.6 \cdot 10^{-128} \lor \neg \left(z \leq -4.7 \cdot 10^{-177}\right) \land z \leq -3.2 \cdot 10^{-215}\right):\\ \;\;\;\;x + \frac{y}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{-x}{z}\\ \end{array} \]

Alternative 4: 85.6% accurate, 0.8× speedup?

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -1.02e9 or 7.9999999999999994e159 < x

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Taylor expanded in y around 0 92.2%

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

    if -1.02e9 < x < 7.9999999999999994e159

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Taylor expanded in y around inf 86.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1020000000 \lor \neg \left(x \leq 8 \cdot 10^{+159}\right):\\ \;\;\;\;x - \frac{x}{z}\\ \mathbf{else}:\\ \;\;\;\;x + \frac{y}{z}\\ \end{array} \]

Alternative 5: 99.0% accurate, 0.8× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;z \leq -1 \lor \neg \left(z \leq 1\right):\\
\;\;\;\;x + \frac{y}{z}\\

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


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

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Taylor expanded in y around inf 98.4%

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

    if -1 < z < 1

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Step-by-step derivation
      1. +-commutative100.0%

        \[\leadsto \color{blue}{\frac{y - x}{z} + x} \]
      2. clear-num99.8%

        \[\leadsto \color{blue}{\frac{1}{\frac{z}{y - x}}} + x \]
      3. associate-/r/99.7%

        \[\leadsto \color{blue}{\frac{1}{z} \cdot \left(y - x\right)} + x \]
      4. fma-def99.7%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{z}, y - x, x\right)} \]
    3. Applied egg-rr99.7%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{z}, y - x, x\right)} \]
    4. Taylor expanded in z around 0 99.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -1 \lor \neg \left(z \leq 1\right):\\ \;\;\;\;x + \frac{y}{z}\\ \mathbf{else}:\\ \;\;\;\;\frac{y - x}{z}\\ \end{array} \]

Alternative 6: 61.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq -11:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 7.2 \cdot 10^{+36}:\\ \;\;\;\;\frac{y}{z}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= z -11.0) x (if (<= z 7.2e+36) (/ y z) x)))
double code(double x, double y, double z) {
	double tmp;
	if (z <= -11.0) {
		tmp = x;
	} else if (z <= 7.2e+36) {
		tmp = y / 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 <= (-11.0d0)) then
        tmp = x
    else if (z <= 7.2d+36) then
        tmp = y / z
    else
        tmp = x
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (z <= -11.0) {
		tmp = x;
	} else if (z <= 7.2e+36) {
		tmp = y / z;
	} else {
		tmp = x;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if z <= -11.0:
		tmp = x
	elif z <= 7.2e+36:
		tmp = y / z
	else:
		tmp = x
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (z <= -11.0)
		tmp = x;
	elseif (z <= 7.2e+36)
		tmp = Float64(y / z);
	else
		tmp = x;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (z <= -11.0)
		tmp = x;
	elseif (z <= 7.2e+36)
		tmp = y / z;
	else
		tmp = x;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[z, -11.0], x, If[LessEqual[z, 7.2e+36], N[(y / z), $MachinePrecision], x]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;z \leq -11:\\
\;\;\;\;x\\

\mathbf{elif}\;z \leq 7.2 \cdot 10^{+36}:\\
\;\;\;\;\frac{y}{z}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -11 or 7.1999999999999995e36 < z

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Taylor expanded in z around inf 74.2%

      \[\leadsto \color{blue}{x} \]

    if -11 < z < 7.1999999999999995e36

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Step-by-step derivation
      1. +-commutative100.0%

        \[\leadsto \color{blue}{\frac{y - x}{z} + x} \]
      2. clear-num99.7%

        \[\leadsto \color{blue}{\frac{1}{\frac{z}{y - x}}} + x \]
      3. associate-/r/99.7%

        \[\leadsto \color{blue}{\frac{1}{z} \cdot \left(y - x\right)} + x \]
      4. fma-def99.7%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{z}, y - x, x\right)} \]
    3. Applied egg-rr99.7%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{z}, y - x, x\right)} \]
    4. Taylor expanded in y around inf 55.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -11:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 7.2 \cdot 10^{+36}:\\ \;\;\;\;\frac{y}{z}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]

Alternative 7: 36.1% 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 + \frac{y - x}{z} \]
  2. Taylor expanded in z around inf 35.7%

    \[\leadsto \color{blue}{x} \]
  3. Final simplification35.7%

    \[\leadsto x \]

Reproduce

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