Numeric.SpecFunctions:stirlingError from math-functions-0.1.5.2

Percentage Accurate: 99.8% → 99.9%
Time: 10.5s
Alternatives: 10
Speedup: 1.0×

Specification

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

\\
\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\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 10 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: 99.8% accurate, 1.0× speedup?

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

\\
\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z
\end{array}

Alternative 1: 99.9% accurate, 0.5× speedup?

\[\begin{array}{l} \\ x + \left(\mathsf{fma}\left(\log y, -0.5 - y, y\right) - z\right) \end{array} \]
(FPCore (x y z) :precision binary64 (+ x (- (fma (log y) (- -0.5 y) y) z)))
double code(double x, double y, double z) {
	return x + (fma(log(y), (-0.5 - y), y) - z);
}
function code(x, y, z)
	return Float64(x + Float64(fma(log(y), Float64(-0.5 - y), y) - z))
end
code[x_, y_, z_] := N[(x + N[(N[(N[Log[y], $MachinePrecision] * N[(-0.5 - y), $MachinePrecision] + y), $MachinePrecision] - z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x + \left(\mathsf{fma}\left(\log y, -0.5 - y, y\right) - z\right)
\end{array}
Derivation
  1. Initial program 99.8%

    \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
  2. Step-by-step derivation
    1. sub-neg99.8%

      \[\leadsto \color{blue}{\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) + \left(-z\right)} \]
    2. sub-neg99.8%

      \[\leadsto \left(\color{blue}{\left(x + \left(-\left(y + 0.5\right) \cdot \log y\right)\right)} + y\right) + \left(-z\right) \]
    3. associate-+l+99.8%

      \[\leadsto \color{blue}{\left(x + \left(\left(-\left(y + 0.5\right) \cdot \log y\right) + y\right)\right)} + \left(-z\right) \]
    4. associate-+l+99.8%

      \[\leadsto \color{blue}{x + \left(\left(\left(-\left(y + 0.5\right) \cdot \log y\right) + y\right) + \left(-z\right)\right)} \]
    5. sub-neg99.8%

      \[\leadsto x + \color{blue}{\left(\left(\left(-\left(y + 0.5\right) \cdot \log y\right) + y\right) - z\right)} \]
    6. neg-sub099.8%

      \[\leadsto x + \left(\left(\color{blue}{\left(0 - \left(y + 0.5\right) \cdot \log y\right)} + y\right) - z\right) \]
    7. associate-+l-99.8%

      \[\leadsto x + \left(\color{blue}{\left(0 - \left(\left(y + 0.5\right) \cdot \log y - y\right)\right)} - z\right) \]
    8. neg-sub099.8%

      \[\leadsto x + \left(\color{blue}{\left(-\left(\left(y + 0.5\right) \cdot \log y - y\right)\right)} - z\right) \]
    9. neg-mul-199.8%

      \[\leadsto x + \left(\color{blue}{-1 \cdot \left(\left(y + 0.5\right) \cdot \log y - y\right)} - z\right) \]
  3. Simplified99.9%

    \[\leadsto \color{blue}{x + \left(\mathsf{fma}\left(\log y, -0.5 - y, y\right) - z\right)} \]
  4. Final simplification99.9%

    \[\leadsto x + \left(\mathsf{fma}\left(\log y, -0.5 - y, y\right) - z\right) \]

Alternative 2: 72.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq 9.8 \cdot 10^{-235}:\\ \;\;\;\;x - \log y \cdot 0.5\\ \mathbf{elif}\;y \leq 1.8 \cdot 10^{+137}:\\ \;\;\;\;x - z\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(1 - \log y\right) - z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= y 9.8e-235)
   (- x (* (log y) 0.5))
   (if (<= y 1.8e+137) (- x z) (- (* y (- 1.0 (log y))) z))))
