System.Random.MWC.Distributions:gamma from mwc-random-0.13.3.2

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

Specification

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

\\
x \cdot 0.5 + y \cdot \left(\left(1 - z\right) + \log z\right)
\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.9% accurate, 1.0× speedup?

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

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

Alternative 1: 99.9% accurate, 0.5× speedup?

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

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

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

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

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

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

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

Alternative 2: 74.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := y \cdot \left(1 + \log z\right)\\ \mathbf{if}\;z \leq 7.8 \cdot 10^{-212}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.35 \cdot 10^{-188}:\\ \;\;\;\;x \cdot 0.5\\ \mathbf{elif}\;z \leq 2.6 \cdot 10^{-131} \lor \neg \left(z \leq 1.95 \cdot 10^{-53}\right) \land z \leq 7 \cdot 10^{-42}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;x \cdot 0.5 - y \cdot z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (* y (+ 1.0 (log z)))))
   (if (<= z 7.8e-212)
     t_0
     (if (<= z 2.35e-188)
       (* x 0.5)
       (if (or (<= z 2.6e-131) (and (not (<= z 1.95e-53)) (<= z 7e-42)))
         t_0
         (- (* x 0.5) (* y z)))))))
double code(double x, double y, double z) {
	double t_0 = y * (1.0 + log(z));
	double tmp;
	if (z <= 7.8e-212) {
		tmp = t_0;
	} else if (z <= 2.35e-188) {
		tmp = x * 0.5;
	} else if ((z <= 2.6e-131) || (!(z <= 1.95e-53) && (z <= 7e-42))) {
		tmp = t_0;
	} else {
		tmp = (x * 0.5) - (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) :: t_0
    real(8) :: tmp
    t_0 = y * (1.0d0 + log(z))
    if (z <= 7.8d-212) then
        tmp = t_0
    else if (z <= 2.35d-188) then
        tmp = x * 0.5d0
    else if ((z <= 2.6d-131) .or. (.not. (z <= 1.95d-53)) .and. (z <= 7d-42)) then
        tmp = t_0
    else
        tmp = (x * 0.5d0) - (y * z)
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double t_0 = y * (1.0 + Math.log(z));
	double tmp;
	if (z <= 7.8e-212) {
		tmp = t_0;
	} else if (z <= 2.35e-188) {
		tmp = x * 0.5;
	} else if ((z <= 2.6e-131) || (!(z <= 1.95e-53) && (z <= 7e-42))) {
		tmp = t_0;
	} else {
		tmp = (x * 0.5) - (y * z);
	}
	return tmp;
}
def code(x, y, z):
	t_0 = y * (1.0 + math.log(z))
	tmp = 0
	if z <= 7.8e-212:
		tmp = t_0
	elif z <= 2.35e-188:
		tmp = x * 0.5
	elif (z <= 2.6e-131) or (not (z <= 1.95e-53) and (z <= 7e-42)):
		tmp = t_0
	else:
		tmp = (x * 0.5) - (y * z)
	return tmp
function code(x, y, z)
	t_0 = Float64(y * Float64(1.0 + log(z)))
	tmp = 0.0
	if (z <= 7.8e-212)
		tmp = t_0;
	elseif (z <= 2.35e-188)
		tmp = Float64(x * 0.5);
	elseif ((z <= 2.6e-131) || (!(z <= 1.95e-53) && (z <= 7e-42)))
		tmp = t_0;
	else
		tmp = Float64(Float64(x * 0.5) - Float64(y * z));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	t_0 = y * (1.0 + log(z));
	tmp = 0.0;
	if (z <= 7.8e-212)
		tmp = t_0;
	elseif (z <= 2.35e-188)
		tmp = x * 0.5;
	elseif ((z <= 2.6e-131) || (~((z <= 1.95e-53)) && (z <= 7e-42)))
		tmp = t_0;
	else
		tmp = (x * 0.5) - (y * z);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, 7.8e-212], t$95$0, If[LessEqual[z, 2.35e-188], N[(x * 0.5), $MachinePrecision], If[Or[LessEqual[z, 2.6e-131], And[N[Not[LessEqual[z, 1.95e-53]], $MachinePrecision], LessEqual[z, 7e-42]]], t$95$0, N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := y \cdot \left(1 + \log z\right)\\
\mathbf{if}\;z \leq 7.8 \cdot 10^{-212}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;z \leq 2.35 \cdot 10^{-188}:\\
\;\;\;\;x \cdot 0.5\\

\mathbf{elif}\;z \leq 2.6 \cdot 10^{-131} \lor \neg \left(z \leq 1.95 \cdot 10^{-53}\right) \land z \leq 7 \cdot 10^{-42}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if z < 7.8e-212 or 2.34999999999999999e-188 < z < 2.59999999999999996e-131 or 1.9500000000000001e-53 < z < 7.0000000000000004e-42

    1. Initial program 99.6%

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

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

      \[\leadsto \color{blue}{y \cdot \left(1 + \log z\right)} \]

    if 7.8e-212 < z < 2.34999999999999999e-188

    1. Initial program 99.8%

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

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

    if 2.59999999999999996e-131 < z < 1.9500000000000001e-53 or 7.0000000000000004e-42 < z

    1. Initial program 99.9%

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

      \[\leadsto x \cdot 0.5 + \color{blue}{-1 \cdot \left(y \cdot z\right)} \]
    3. Step-by-step derivation
      1. associate-*r*87.7%

        \[\leadsto x \cdot 0.5 + \color{blue}{\left(-1 \cdot y\right) \cdot z} \]
      2. neg-mul-187.7%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq 7.8 \cdot 10^{-212}:\\ \;\;\;\;y \cdot \left(1 + \log z\right)\\ \mathbf{elif}\;z \leq 2.35 \cdot 10^{-188}:\\ \;\;\;\;x \cdot 0.5\\ \mathbf{elif}\;z \leq 2.6 \cdot 10^{-131} \lor \neg \left(z \leq 1.95 \cdot 10^{-53}\right) \land z \leq 7 \cdot 10^{-42}:\\ \;\;\;\;y \cdot \left(1 + \log z\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot 0.5 - y \cdot z\\ \end{array} \]

Alternative 3: 84.7% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.4 \cdot 10^{+49} \lor \neg \left(y \leq 8.5 \cdot 10^{+56}\right):\\
\;\;\;\;y \cdot \left(\left(1 - z\right) + \log z\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -1.3999999999999999e49 or 8.4999999999999998e56 < y

    1. Initial program 99.7%

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

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

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

        \[\leadsto x \cdot 0.5 + \color{blue}{\left(1 \cdot y + \left(\left(-z\right) + \log z\right) \cdot y\right)} \]
      4. *-lft-identity99.7%

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

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

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

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

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

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

        \[\leadsto \color{blue}{\left(x \cdot 0.5 + y\right) - \left(z - \log z\right) \cdot y} \]
      11. fma-def99.7%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, 0.5, y\right)} - \left(z - \log z\right) \cdot y \]
      12. *-commutative99.7%

        \[\leadsto \mathsf{fma}\left(x, 0.5, y\right) - \color{blue}{y \cdot \left(z - \log z\right)} \]
    3. Simplified99.7%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, 0.5, y\right) - y \cdot \left(z - \log z\right)} \]
    4. Step-by-step derivation
      1. expm1-log1p-u97.4%

        \[\leadsto \mathsf{fma}\left(x, 0.5, y\right) - y \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(z - \log z\right)\right)} \]
    5. Applied egg-rr97.4%

      \[\leadsto \mathsf{fma}\left(x, 0.5, y\right) - y \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(z - \log z\right)\right)} \]
    6. Taylor expanded in x around 0 91.0%

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

        \[\leadsto y - \color{blue}{\left(z - \log z\right) \cdot y} \]
      2. cancel-sign-sub-inv91.0%

        \[\leadsto \color{blue}{y + \left(-\left(z - \log z\right)\right) \cdot y} \]
      3. *-lft-identity91.0%

        \[\leadsto \color{blue}{1 \cdot y} + \left(-\left(z - \log z\right)\right) \cdot y \]
      4. distribute-rgt-in91.0%

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

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

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

        \[\leadsto y \cdot \left(\left(-\color{blue}{\left(\left(-\log z\right) + z\right)}\right) + 1\right) \]
      8. distribute-neg-in91.0%

        \[\leadsto y \cdot \left(\color{blue}{\left(\left(-\left(-\log z\right)\right) + \left(-z\right)\right)} + 1\right) \]
      9. remove-double-neg91.0%

        \[\leadsto y \cdot \left(\left(\color{blue}{\log z} + \left(-z\right)\right) + 1\right) \]
      10. associate-+r+91.0%

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

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

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

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

    if -1.3999999999999999e49 < y < 8.4999999999999998e56

    1. Initial program 99.9%

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

      \[\leadsto x \cdot 0.5 + \color{blue}{-1 \cdot \left(y \cdot z\right)} \]
    3. Step-by-step derivation
      1. associate-*r*86.1%

        \[\leadsto x \cdot 0.5 + \color{blue}{\left(-1 \cdot y\right) \cdot z} \]
      2. neg-mul-186.1%

        \[\leadsto x \cdot 0.5 + \color{blue}{\left(-y\right)} \cdot z \]
    4. Simplified86.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.4 \cdot 10^{+49} \lor \neg \left(y \leq 8.5 \cdot 10^{+56}\right):\\ \;\;\;\;y \cdot \left(\left(1 - z\right) + \log z\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot 0.5 - y \cdot z\\ \end{array} \]

