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

Percentage Accurate: 99.9% → 99.9%
Time: 20.2s
Alternatives: 8
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 8 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, 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.9%

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

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

Alternative 2: 74.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := y + y \cdot \log z\\ t_1 := x \cdot 0.5 - y \cdot z\\ \mathbf{if}\;z \leq 1.7 \cdot 10^{-190}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 7.4 \cdot 10^{-146}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 4.5 \cdot 10^{-89}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.6 \cdot 10^{-56}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;z \leq 0.05:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (+ y (* y (log z)))) (t_1 (- (* x 0.5) (* y z))))
   (if (<= z 1.7e-190)
     t_0
     (if (<= z 7.4e-146)
       t_1
       (if (<= z 4.5e-89)
         t_0
         (if (<= z 2.6e-56)
           t_1
           (if (<= z 0.05) t_0 (fma (- z) y (* x 0.5)))))))))
double code(double x, double y, double z) {
	double t_0 = y + (y * log(z));
	double t_1 = (x * 0.5) - (y * z);
	double tmp;
	if (z <= 1.7e-190) {
		tmp = t_0;
	} else if (z <= 7.4e-146) {
		tmp = t_1;
	} else if (z <= 4.5e-89) {
		tmp = t_0;
	} else if (z <= 2.6e-56) {
		tmp = t_1;
	} else if (z <= 0.05) {
		tmp = t_0;
	} else {
		tmp = fma(-z, y, (x * 0.5));
	}
	return tmp;
}
function code(x, y, z)
	t_0 = Float64(y + Float64(y * log(z)))
	t_1 = Float64(Float64(x * 0.5) - Float64(y * z))
	tmp = 0.0
	if (z <= 1.7e-190)
		tmp = t_0;
	elseif (z <= 7.4e-146)
		tmp = t_1;
	elseif (z <= 4.5e-89)
		tmp = t_0;
	elseif (z <= 2.6e-56)
		tmp = t_1;
	elseif (z <= 0.05)
		tmp = t_0;
	else
		tmp = fma(Float64(-z), y, Float64(x * 0.5));
	end
	return tmp
