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

Percentage Accurate: 100.0% → 100.0%
Time: 5.5s
Alternatives: 6
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 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?

\[\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. Final simplification100.0%

    \[\leadsto x + \frac{y - x}{z} \]
  4. Add Preprocessing

Alternative 2: 61.1% accurate, 0.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{-x}{z}\\ \mathbf{if}\;z \leq -2.9 \cdot 10^{+58}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq -5.5 \cdot 10^{-78}:\\ \;\;\;\;\frac{y}{z}\\ \mathbf{elif}\;z \leq -3.4 \cdot 10^{-106}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z \leq -1.15 \cdot 10^{-210}:\\ \;\;\;\;\frac{y}{z}\\ \mathbf{elif}\;z \leq 1.75 \cdot 10^{-283}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z \leq 1800000000000 \lor \neg \left(z \leq 3.5 \cdot 10^{+71}\right) \land z \leq 4.5 \cdot 10^{+104}:\\ \;\;\;\;\frac{y}{z}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (/ (- x) z)))
   (if (<= z -2.9e+58)
     x
     (if (<= z -5.5e-78)
       (/ y z)
       (if (<= z -3.4e-106)
         t_0
         (if (<= z -1.15e-210)
           (/ y z)
           (if (<= z 1.75e-283)
             t_0
             (if (or (<= z 1800000000000.0)
                     (and (not (<= z 3.5e+71)) (<= z 4.5e+104)))
               (/ y z)
               x))))))))
double code(double x, double y, double z) {
	double t_0 = -x / z;
	double tmp;
	if (z <= -2.9e+58) {
		tmp = x;
	} else if (z <= -5.5e-78) {
		tmp = y / z;
	} else if (z <= -3.4e-106) {
		tmp = t_0;
	} else if (z <= -1.15e-210) {
		tmp = y / z;
	} else if (z <= 1.75e-283) {
		tmp = t_0;
	} else if ((z <= 1800000000000.0) || (!(z <= 3.5e+71) && (z <= 4.5e+104))) {
		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 <= (-2.9d+58)) then
        tmp = x
    else if (z <= (-5.5d-78)) then
        tmp = y / z
    else if (z <= (-3.4d-106)) then
        tmp = t_0
    else if (z <= (-1.15d-210)) then
        tmp = y / z
    else if (z <= 1.75d-283) then
        tmp = t_0
    else if ((z <= 1800000000000.0d0) .or. (.not. (z <= 3.5d+71)) .and. (z <= 4.5d+104)) 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 <= -2.9e+58) {
		tmp = x;
	} else if (z <= -5.5e-78) {
		tmp = y / z;
	} else if (z <= -3.4e-106) {
		tmp = t_0;
	} else if (z <= -1.15e-210) {
		tmp = y / z;
	} else if (z <= 1.75e-283) {
		tmp = t_0;
	} else if ((z <= 1800000000000.0) || (!(z <= 3.5e+71) && (z <= 4.5e+104))) {
		tmp = y / z;
	} else {
		tmp = x;
	}
	return tmp;
}
def code(x, y, z):
	t_0 = -x / z
	tmp = 0
	if z <= -2.9e+58:
		tmp = x
	elif z <= -5.5e-78:
		tmp = y / z
	elif z <= -3.4e-106:
		tmp = t_0
	elif z <= -1.15e-210:
		tmp = y / z
	elif z <= 1.75e-283:
		tmp = t_0
	elif (z <= 1800000000000.0) or (not (z <= 3.5e+71) and (z <= 4.5e+104)):
		tmp = y / z
	else:
		tmp = x
	return tmp