Alternative 4: 98.8% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.28:\\
\;\;\;\;x \cdot 0.5 + y \cdot \left(1 + \log z\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\


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

    1. Initial program 99.7%

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

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

    if 0.28000000000000003 < z

    1. Initial program 100.0%

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

      \[\leadsto x \cdot 0.5 + \color{blue}{-1 \cdot \left(y \cdot z\right)} \]
    3. Step-by-step derivation
      1. associate-*r*97.4%

        \[\leadsto x \cdot 0.5 + \color{blue}{\left(-1 \cdot y\right) \cdot z} \]
      2. neg-mul-197.4%

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

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

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

Alternative 5: 98.7% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.28:\\
\;\;\;\;x \cdot 0.5 + \left(y + y \cdot \log z\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\


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

    1. Initial program 99.7%

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

      \[\leadsto x \cdot 0.5 + \color{blue}{y \cdot \left(1 + \log z\right)} \]
    3. Step-by-step derivation
      1. distribute-lft-in99.2%

        \[\leadsto x \cdot 0.5 + \color{blue}{\left(y \cdot 1 + y \cdot \log z\right)} \]
      2. *-rgt-identity99.2%

        \[\leadsto x \cdot 0.5 + \left(\color{blue}{y} + y \cdot \log z\right) \]
    4. Simplified99.2%

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

    if 0.28000000000000003 < z

    1. Initial program 100.0%

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

      \[\leadsto x \cdot 0.5 + \color{blue}{-1 \cdot \left(y \cdot z\right)} \]
    3. Step-by-step derivation
      1. associate-*r*97.4%

        \[\leadsto x \cdot 0.5 + \color{blue}{\left(-1 \cdot y\right) \cdot z} \]
      2. neg-mul-197.4%

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

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

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

Alternative 6: 99.9% accurate, 1.0× speedup?

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

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

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

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

Alternative 7: 74.4% accurate, 15.9× speedup?

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

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

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

    \[\leadsto x \cdot 0.5 + \color{blue}{-1 \cdot \left(y \cdot z\right)} \]
  3. Step-by-step derivation
    1. associate-*r*72.0%

      \[\leadsto x \cdot 0.5 + \color{blue}{\left(-1 \cdot y\right) \cdot z} \]
    2. neg-mul-172.0%

      \[\leadsto x \cdot 0.5 + \color{blue}{\left(-y\right)} \cdot z \]
  4. Simplified72.0%

    \[\leadsto x \cdot 0.5 + \color{blue}{\left(-y\right) \cdot z} \]
  5. Final simplification72.0%

    \[\leadsto x \cdot 0.5 - y \cdot z \]

Alternative 8: 59.1% accurate, 18.3× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;z \leq 2.5 \cdot 10^{+47}:\\
\;\;\;\;x \cdot 0.5\\

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


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

    1. Initial program 99.7%

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

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

    if 2.50000000000000011e47 < z

    1. Initial program 100.0%

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

        \[\leadsto \color{blue}{y \cdot \left(\left(1 - z\right) + \log z\right) + x \cdot 0.5} \]
      2. fma-def100.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(y, \left(1 - z\right) + \log z, x \cdot 0.5\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, \left(1 - z\right) + \log z, x \cdot 0.5\right)} \]
    4. Step-by-step derivation
      1. fma-udef100.0%

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

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

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

        \[\leadsto \color{blue}{\left(x \cdot 0.5 + y \cdot \left(1 - z\right)\right) + y \cdot \log z} \]
    5. Applied egg-rr100.0%

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

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

        \[\leadsto \color{blue}{-y \cdot z} \]
      2. distribute-rgt-neg-in76.1%

        \[\leadsto \color{blue}{y \cdot \left(-z\right)} \]
    8. Simplified76.1%

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

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