end
code[x_, y_, z_] := Block[{t$95$0 = N[(y + N[(y * N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, 1.7e-190], t$95$0, If[LessEqual[z, 7.4e-146], t$95$1, If[LessEqual[z, 4.5e-89], t$95$0, If[LessEqual[z, 2.6e-56], t$95$1, If[LessEqual[z, 0.05], t$95$0, N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := y + y \cdot \log z\\
t_1 := x \cdot 0.5 - y \cdot z\\
\mathbf{if}\;z \leq 1.7 \cdot 10^{-190}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;z \leq 7.4 \cdot 10^{-146}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;z \leq 4.5 \cdot 10^{-89}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;z \leq 2.6 \cdot 10^{-56}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;z \leq 0.05:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if z < 1.69999999999999991e-190 or 7.39999999999999973e-146 < z < 4.4999999999999999e-89 or 2.59999999999999997e-56 < z < 0.050000000000000003

    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.7%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{y \cdot \log z + y} \]

    if 1.69999999999999991e-190 < z < 7.39999999999999973e-146 or 4.4999999999999999e-89 < z < 2.59999999999999997e-56

    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 68.4%

      \[\leadsto x \cdot 0.5 + \color{blue}{-1 \cdot \left(y \cdot z\right)} \]
    3. Step-by-step derivation
      1. mul-1-neg68.4%

        \[\leadsto x \cdot 0.5 + \color{blue}{\left(-y \cdot z\right)} \]
      2. distribute-rgt-neg-out68.4%

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

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

    if 0.050000000000000003 < 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. *-commutative100.0%

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

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

        \[\leadsto \mathsf{fma}\left(\color{blue}{\left(1 + \left(-z\right)\right)} + \log z, y, x \cdot 0.5\right) \]
      5. associate-+l+100.0%

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

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

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

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

      \[\leadsto \mathsf{fma}\left(\color{blue}{-1 \cdot z}, y, x \cdot 0.5\right) \]
    5. Step-by-step derivation
      1. neg-mul-199.4%

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

      \[\leadsto \mathsf{fma}\left(\color{blue}{-z}, y, x \cdot 0.5\right) \]
  3. Recombined 3 regimes into one program.
  4. Final simplification85.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq 1.7 \cdot 10^{-190}:\\ \;\;\;\;y + y \cdot \log z\\ \mathbf{elif}\;z \leq 7.4 \cdot 10^{-146}:\\ \;\;\;\;x \cdot 0.5 - y \cdot z\\ \mathbf{elif}\;z \leq 4.5 \cdot 10^{-89}:\\ \;\;\;\;y + y \cdot \log z\\ \mathbf{elif}\;z \leq 2.6 \cdot 10^{-56}:\\ \;\;\;\;x \cdot 0.5 - y \cdot z\\ \mathbf{elif}\;z \leq 0.05:\\ \;\;\;\;y + y \cdot \log z\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\ \end{array} \]

Alternative 3: 74.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq 1.65 \cdot 10^{-190} \lor \neg \left(z \leq 2.25 \cdot 10^{-147}\right) \land \left(z \leq 7.5 \cdot 10^{-89} \lor \neg \left(z \leq 4 \cdot 10^{-56}\right) \land z \leq 0.05\right):\\ \;\;\;\;y + y \cdot \log z\\ \mathbf{else}:\\ \;\;\;\;x \cdot 0.5 - y \cdot z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= z 1.65e-190)
         (and (not (<= z 2.25e-147))
              (or (<= z 7.5e-89) (and (not (<= z 4e-56)) (<= z 0.05)))))
   (+ y (* y (log z)))
   (- (* x 0.5) (* y z))))
double code(double x, double y, double z) {
	double tmp;
	if ((z <= 1.65e-190) || (!(z <= 2.25e-147) && ((z <= 7.5e-89) || (!(z <= 4e-56) && (z <= 0.05))))) {
		tmp = 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 <= 1.65d-190) .or. (.not. (z <= 2.25d-147)) .and. (z <= 7.5d-89) .or. (.not. (z <= 4d-56)) .and. (z <= 0.05d0)) then
        tmp = 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 <= 1.65e-190) || (!(z <= 2.25e-147) && ((z <= 7.5e-89) || (!(z <= 4e-56) && (z <= 0.05))))) {
		tmp = y + (y * Math.log(z));
	} else {
		tmp = (x * 0.5) - (y * z);
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (z <= 1.65e-190) or (not (z <= 2.25e-147) and ((z <= 7.5e-89) or (not (z <= 4e-56) and (z <= 0.05)))):
		tmp = y + (y * math.log(z))
	else:
		tmp = (x * 0.5) - (y * z)
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((z <= 1.65e-190) || (!(z <= 2.25e-147) && ((z <= 7.5e-89) || (!(z <= 4e-56) && (z <= 0.05)))))
		tmp = 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 <= 1.65e-190) || (~((z <= 2.25e-147)) && ((z <= 7.5e-89) || (~((z <= 4e-56)) && (z <= 0.05)))))
		tmp = y + (y * log(z));
	else
		tmp = (x * 0.5) - (y * z);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[z, 1.65e-190], And[N[Not[LessEqual[z, 2.25e-147]], $MachinePrecision], Or[LessEqual[z, 7.5e-89], And[N[Not[LessEqual[z, 4e-56]], $MachinePrecision], LessEqual[z, 0.05]]]]], N[(y + N[(y * 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}\;z \leq 1.65 \cdot 10^{-190} \lor \neg \left(z \leq 2.25 \cdot 10^{-147}\right) \land \left(z \leq 7.5 \cdot 10^{-89} \lor \neg \left(z \leq 4 \cdot 10^{-56}\right) \land z \leq 0.05\right):\\
\;\;\;\;y + y \cdot \log z\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < 1.65000000000000009e-190 or 2.24999999999999986e-147 < z < 7.4999999999999999e-89 or 4.0000000000000002e-56 < z < 0.050000000000000003

    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.7%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{y \cdot \log z + y} \]

    if 1.65000000000000009e-190 < z < 2.24999999999999986e-147 or 7.4999999999999999e-89 < z < 4.0000000000000002e-56 or 0.050000000000000003 < 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 92.7%

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

        \[\leadsto x \cdot 0.5 + \color{blue}{\left(-y \cdot z\right)} \]
      2. distribute-rgt-neg-out92.7%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq 1.65 \cdot 10^{-190} \lor \neg \left(z \leq 2.25 \cdot 10^{-147}\right) \land \left(z \leq 7.5 \cdot 10^{-89} \lor \neg \left(z \leq 4 \cdot 10^{-56}\right) \land z \leq 0.05\right):\\ \;\;\;\;y + y \cdot \log z\\ \mathbf{else}:\\ \;\;\;\;x \cdot 0.5 - y \cdot z\\ \end{array} \]

Alternative 4: 85.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -6.2 \cdot 10^{+30} \lor \neg \left(y \leq 35\right):\\ \;\;\;\;y \cdot \left(\left(1 - z\right) + \log z\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= y -6.2e+30) (not (<= y 35.0)))
   (* y (+ (- 1.0 z) (log z)))
   (fma (- z) y (* x 0.5))))