double code(double x, double y, double z) {
	double tmp;
	if (y <= 9.8e-235) {
		tmp = x - (log(y) * 0.5);
	} else if (y <= 1.8e+137) {
		tmp = x - z;
	} else {
		tmp = (y * (1.0 - log(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 (y <= 9.8d-235) then
        tmp = x - (log(y) * 0.5d0)
    else if (y <= 1.8d+137) then
        tmp = x - z
    else
        tmp = (y * (1.0d0 - log(y))) - z
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (y <= 9.8e-235) {
		tmp = x - (Math.log(y) * 0.5);
	} else if (y <= 1.8e+137) {
		tmp = x - z;
	} else {
		tmp = (y * (1.0 - Math.log(y))) - z;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if y <= 9.8e-235:
		tmp = x - (math.log(y) * 0.5)
	elif y <= 1.8e+137:
		tmp = x - z
	else:
		tmp = (y * (1.0 - math.log(y))) - z
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (y <= 9.8e-235)
		tmp = Float64(x - Float64(log(y) * 0.5));
	elseif (y <= 1.8e+137)
		tmp = Float64(x - z);
	else
		tmp = Float64(Float64(y * Float64(1.0 - log(y))) - z);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y <= 9.8e-235)
		tmp = x - (log(y) * 0.5);
	elseif (y <= 1.8e+137)
		tmp = x - z;
	else
		tmp = (y * (1.0 - log(y))) - z;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[y, 9.8e-235], N[(x - N[(N[Log[y], $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 1.8e+137], N[(x - z), $MachinePrecision], N[(N[(y * N[(1.0 - N[Log[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq 9.8 \cdot 10^{-235}:\\
\;\;\;\;x - \log y \cdot 0.5\\

\mathbf{elif}\;y \leq 1.8 \cdot 10^{+137}:\\
\;\;\;\;x - z\\

\mathbf{else}:\\
\;\;\;\;y \cdot \left(1 - \log y\right) - z\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if y < 9.79999999999999931e-235

    1. Initial program 99.9%

      \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
    2. Taylor expanded in y around 0 99.9%

      \[\leadsto \color{blue}{\left(x - 0.5 \cdot \log y\right)} - z \]
    3. Taylor expanded in z around 0 83.5%

      \[\leadsto \color{blue}{x - 0.5 \cdot \log y} \]

    if 9.79999999999999931e-235 < y < 1.8e137

    1. Initial program 99.9%

      \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
    2. Taylor expanded in x around inf 80.7%

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

    if 1.8e137 < y

    1. Initial program 99.6%

      \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
    2. Taylor expanded in y around inf 82.9%

      \[\leadsto \left(\color{blue}{y \cdot \log \left(\frac{1}{y}\right)} + y\right) - z \]
    3. Step-by-step derivation
      1. *-commutative82.9%

        \[\leadsto \left(\color{blue}{\log \left(\frac{1}{y}\right) \cdot y} + y\right) - z \]
      2. log-rec82.9%

        \[\leadsto \left(\color{blue}{\left(-\log y\right)} \cdot y + y\right) - z \]
      3. distribute-lft-neg-in82.9%

        \[\leadsto \left(\color{blue}{\left(-\log y \cdot y\right)} + y\right) - z \]
      4. distribute-rgt-neg-in82.9%

        \[\leadsto \left(\color{blue}{\log y \cdot \left(-y\right)} + y\right) - z \]
    4. Simplified82.9%

      \[\leadsto \left(\color{blue}{\log y \cdot \left(-y\right)} + y\right) - z \]
    5. Taylor expanded in y around 0 83.1%

      \[\leadsto \color{blue}{y \cdot \left(1 + -1 \cdot \log y\right)} - z \]
    6. Step-by-step derivation
      1. neg-mul-183.1%

        \[\leadsto y \cdot \left(1 + \color{blue}{\left(-\log y\right)}\right) - z \]
      2. log-rec83.1%

        \[\leadsto y \cdot \left(1 + \color{blue}{\log \left(\frac{1}{y}\right)}\right) - z \]
      3. log-rec83.1%

        \[\leadsto y \cdot \left(1 + \color{blue}{\left(-\log y\right)}\right) - z \]
      4. sub-neg83.1%

        \[\leadsto y \cdot \color{blue}{\left(1 - \log y\right)} - z \]
    7. Simplified83.1%

      \[\leadsto \color{blue}{y \cdot \left(1 - \log y\right)} - z \]
  3. Recombined 3 regimes into one program.
  4. Final simplification81.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 9.8 \cdot 10^{-235}:\\ \;\;\;\;x - \log y \cdot 0.5\\ \mathbf{elif}\;y \leq 1.8 \cdot 10^{+137}:\\ \;\;\;\;x - z\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(1 - \log y\right) - z\\ \end{array} \]

Alternative 3: 99.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq 3.6 \cdot 10^{-9}:\\ \;\;\;\;\left(x - \log y \cdot 0.5\right) - z\\ \mathbf{else}:\\ \;\;\;\;x + \left(y \cdot \left(1 - \log y\right) - z\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= y 3.6e-9)
   (- (- x (* (log y) 0.5)) z)
   (+ x (- (* y (- 1.0 (log y))) z))))
double code(double x, double y, double z) {
	double tmp;
	if (y <= 3.6e-9) {
		tmp = (x - (log(y) * 0.5)) - z;
	} else {
		tmp = x + ((y * (1.0 - log(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 (y <= 3.6d-9) then
        tmp = (x - (log(y) * 0.5d0)) - z
    else
        tmp = x + ((y * (1.0d0 - log(y))) - z)
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (y <= 3.6e-9) {
		tmp = (x - (Math.log(y) * 0.5)) - z;
	} else {
		tmp = x + ((y * (1.0 - Math.log(y))) - z);
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if y <= 3.6e-9:
		tmp = (x - (math.log(y) * 0.5)) - z
	else:
		tmp = x + ((y * (1.0 - math.log(y))) - z)
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (y <= 3.6e-9)
		tmp = Float64(Float64(x - Float64(log(y) * 0.5)) - z);
	else
		tmp = Float64(x + Float64(Float64(y * Float64(1.0 - log(y))) - z));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y <= 3.6e-9)
		tmp = (x - (log(y) * 0.5)) - z;
	else
		tmp = x + ((y * (1.0 - log(y))) - z);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[y, 3.6e-9], N[(N[(x - N[(N[Log[y], $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision], N[(x + N[(N[(y * N[(1.0 - N[Log[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq 3.6 \cdot 10^{-9}:\\
\;\;\;\;\left(x - \log y \cdot 0.5\right) - z\\

\mathbf{else}:\\
\;\;\;\;x + \left(y \cdot \left(1 - \log y\right) - z\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 3.6e-9

    1. Initial program 100.0%

      \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
    2. Taylor expanded in y around 0 99.7%

      \[\leadsto \color{blue}{\left(x - 0.5 \cdot \log y\right)} - z \]

    if 3.6e-9 < y

    1. Initial program 99.7%

      \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
    2. Step-by-step derivation
      1. sub-neg99.7%

        \[\leadsto \color{blue}{\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) + \left(-z\right)} \]
      2. sub-neg99.7%

        \[\leadsto \left(\color{blue}{\left(x + \left(-\left(y + 0.5\right) \cdot \log y\right)\right)} + y\right) + \left(-z\right) \]
      3. associate-+l+99.7%

        \[\leadsto \color{blue}{\left(x + \left(\left(-\left(y + 0.5\right) \cdot \log y\right) + y\right)\right)} + \left(-z\right) \]
      4. associate-+l+99.7%

        \[\leadsto \color{blue}{x + \left(\left(\left(-\left(y + 0.5\right) \cdot \log y\right) + y\right) + \left(-z\right)\right)} \]
      5. sub-neg99.7%

        \[\leadsto x + \color{blue}{\left(\left(\left(-\left(y + 0.5\right) \cdot \log y\right) + y\right) - z\right)} \]
      6. neg-sub099.7%

        \[\leadsto x + \left(\left(\color{blue}{\left(0 - \left(y + 0.5\right) \cdot \log y\right)} + y\right) - z\right) \]
      7. associate-+l-99.7%

        \[\leadsto x + \left(\color{blue}{\left(0 - \left(\left(y + 0.5\right) \cdot \log y - y\right)\right)} - z\right) \]
      8. neg-sub099.7%

        \[\leadsto x + \left(\color{blue}{\left(-\left(\left(y + 0.5\right) \cdot \log y - y\right)\right)} - z\right) \]
      9. neg-mul-199.7%

        \[\leadsto x + \left(\color{blue}{-1 \cdot \left(\left(y + 0.5\right) \cdot \log y - y\right)} - z\right) \]
    3. Simplified99.9%

      \[\leadsto \color{blue}{x + \left(\mathsf{fma}\left(\log y, -0.5 - y, y\right) - z\right)} \]
    4. Taylor expanded in y around inf 99.9%

      \[\leadsto x + \left(\color{blue}{y \cdot \left(1 + \log \left(\frac{1}{y}\right)\right)} - z\right) \]
    5. Step-by-step derivation
      1. log-rec99.9%

        \[\leadsto x + \left(y \cdot \left(1 + \color{blue}{\left(-\log y\right)}\right) - z\right) \]
      2. sub-neg99.9%

        \[\leadsto x + \left(y \cdot \color{blue}{\left(1 - \log y\right)} - z\right) \]
    6. Simplified99.9%

      \[\leadsto x + \left(\color{blue}{y \cdot \left(1 - \log y\right)} - z\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 3.6 \cdot 10^{-9}:\\ \;\;\;\;\left(x - \log y \cdot 0.5\right) - z\\ \mathbf{else}:\\ \;\;\;\;x + \left(y \cdot \left(1 - \log y\right) - z\right)\\ \end{array} \]

Alternative 4: 99.8% accurate, 1.0× speedup?

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

\\
\left(y + \left(x - \log y \cdot \left(y + 0.5\right)\right)\right) - z
\end{array}
Derivation
  1. Initial program 99.8%

    \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
  2. Final simplification99.8%

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

Alternative 5: 69.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq -19000:\\ \;\;\;\;x - z\\ \mathbf{elif}\;z \leq 260:\\ \;\;\;\;x - \log y \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;x - z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= z -19000.0) (- x z) (if (<= z 260.0) (- x (* (log y) 0.5)) (- x z))))
double code(double x, double y, double z) {
	double tmp;
	if (z <= -19000.0) {
		tmp = x - z;
	} else if (z <= 260.0) {
		tmp = x - (log(y) * 0.5);
	} 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 <= (-19000.0d0)) then
        tmp = x - z
    else if (z <= 260.0d0) then
        tmp = x - (log(y) * 0.5d0)
    else
        tmp = x - z
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (z <= -19000.0) {
		tmp = x - z;
	} else if (z <= 260.0) {
		tmp = x - (Math.log(y) * 0.5);
	} else {
		tmp = x - z;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if z <= -19000.0:
		tmp = x - z
	elif z <= 260.0:
		tmp = x - (math.log(y) * 0.5)
	else:
		tmp = x - z
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (z <= -19000.0)
		tmp = Float64(x - z);
	elseif (z <= 260.0)
		tmp = Float64(x - Float64(log(y) * 0.5));
	else
		tmp = Float64(x - z);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (z <= -19000.0)
		tmp = x - z;
	elseif (z <= 260.0)
		tmp = x - (log(y) * 0.5);
	else
		tmp = x - z;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[z, -19000.0], N[(x - z), $MachinePrecision], If[LessEqual[z, 260.0], N[(x - N[(N[Log[y], $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision], N[(x - z), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;z \leq 260:\\
\;\;\;\;x - \log y \cdot 0.5\\

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


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

    1. Initial program 99.9%

      \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
    2. Taylor expanded in x around inf 78.1%

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

    if -19000 < z < 260

    1. Initial program 99.8%

      \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
    2. Taylor expanded in y around 0 72.3%

      \[\leadsto \color{blue}{\left(x - 0.5 \cdot \log y\right)} - z \]
    3. Taylor expanded in z around 0 71.6%

      \[\leadsto \color{blue}{x - 0.5 \cdot \log y} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification74.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -19000:\\ \;\;\;\;x - z\\ \mathbf{elif}\;z \leq 260:\\ \;\;\;\;x - \log y \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;x - z\\ \end{array} \]

Alternative 6: 69.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq 7.6 \cdot 10^{-235}:\\ \;\;\;\;x - \log y \cdot 0.5\\ \mathbf{elif}\;y \leq 5.4 \cdot 10^{+138}:\\ \;\;\;\;x - z\\ \mathbf{else}:\\ \;\;\;\;y - y \cdot \log y\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= y 7.6e-235)
   (- x (* (log y) 0.5))
   (if (<= y 5.4e+138) (- x z) (- y (* y (log y))))))
double code(double x, double y, double z) {
	double tmp;
	if (y <= 7.6e-235) {
		tmp = x - (log(y) * 0.5);
	} else if (y <= 5.4e+138) {
		tmp = x - z;
	} else {
		tmp = y - (y * log(y));
	}
	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 <= 7.6d-235) then
        tmp = x - (log(y) * 0.5d0)
    else if (y <= 5.4d+138) then
        tmp = x - z
    else
        tmp = y - (y * log(y))
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (y <= 7.6e-235) {
		tmp = x - (Math.log(y) * 0.5);
	} else if (y <= 5.4e+138) {
		tmp = x - z;
	} else {
		tmp = y - (y * Math.log(y));
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if y <= 7.6e-235:
		tmp = x - (math.log(y) * 0.5)
	elif y <= 5.4e+138:
		tmp = x - z
	else:
		tmp = y - (y * math.log(y))
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (y <= 7.6e-235)
		tmp = Float64(x - Float64(log(y) * 0.5));
	elseif (y <= 5.4e+138)
		tmp = Float64(x - z);
	else
		tmp = Float64(y - Float64(y * log(y)));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y <= 7.6e-235)
		tmp = x - (log(y) * 0.5);
	elseif (y <= 5.4e+138)
		tmp = x - z;
	else
		tmp = y - (y * log(y));
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[y, 7.6e-235], N[(x - N[(N[Log[y], $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 5.4e+138], N[(x - z), $MachinePrecision], N[(y - N[(y * N[Log[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq 7.6 \cdot 10^{-235}:\\
\;\;\;\;x - \log y \cdot 0.5\\

\mathbf{elif}\;y \leq 5.4 \cdot 10^{+138}:\\
\;\;\;\;x - z\\

\mathbf{else}:\\
\;\;\;\;y - y \cdot \log y\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if y < 7.60000000000000052e-235

    1. Initial program 99.9%

      \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
    2. Taylor expanded in y around 0 99.9%

      \[\leadsto \color{blue}{\left(x - 0.5 \cdot \log y\right)} - z \]
    3. Taylor expanded in z around 0 83.5%

      \[\leadsto \color{blue}{x - 0.5 \cdot \log y} \]

    if 7.60000000000000052e-235 < y < 5.40000000000000018e138

    1. Initial program 99.9%

      \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
    2. Taylor expanded in x around inf 80.8%

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

    if 5.40000000000000018e138 < y

    1. Initial program 99.6%

      \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
    2. Taylor expanded in y around inf 82.7%

      \[\leadsto \left(\color{blue}{y \cdot \log \left(\frac{1}{y}\right)} + y\right) - z \]
    3. Step-by-step derivation
      1. *-commutative82.7%

        \[\leadsto \left(\color{blue}{\log \left(\frac{1}{y}\right) \cdot y} + y\right) - z \]
      2. log-rec82.7%

        \[\leadsto \left(\color{blue}{\left(-\log y\right)} \cdot y + y\right) - z \]
      3. distribute-lft-neg-in82.7%

        \[\leadsto \left(\color{blue}{\left(-\log y \cdot y\right)} + y\right) - z \]
      4. distribute-rgt-neg-in82.7%

        \[\leadsto \left(\color{blue}{\log y \cdot \left(-y\right)} + y\right) - z \]
    4. Simplified82.7%

      \[\leadsto \left(\color{blue}{\log y \cdot \left(-y\right)} + y\right) - z \]
    5. Taylor expanded in z around 0 69.8%

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot \log y\right) + y} \]
    6. Taylor expanded in y around 0 70.0%

      \[\leadsto \color{blue}{y \cdot \left(1 + -1 \cdot \log y\right)} \]
    7. Step-by-step derivation
      1. neg-mul-170.0%

        \[\leadsto y \cdot \left(1 + \color{blue}{\left(-\log y\right)}\right) \]
      2. unsub-neg70.0%

        \[\leadsto y \cdot \color{blue}{\left(1 - \log y\right)} \]
      3. distribute-lft-out--69.8%

        \[\leadsto \color{blue}{y \cdot 1 - y \cdot \log y} \]
      4. *-rgt-identity69.8%

        \[\leadsto \color{blue}{y} - y \cdot \log y \]
    8. Simplified69.8%

      \[\leadsto \color{blue}{y - y \cdot \log y} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification78.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 7.6 \cdot 10^{-235}:\\ \;\;\;\;x - \log y \cdot 0.5\\ \mathbf{elif}\;y \leq 5.4 \cdot 10^{+138}:\\ \;\;\;\;x - z\\ \mathbf{else}:\\ \;\;\;\;y - y \cdot \log y\\ \end{array} \]

Alternative 7: 87.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq 4.8 \cdot 10^{+136}:\\ \;\;\;\;\left(x - \log y \cdot 0.5\right) - z\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(1 - \log y\right) - z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= y 4.8e+136) (- (- x (* (log y) 0.5)) z) (- (* y (- 1.0 (log y))) z)))
double code(double x, double y, double z) {
	double tmp;
	if (y <= 4.8e+136) {
		tmp = (x - (log(y) * 0.5)) - z;
	} else {
		tmp = (y * (1.0 - log(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 (y <= 4.8d+136) then
        tmp = (x - (log(y) * 0.5d0)) - z
    else
        tmp = (y * (1.0d0 - log(y))) - z
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (y <= 4.8e+136) {
		tmp = (x - (Math.log(y) * 0.5)) - z;
	} else {
		tmp = (y * (1.0 - Math.log(y))) - z;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if y <= 4.8e+136:
		tmp = (x - (math.log(y) * 0.5)) - z
	else:
		tmp = (y * (1.0 - math.log(y))) - z
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (y <= 4.8e+136)
		tmp = Float64(Float64(x - Float64(log(y) * 0.5)) - z);
	else
		tmp = Float64(Float64(y * Float64(1.0 - log(y))) - z);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y <= 4.8e+136)
		tmp = (x - (log(y) * 0.5)) - z;
	else
		tmp = (y * (1.0 - log(y))) - z;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[y, 4.8e+136], N[(N[(x - N[(N[Log[y], $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision], N[(N[(y * N[(1.0 - N[Log[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq 4.8 \cdot 10^{+136}:\\
\;\;\;\;\left(x - \log y \cdot 0.5\right) - z\\

\mathbf{else}:\\
\;\;\;\;y \cdot \left(1 - \log y\right) - z\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 4.8000000000000001e136

    1. Initial program 99.9%

      \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
    2. Taylor expanded in y around 0 93.8%

      \[\leadsto \color{blue}{\left(x - 0.5 \cdot \log y\right)} - z \]

    if 4.8000000000000001e136 < y

    1. Initial program 99.6%

      \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
    2. Taylor expanded in y around inf 82.9%

      \[\leadsto \left(\color{blue}{y \cdot \log \left(\frac{1}{y}\right)} + y\right) - z \]
    3. Step-by-step derivation
      1. *-commutative82.9%

        \[\leadsto \left(\color{blue}{\log \left(\frac{1}{y}\right) \cdot y} + y\right) - z \]
      2. log-rec82.9%

        \[\leadsto \left(\color{blue}{\left(-\log y\right)} \cdot y + y\right) - z \]
      3. distribute-lft-neg-in82.9%

        \[\leadsto \left(\color{blue}{\left(-\log y \cdot y\right)} + y\right) - z \]
      4. distribute-rgt-neg-in82.9%

        \[\leadsto \left(\color{blue}{\log y \cdot \left(-y\right)} + y\right) - z \]
    4. Simplified82.9%

      \[\leadsto \left(\color{blue}{\log y \cdot \left(-y\right)} + y\right) - z \]
    5. Taylor expanded in y around 0 83.1%

      \[\leadsto \color{blue}{y \cdot \left(1 + -1 \cdot \log y\right)} - z \]
    6. Step-by-step derivation
      1. neg-mul-183.1%

        \[\leadsto y \cdot \left(1 + \color{blue}{\left(-\log y\right)}\right) - z \]
      2. log-rec83.1%

        \[\leadsto y \cdot \left(1 + \color{blue}{\log \left(\frac{1}{y}\right)}\right) - z \]
      3. log-rec83.1%

        \[\leadsto y \cdot \left(1 + \color{blue}{\left(-\log y\right)}\right) - z \]
      4. sub-neg83.1%

        \[\leadsto y \cdot \color{blue}{\left(1 - \log y\right)} - z \]
    7. Simplified83.1%

      \[\leadsto \color{blue}{y \cdot \left(1 - \log y\right)} - z \]
  3. Recombined 2 regimes into one program.
  4. Final simplification90.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq 4.8 \cdot 10^{+136}:\\ \;\;\;\;\left(x - \log y \cdot 0.5\right) - z\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(1 - \log y\right) - z\\ \end{array} \]

Alternative 8: 47.0% accurate, 18.1× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;x \leq -6 \cdot 10^{+65}:\\
\;\;\;\;x\\

\mathbf{elif}\;x \leq 2100000000:\\
\;\;\;\;-z\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -6.0000000000000004e65 or 2.1e9 < x

    1. Initial program 99.9%

      \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
    2. Taylor expanded in y around 0 87.3%

      \[\leadsto \color{blue}{\left(x - 0.5 \cdot \log y\right)} - z \]
    3. Taylor expanded in x around inf 73.0%

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

    if -6.0000000000000004e65 < x < 2.1e9

    1. Initial program 99.8%

      \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
    2. Taylor expanded in y around inf 74.8%

      \[\leadsto \left(\color{blue}{y \cdot \log \left(\frac{1}{y}\right)} + y\right) - z \]
    3. Step-by-step derivation
      1. *-commutative74.8%

        \[\leadsto \left(\color{blue}{\log \left(\frac{1}{y}\right) \cdot y} + y\right) - z \]
      2. log-rec74.8%

        \[\leadsto \left(\color{blue}{\left(-\log y\right)} \cdot y + y\right) - z \]
      3. distribute-lft-neg-in74.8%

        \[\leadsto \left(\color{blue}{\left(-\log y \cdot y\right)} + y\right) - z \]
      4. distribute-rgt-neg-in74.8%

        \[\leadsto \left(\color{blue}{\log y \cdot \left(-y\right)} + y\right) - z \]
    4. Simplified74.8%

      \[\leadsto \left(\color{blue}{\log y \cdot \left(-y\right)} + y\right) - z \]
    5. Taylor expanded in y around 0 40.4%

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

        \[\leadsto \color{blue}{-z} \]
    7. Simplified40.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -6 \cdot 10^{+65}:\\ \;\;\;\;x\\ \mathbf{elif}\;x \leq 2100000000:\\ \;\;\;\;-z\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]

Alternative 9: 57.3% accurate, 37.0× speedup?

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

\\
x - z
\end{array}
Derivation
  1. Initial program 99.8%

    \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
  2. Taylor expanded in x around inf 62.3%

    \[\leadsto \color{blue}{x} - z \]
  3. Final simplification62.3%

    \[\leadsto x - z \]

Alternative 10: 28.9% accurate, 111.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 99.8%

    \[\left(\left(x - \left(y + 0.5\right) \cdot \log y\right) + y\right) - z \]
  2. Taylor expanded in y around 0 75.6%

    \[\leadsto \color{blue}{\left(x - 0.5 \cdot \log y\right)} - z \]
  3. Taylor expanded in x around inf 35.2%

    \[\leadsto \color{blue}{x} \]
  4. Final simplification35.2%

    \[\leadsto x \]

Developer target: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\left(y + x\right) - z\right) - \left(y + 0.5\right) \cdot \log y \end{array} \]
(FPCore (x y z) :precision binary64 (- (- (+ y x) z) (* (+ y 0.5) (log y))))
double code(double x, double y, double z) {
	return ((y + x) - z) - ((y + 0.5) * log(y));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = ((y + x) - z) - ((y + 0.5d0) * log(y))
end function
public static double code(double x, double y, double z) {
	return ((y + x) - z) - ((y + 0.5) * Math.log(y));
}
def code(x, y, z):
	return ((y + x) - z) - ((y + 0.5) * math.log(y))
function code(x, y, z)
	return Float64(Float64(Float64(y + x) - z) - Float64(Float64(y + 0.5) * log(y)))
end
function tmp = code(x, y, z)
	tmp = ((y + x) - z) - ((y + 0.5) * log(y));
end
code[x_, y_, z_] := N[(N[(N[(y + x), $MachinePrecision] - z), $MachinePrecision] - N[(N[(y + 0.5), $MachinePrecision] * N[Log[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(y + x\right) - z\right) - \left(y + 0.5\right) \cdot \log y
\end{array}

Reproduce

?
herbie shell --seed 2023196 
(FPCore (x y z)
  :name "Numeric.SpecFunctions:stirlingError from math-functions-0.1.5.2"
  :precision binary64

  :herbie-target
  (- (- (+ y x) z) (* (+ y 0.5) (log y)))

  (- (+ (- x (* (+ y 0.5) (log y))) y) z))