function code(x, y, z)
	t_0 = Float64(Float64(-x) / z)
	tmp = 0.0
	if (z <= -2.9e+58)
		tmp = x;
	elseif (z <= -5.5e-78)
		tmp = Float64(y / z);
	elseif (z <= -3.4e-106)
		tmp = t_0;
	elseif (z <= -1.15e-210)
		tmp = Float64(y / z);
	elseif (z <= 1.75e-283)
		tmp = t_0;
	elseif ((z <= 1800000000000.0) || (!(z <= 3.5e+71) && (z <= 4.5e+104)))
		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 <= -2.9e+58)
		tmp = x;
	elseif (z <= -5.5e-78)
		tmp = y / z;
	elseif (z <= -3.4e-106)
		tmp = t_0;
	elseif (z <= -1.15e-210)
		tmp = y / z;
	elseif (z <= 1.75e-283)
		tmp = t_0;
	elseif ((z <= 1800000000000.0) || (~((z <= 3.5e+71)) && (z <= 4.5e+104)))
		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, -2.9e+58], x, If[LessEqual[z, -5.5e-78], N[(y / z), $MachinePrecision], If[LessEqual[z, -3.4e-106], t$95$0, If[LessEqual[z, -1.15e-210], N[(y / z), $MachinePrecision], If[LessEqual[z, 1.75e-283], t$95$0, If[Or[LessEqual[z, 1800000000000.0], And[N[Not[LessEqual[z, 3.5e+71]], $MachinePrecision], LessEqual[z, 4.5e+104]]], N[(y / z), $MachinePrecision], x]]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;z \leq -5.5 \cdot 10^{-78}:\\
\;\;\;\;\frac{y}{z}\\

\mathbf{elif}\;z \leq -3.4 \cdot 10^{-106}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;z \leq -1.15 \cdot 10^{-210}:\\
\;\;\;\;\frac{y}{z}\\

\mathbf{elif}\;z \leq 1.75 \cdot 10^{-283}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;z \leq 1800000000000 \lor \neg \left(z \leq 3.5 \cdot 10^{+71}\right) \land z \leq 4.5 \cdot 10^{+104}:\\
\;\;\;\;\frac{y}{z}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if z < -2.90000000000000002e58 or 1.8e12 < z < 3.4999999999999999e71 or 4.4999999999999998e104 < z

    1. Initial program 100.0%

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

        \[\leadsto x + \color{blue}{\left(\frac{y}{z} - \frac{x}{z}\right)} \]
      2. associate-+r-100.0%

        \[\leadsto \color{blue}{\left(x + \frac{y}{z}\right) - \frac{x}{z}} \]
      3. remove-double-neg100.0%

        \[\leadsto \left(x + \color{blue}{\left(-\left(-\frac{y}{z}\right)\right)}\right) - \frac{x}{z} \]
      4. distribute-frac-neg100.0%

        \[\leadsto \left(x + \left(-\color{blue}{\frac{-y}{z}}\right)\right) - \frac{x}{z} \]
      5. unsub-neg100.0%

        \[\leadsto \color{blue}{\left(x - \frac{-y}{z}\right)} - \frac{x}{z} \]
      6. associate--r+100.0%

        \[\leadsto \color{blue}{x - \left(\frac{-y}{z} + \frac{x}{z}\right)} \]
      7. +-commutative100.0%

        \[\leadsto x - \color{blue}{\left(\frac{x}{z} + \frac{-y}{z}\right)} \]
      8. distribute-frac-neg100.0%

        \[\leadsto x - \left(\frac{x}{z} + \color{blue}{\left(-\frac{y}{z}\right)}\right) \]
      9. sub-neg100.0%

        \[\leadsto x - \color{blue}{\left(\frac{x}{z} - \frac{y}{z}\right)} \]
      10. div-sub100.0%

        \[\leadsto x - \color{blue}{\frac{x - y}{z}} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{x - \frac{x - y}{z}} \]
    4. Add Preprocessing
    5. Taylor expanded in x around inf 78.6%

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

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

    if -2.90000000000000002e58 < z < -5.50000000000000017e-78 or -3.39999999999999982e-106 < z < -1.15e-210 or 1.7499999999999999e-283 < z < 1.8e12 or 3.4999999999999999e71 < z < 4.4999999999999998e104

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Step-by-step derivation
      1. div-sub96.8%

        \[\leadsto x + \color{blue}{\left(\frac{y}{z} - \frac{x}{z}\right)} \]
      2. associate-+r-96.8%

        \[\leadsto \color{blue}{\left(x + \frac{y}{z}\right) - \frac{x}{z}} \]
      3. remove-double-neg96.8%

        \[\leadsto \left(x + \color{blue}{\left(-\left(-\frac{y}{z}\right)\right)}\right) - \frac{x}{z} \]
      4. distribute-frac-neg96.8%

        \[\leadsto \left(x + \left(-\color{blue}{\frac{-y}{z}}\right)\right) - \frac{x}{z} \]
      5. unsub-neg96.8%

        \[\leadsto \color{blue}{\left(x - \frac{-y}{z}\right)} - \frac{x}{z} \]
      6. associate--r+96.8%

        \[\leadsto \color{blue}{x - \left(\frac{-y}{z} + \frac{x}{z}\right)} \]
      7. +-commutative96.8%

        \[\leadsto x - \color{blue}{\left(\frac{x}{z} + \frac{-y}{z}\right)} \]
      8. distribute-frac-neg96.8%

        \[\leadsto x - \left(\frac{x}{z} + \color{blue}{\left(-\frac{y}{z}\right)}\right) \]
      9. sub-neg96.8%

        \[\leadsto x - \color{blue}{\left(\frac{x}{z} - \frac{y}{z}\right)} \]
      10. div-sub100.0%

        \[\leadsto x - \color{blue}{\frac{x - y}{z}} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{x - \frac{x - y}{z}} \]
    4. Add Preprocessing
    5. Taylor expanded in x around 0 71.9%

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

        \[\leadsto x - \color{blue}{\left(-\frac{y}{z}\right)} \]
      2. distribute-neg-frac71.9%

        \[\leadsto x - \color{blue}{\frac{-y}{z}} \]
    7. Simplified71.9%

      \[\leadsto x - \color{blue}{\frac{-y}{z}} \]
    8. Taylor expanded in x around 0 67.1%

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

    if -5.50000000000000017e-78 < z < -3.39999999999999982e-106 or -1.15e-210 < z < 1.7499999999999999e-283

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Step-by-step derivation
      1. div-sub96.4%

        \[\leadsto x + \color{blue}{\left(\frac{y}{z} - \frac{x}{z}\right)} \]
      2. associate-+r-96.4%

        \[\leadsto \color{blue}{\left(x + \frac{y}{z}\right) - \frac{x}{z}} \]
      3. remove-double-neg96.4%

        \[\leadsto \left(x + \color{blue}{\left(-\left(-\frac{y}{z}\right)\right)}\right) - \frac{x}{z} \]
      4. distribute-frac-neg96.4%

        \[\leadsto \left(x + \left(-\color{blue}{\frac{-y}{z}}\right)\right) - \frac{x}{z} \]
      5. unsub-neg96.4%

        \[\leadsto \color{blue}{\left(x - \frac{-y}{z}\right)} - \frac{x}{z} \]
      6. associate--r+96.4%

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

        \[\leadsto x - \color{blue}{\left(\frac{x}{z} + \frac{-y}{z}\right)} \]
      8. distribute-frac-neg96.4%

        \[\leadsto x - \left(\frac{x}{z} + \color{blue}{\left(-\frac{y}{z}\right)}\right) \]
      9. sub-neg96.4%

        \[\leadsto x - \color{blue}{\left(\frac{x}{z} - \frac{y}{z}\right)} \]
      10. div-sub100.0%

        \[\leadsto x - \color{blue}{\frac{x - y}{z}} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{x - \frac{x - y}{z}} \]
    4. Add Preprocessing
    5. Taylor expanded in x around inf 74.9%

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

      \[\leadsto \color{blue}{-1 \cdot \frac{x}{z}} \]
    7. Step-by-step derivation
      1. mul-1-neg74.9%

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

        \[\leadsto \color{blue}{\frac{-x}{z}} \]
    8. Simplified74.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -2.9 \cdot 10^{+58}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq -5.5 \cdot 10^{-78}:\\ \;\;\;\;\frac{y}{z}\\ \mathbf{elif}\;z \leq -3.4 \cdot 10^{-106}:\\ \;\;\;\;\frac{-x}{z}\\ \mathbf{elif}\;z \leq -1.15 \cdot 10^{-210}:\\ \;\;\;\;\frac{y}{z}\\ \mathbf{elif}\;z \leq 1.75 \cdot 10^{-283}:\\ \;\;\;\;\frac{-x}{z}\\ \mathbf{elif}\;z \leq 1800000000000 \lor \neg \left(z \leq 3.5 \cdot 10^{+71}\right) \land z \leq 4.5 \cdot 10^{+104}:\\ \;\;\;\;\frac{y}{z}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 61.4% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq -1.8 \cdot 10^{+58}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 31000000000000 \lor \neg \left(z \leq 3.4 \cdot 10^{+71}\right) \land z \leq 1.45 \cdot 10^{+105}:\\ \;\;\;\;\frac{y}{z}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= z -1.8e+58)
   x
   (if (or (<= z 31000000000000.0) (and (not (<= z 3.4e+71)) (<= z 1.45e+105)))
     (/ y z)
     x)))