double code(double x, double y, double z) {
	double tmp;
	if ((y <= -6.2e+30) || !(y <= 35.0)) {
		tmp = y * ((1.0 - z) + log(z));
	} else {
		tmp = fma(-z, y, (x * 0.5));
	}
	return tmp;
}
function code(x, y, z)
	tmp = 0.0
	if ((y <= -6.2e+30) || !(y <= 35.0))
		tmp = Float64(y * Float64(Float64(1.0 - z) + log(z)));
	else
		tmp = fma(Float64(-z), y, Float64(x * 0.5));
	end
	return tmp
end
code[x_, y_, z_] := If[Or[LessEqual[y, -6.2e+30], N[Not[LessEqual[y, 35.0]], $MachinePrecision]], N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -6.2 \cdot 10^{+30} \lor \neg \left(y \leq 35\right):\\
\;\;\;\;y \cdot \left(\left(1 - z\right) + \log z\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -6.1999999999999995e30 or 35 < y

    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. *-commutative99.8%

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

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

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

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

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

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

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

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

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

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

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

    if -6.1999999999999995e30 < y < 35

    1. Initial program 99.9%

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

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

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

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

        \[\leadsto \mathsf{fma}\left(\color{blue}{\left(1 + \left(-z\right)\right)} + \log z, y, x \cdot 0.5\right) \]
      5. associate-+l+99.9%

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

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

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

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

      \[\leadsto \mathsf{fma}\left(\color{blue}{-1 \cdot z}, y, x \cdot 0.5\right) \]
    5. Step-by-step derivation
      1. neg-mul-185.8%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -6.2 \cdot 10^{+30} \lor \neg \left(y \leq 35\right):\\ \;\;\;\;y \cdot \left(\left(1 - z\right) + \log z\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\ \end{array} \]

Alternative 5: 98.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq 0.062:\\ \;\;\;\;x \cdot 0.5 + \left(y + y \cdot \log z\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= z 0.062) (+ (* x 0.5) (+ y (* y (log z)))) (fma (- z) y (* x 0.5))))
double code(double x, double y, double z) {
	double tmp;
	if (z <= 0.062) {
		tmp = (x * 0.5) + (y + (y * log(z)));
	} else {
		tmp = fma(-z, y, (x * 0.5));
	}
	return tmp;
}
function code(x, y, z)
	tmp = 0.0
	if (z <= 0.062)
		tmp = Float64(Float64(x * 0.5) + Float64(y + Float64(y * log(z))));
	else
		tmp = fma(Float64(-z), y, Float64(x * 0.5));
	end
	return tmp