Alternative 9: 40.6% accurate, 37.0× speedup?

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

\\
x \cdot 0.5
\end{array}
Derivation
  1. Initial program 99.8%

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

    \[\leadsto \color{blue}{0.5 \cdot x} \]
  3. Final simplification40.4%

    \[\leadsto x \cdot 0.5 \]

Alternative 10: 1.9% accurate, 111.0× speedup?

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

\\
y
\end{array}
Derivation
  1. Initial program 99.8%

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

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

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

      \[\leadsto x \cdot 0.5 + \color{blue}{\left(1 \cdot y + \left(\left(-z\right) + \log z\right) \cdot y\right)} \]
    4. *-lft-identity99.8%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\left(x \cdot 0.5 + y\right) - \left(z - \log z\right) \cdot y} \]
    11. fma-def99.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, 0.5, y\right)} - \left(z - \log z\right) \cdot y \]
    12. *-commutative99.8%

      \[\leadsto \mathsf{fma}\left(x, 0.5, y\right) - \color{blue}{y \cdot \left(z - \log z\right)} \]
  3. Simplified99.8%

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

    \[\leadsto \mathsf{fma}\left(x, 0.5, y\right) - \color{blue}{y \cdot z} \]
  5. Taylor expanded in y around inf 33.0%

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

    \[\leadsto \color{blue}{y} \]
  7. Final simplification2.0%

    \[\leadsto y \]

Developer target: 99.8% accurate, 1.0× speedup?

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

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

Reproduce

?
herbie shell --seed 2023290 
(FPCore (x y z)
  :name "System.Random.MWC.Distributions:gamma from mwc-random-0.13.3.2"
  :precision binary64

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

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