double code(double x, double y, double z) {
	double tmp;
	if (z <= -1.8e+58) {
		tmp = x;
	} else if ((z <= 31000000000000.0) || (!(z <= 3.4e+71) && (z <= 1.45e+105))) {
		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 <= (-1.8d+58)) then
        tmp = x
    else if ((z <= 31000000000000.0d0) .or. (.not. (z <= 3.4d+71)) .and. (z <= 1.45d+105)) 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 <= -1.8e+58) {
		tmp = x;
	} else if ((z <= 31000000000000.0) || (!(z <= 3.4e+71) && (z <= 1.45e+105))) {
		tmp = y / z;
	} else {
		tmp = x;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if z <= -1.8e+58:
		tmp = x
	elif (z <= 31000000000000.0) or (not (z <= 3.4e+71) and (z <= 1.45e+105)):
		tmp = y / z
	else:
		tmp = x
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (z <= -1.8e+58)
		tmp = x;
	elseif ((z <= 31000000000000.0) || (!(z <= 3.4e+71) && (z <= 1.45e+105)))
		tmp = Float64(y / z);
	else
		tmp = x;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (z <= -1.8e+58)
		tmp = x;
	elseif ((z <= 31000000000000.0) || (~((z <= 3.4e+71)) && (z <= 1.45e+105)))
		tmp = y / z;
	else
		tmp = x;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[z, -1.8e+58], x, If[Or[LessEqual[z, 31000000000000.0], And[N[Not[LessEqual[z, 3.4e+71]], $MachinePrecision], LessEqual[z, 1.45e+105]]], N[(y / z), $MachinePrecision], x]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.8 \cdot 10^{+58}:\\
\;\;\;\;x\\

\mathbf{elif}\;z \leq 31000000000000 \lor \neg \left(z \leq 3.4 \cdot 10^{+71}\right) \land z \leq 1.45 \cdot 10^{+105}:\\
\;\;\;\;\frac{y}{z}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -1.79999999999999998e58 or 3.1e13 < z < 3.3999999999999998e71 or 1.45000000000000005e105 < z

    1. Initial program 100.0%

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

        \[\leadsto x + \color{blue}{\left(\frac{y}{z} - \frac{x}{z}\right)} \]
      2. associate-+r-100.0%

        \[\leadsto \color{blue}{\left(x + \frac{y}{z}\right) - \frac{x}{z}} \]
      3. remove-double-neg100.0%

        \[\leadsto \left(x + \color{blue}{\left(-\left(-\frac{y}{z}\right)\right)}\right) - \frac{x}{z} \]
      4. distribute-frac-neg100.0%

        \[\leadsto \left(x + \left(-\color{blue}{\frac{-y}{z}}\right)\right) - \frac{x}{z} \]
      5. unsub-neg100.0%

        \[\leadsto \color{blue}{\left(x - \frac{-y}{z}\right)} - \frac{x}{z} \]
      6. associate--r+100.0%

        \[\leadsto \color{blue}{x - \left(\frac{-y}{z} + \frac{x}{z}\right)} \]
      7. +-commutative100.0%

        \[\leadsto x - \color{blue}{\left(\frac{x}{z} + \frac{-y}{z}\right)} \]
      8. distribute-frac-neg100.0%

        \[\leadsto x - \left(\frac{x}{z} + \color{blue}{\left(-\frac{y}{z}\right)}\right) \]
      9. sub-neg100.0%

        \[\leadsto x - \color{blue}{\left(\frac{x}{z} - \frac{y}{z}\right)} \]
      10. div-sub100.0%

        \[\leadsto x - \color{blue}{\frac{x - y}{z}} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{x - \frac{x - y}{z}} \]
    4. Add Preprocessing
    5. Taylor expanded in x around inf 78.6%

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

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

    if -1.79999999999999998e58 < z < 3.1e13 or 3.3999999999999998e71 < z < 1.45000000000000005e105

    1. Initial program 100.0%

      \[x + \frac{y - x}{z} \]
    2. Step-by-step derivation
      1. div-sub96.8%

        \[\leadsto x + \color{blue}{\left(\frac{y}{z} - \frac{x}{z}\right)} \]
      2. associate-+r-96.8%

        \[\leadsto \color{blue}{\left(x + \frac{y}{z}\right) - \frac{x}{z}} \]
      3. remove-double-neg96.8%

        \[\leadsto \left(x + \color{blue}{\left(-\left(-\frac{y}{z}\right)\right)}\right) - \frac{x}{z} \]
      4. distribute-frac-neg96.8%

        \[\leadsto \left(x + \left(-\color{blue}{\frac{-y}{z}}\right)\right) - \frac{x}{z} \]
      5. unsub-neg96.8%

        \[\leadsto \color{blue}{\left(x - \frac{-y}{z}\right)} - \frac{x}{z} \]
      6. associate--r+96.8%

        \[\leadsto \color{blue}{x - \left(\frac{-y}{z} + \frac{x}{z}\right)} \]
      7. +-commutative96.8%

        \[\leadsto x - \color{blue}{\left(\frac{x}{z} + \frac{-y}{z}\right)} \]
      8. distribute-frac-neg96.8%

        \[\leadsto x - \left(\frac{x}{z} + \color{blue}{\left(-\frac{y}{z}\right)}\right) \]
      9. sub-neg96.8%

        \[\leadsto x - \color{blue}{\left(\frac{x}{z} - \frac{y}{z}\right)} \]
      10. div-sub100.0%

        \[\leadsto x - \color{blue}{\frac{x - y}{z}} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{x - \frac{x - y}{z}} \]
    4. Add Preprocessing
    5. Taylor expanded in x around 0 65.9%

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

        \[\leadsto x - \color{blue}{\left(-\frac{y}{z}\right)} \]
      2. distribute-neg-frac65.9%

        \[\leadsto x - \color{blue}{\frac{-y}{z}} \]
    7. Simplified65.9%

      \[\leadsto x - \color{blue}{\frac{-y}{z}} \]
    8. Taylor expanded in x around 0 61.8%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -1.8 \cdot 10^{+58}:\\ \;\;\;\;x\\ \mathbf{elif}\;z \leq 31000000000000 \lor \neg \left(z \leq 3.4 \cdot 10^{+71}\right) \land z \leq 1.45 \cdot 10^{+105}:\\ \;\;\;\;\frac{y}{z}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 86.3% accurate, 0.5× speedup?

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

    1. Initial program 100.0%

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

        \[\leadsto x + \color{blue}{\left(\frac{y}{z} - \frac{x}{z}\right)} \]
      2. associate-+r-95.1%

        \[\leadsto \color{blue}{\left(x + \frac{y}{z}\right) - \frac{x}{z}} \]
      3. remove-double-neg95.1%

        \[\leadsto \left(x + \color{blue}{\left(-\left(-\frac{y}{z}\right)\right)}\right) - \frac{x}{z} \]
      4. distribute-frac-neg95.1%

        \[\leadsto \left(x + \left(-\color{blue}{\frac{-y}{z}}\right)\right) - \frac{x}{z} \]
      5. unsub-neg95.1%

        \[\leadsto \color{blue}{\left(x - \frac{-y}{z}\right)} - \frac{x}{z} \]
      6. associate--r+95.1%

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

        \[\leadsto x - \color{blue}{\left(\frac{x}{z} + \frac{-y}{z}\right)} \]
      8. distribute-frac-neg95.1%

        \[\leadsto x - \left(\frac{x}{z} + \color{blue}{\left(-\frac{y}{z}\right)}\right) \]
      9. sub-neg95.1%

        \[\leadsto x - \color{blue}{\left(\frac{x}{z} - \frac{y}{z}\right)} \]
      10. div-sub100.0%

        \[\leadsto x - \color{blue}{\frac{x - y}{z}} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{x - \frac{x - y}{z}} \]
    4. Add Preprocessing
    5. Taylor expanded in x around inf 88.4%

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

    if -1.39999999999999992e107 < x < 5.4e25

    1. Initial program 100.0%

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

        \[\leadsto x + \color{blue}{\left(\frac{y}{z} - \frac{x}{z}\right)} \]
      2. associate-+r-100.0%

        \[\leadsto \color{blue}{\left(x + \frac{y}{z}\right) - \frac{x}{z}} \]
      3. remove-double-neg100.0%

        \[\leadsto \left(x + \color{blue}{\left(-\left(-\frac{y}{z}\right)\right)}\right) - \frac{x}{z} \]
      4. distribute-frac-neg100.0%

        \[\leadsto \left(x + \left(-\color{blue}{\frac{-y}{z}}\right)\right) - \frac{x}{z} \]
      5. unsub-neg100.0%

        \[\leadsto \color{blue}{\left(x - \frac{-y}{z}\right)} - \frac{x}{z} \]
      6. associate--r+100.0%

        \[\leadsto \color{blue}{x - \left(\frac{-y}{z} + \frac{x}{z}\right)} \]
      7. +-commutative100.0%

        \[\leadsto x - \color{blue}{\left(\frac{x}{z} + \frac{-y}{z}\right)} \]
      8. distribute-frac-neg100.0%

        \[\leadsto x - \left(\frac{x}{z} + \color{blue}{\left(-\frac{y}{z}\right)}\right) \]
      9. sub-neg100.0%

        \[\leadsto x - \color{blue}{\left(\frac{x}{z} - \frac{y}{z}\right)} \]
      10. div-sub100.0%

        \[\leadsto x - \color{blue}{\frac{x - y}{z}} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{x - \frac{x - y}{z}} \]
    4. Add Preprocessing
    5. Taylor expanded in x around 0 90.1%

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

        \[\leadsto x - \color{blue}{\left(-\frac{y}{z}\right)} \]
      2. distribute-neg-frac90.1%

        \[\leadsto x - \color{blue}{\frac{-y}{z}} \]
    7. Simplified90.1%

      \[\leadsto x - \color{blue}{\frac{-y}{z}} \]
    8. Taylor expanded in x around 0 90.1%

      \[\leadsto \color{blue}{x + \frac{y}{z}} \]
    9. Step-by-step derivation
      1. +-commutative90.1%

        \[\leadsto \color{blue}{\frac{y}{z} + x} \]
    10. Simplified90.1%

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

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

Alternative 5: 76.8% accurate, 1.4× speedup?

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

\\
x + \frac{y}{z}
\end{array}
Derivation
  1. Initial program 100.0%

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

      \[\leadsto x + \color{blue}{\left(\frac{y}{z} - \frac{x}{z}\right)} \]
    2. associate-+r-98.0%

      \[\leadsto \color{blue}{\left(x + \frac{y}{z}\right) - \frac{x}{z}} \]
    3. remove-double-neg98.0%

      \[\leadsto \left(x + \color{blue}{\left(-\left(-\frac{y}{z}\right)\right)}\right) - \frac{x}{z} \]
    4. distribute-frac-neg98.0%

      \[\leadsto \left(x + \left(-\color{blue}{\frac{-y}{z}}\right)\right) - \frac{x}{z} \]
    5. unsub-neg98.0%

      \[\leadsto \color{blue}{\left(x - \frac{-y}{z}\right)} - \frac{x}{z} \]
    6. associate--r+98.0%

      \[\leadsto \color{blue}{x - \left(\frac{-y}{z} + \frac{x}{z}\right)} \]
    7. +-commutative98.0%

      \[\leadsto x - \color{blue}{\left(\frac{x}{z} + \frac{-y}{z}\right)} \]
    8. distribute-frac-neg98.0%

      \[\leadsto x - \left(\frac{x}{z} + \color{blue}{\left(-\frac{y}{z}\right)}\right) \]
    9. sub-neg98.0%

      \[\leadsto x - \color{blue}{\left(\frac{x}{z} - \frac{y}{z}\right)} \]
    10. div-sub100.0%

      \[\leadsto x - \color{blue}{\frac{x - y}{z}} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{x - \frac{x - y}{z}} \]
  4. Add Preprocessing
  5. Taylor expanded in x around 0 79.2%

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

      \[\leadsto x - \color{blue}{\left(-\frac{y}{z}\right)} \]
    2. distribute-neg-frac79.2%

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

    \[\leadsto x - \color{blue}{\frac{-y}{z}} \]
  8. Taylor expanded in x around 0 79.2%

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

      \[\leadsto \color{blue}{\frac{y}{z} + x} \]
  10. Simplified79.2%

    \[\leadsto \color{blue}{\frac{y}{z} + x} \]
  11. Final simplification79.2%

    \[\leadsto x + \frac{y}{z} \]
  12. Add Preprocessing

Alternative 6: 36.3% 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. Step-by-step derivation
    1. div-sub98.0%

      \[\leadsto x + \color{blue}{\left(\frac{y}{z} - \frac{x}{z}\right)} \]
    2. associate-+r-98.0%

      \[\leadsto \color{blue}{\left(x + \frac{y}{z}\right) - \frac{x}{z}} \]
    3. remove-double-neg98.0%

      \[\leadsto \left(x + \color{blue}{\left(-\left(-\frac{y}{z}\right)\right)}\right) - \frac{x}{z} \]
    4. distribute-frac-neg98.0%

      \[\leadsto \left(x + \left(-\color{blue}{\frac{-y}{z}}\right)\right) - \frac{x}{z} \]
    5. unsub-neg98.0%

      \[\leadsto \color{blue}{\left(x - \frac{-y}{z}\right)} - \frac{x}{z} \]
    6. associate--r+98.0%

      \[\leadsto \color{blue}{x - \left(\frac{-y}{z} + \frac{x}{z}\right)} \]
    7. +-commutative98.0%

      \[\leadsto x - \color{blue}{\left(\frac{x}{z} + \frac{-y}{z}\right)} \]
    8. distribute-frac-neg98.0%

      \[\leadsto x - \left(\frac{x}{z} + \color{blue}{\left(-\frac{y}{z}\right)}\right) \]
    9. sub-neg98.0%

      \[\leadsto x - \color{blue}{\left(\frac{x}{z} - \frac{y}{z}\right)} \]
    10. div-sub100.0%

      \[\leadsto x - \color{blue}{\frac{x - y}{z}} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{x - \frac{x - y}{z}} \]
  4. Add Preprocessing
  5. Taylor expanded in x around inf 56.9%

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

    \[\leadsto \color{blue}{x} \]
  7. Final simplification34.4%

    \[\leadsto x \]
  8. Add Preprocessing

Reproduce

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