end
code[x_, y_, z_] := If[LessEqual[z, 0.062], N[(N[(x * 0.5), $MachinePrecision] + N[(y + N[(y * N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

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

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\


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

    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 0 97.9%

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

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

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

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

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

    if 0.062 < 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. *-commutative100.0%

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

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

        \[\leadsto \mathsf{fma}\left(\color{blue}{\left(1 + \left(-z\right)\right)} + \log z, y, x \cdot 0.5\right) \]
      5. associate-+l+100.0%

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

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

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

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

      \[\leadsto \mathsf{fma}\left(\color{blue}{-1 \cdot z}, y, x \cdot 0.5\right) \]
    5. Step-by-step derivation
      1. neg-mul-199.4%

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

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

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

Alternative 6: 59.0% accurate, 10.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq 2.1 \cdot 10^{+58} \lor \neg \left(z \leq 6.6 \cdot 10^{+187}\right) \land z \leq 2.7 \cdot 10^{+196}:\\ \;\;\;\;x \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(-z\right)\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= z 2.1e+58) (and (not (<= z 6.6e+187)) (<= z 2.7e+196)))
   (* x 0.5)
   (* y (- z))))
double code(double x, double y, double z) {
	double tmp;
	if ((z <= 2.1e+58) || (!(z <= 6.6e+187) && (z <= 2.7e+196))) {
		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.1d+58) .or. (.not. (z <= 6.6d+187)) .and. (z <= 2.7d+196)) 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.1e+58) || (!(z <= 6.6e+187) && (z <= 2.7e+196))) {
		tmp = x * 0.5;
	} else {
		tmp = y * -z;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (z <= 2.1e+58) or (not (z <= 6.6e+187) and (z <= 2.7e+196)):
		tmp = x * 0.5
	else:
		tmp = y * -z
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((z <= 2.1e+58) || (!(z <= 6.6e+187) && (z <= 2.7e+196)))
		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.1e+58) || (~((z <= 6.6e+187)) && (z <= 2.7e+196)))
		tmp = x * 0.5;
	else
		tmp = y * -z;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[z, 2.1e+58], And[N[Not[LessEqual[z, 6.6e+187]], $MachinePrecision], LessEqual[z, 2.7e+196]]], N[(x * 0.5), $MachinePrecision], N[(y * (-z)), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;z \leq 2.1 \cdot 10^{+58} \lor \neg \left(z \leq 6.6 \cdot 10^{+187}\right) \land z \leq 2.7 \cdot 10^{+196}:\\
\;\;\;\;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.10000000000000012e58 or 6.6000000000000003e187 < z < 2.69999999999999995e196

    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 46.8%

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

    if 2.10000000000000012e58 < z < 6.6000000000000003e187 or 2.69999999999999995e196 < 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. *-commutative100.0%

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

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

        \[\leadsto \mathsf{fma}\left(\color{blue}{\left(1 + \left(-z\right)\right)} + \log z, y, x \cdot 0.5\right) \]
      5. associate-+l+100.0%

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

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

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

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

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

        \[\leadsto \color{blue}{-y \cdot z} \]
      2. *-commutative77.2%

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

        \[\leadsto \color{blue}{z \cdot \left(-y\right)} \]
    6. Simplified77.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq 2.1 \cdot 10^{+58} \lor \neg \left(z \leq 6.6 \cdot 10^{+187}\right) \land z \leq 2.7 \cdot 10^{+196}:\\ \;\;\;\;x \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(-z\right)\\ \end{array} \]

Alternative 7: 74.8% 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.9%

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

    \[\leadsto x \cdot 0.5 + \color{blue}{-1 \cdot \left(y \cdot z\right)} \]
  3. Step-by-step derivation
    1. mul-1-neg72.5%

      \[\leadsto x \cdot 0.5 + \color{blue}{\left(-y \cdot z\right)} \]
    2. distribute-rgt-neg-out72.5%

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

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

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

Alternative 8: 39.4% 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.9%

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

    \[\leadsto \color{blue}{0.5 \cdot x} \]
  3. Final simplification38.0%

    \[\leadsto x \cdot 0.5 \]

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 2023278 
(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)))))