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

?

Percentage Accurate: 56.4% → 90.5%
Time: 19.4s
Precision: binary64
Cost: 19780

?

\[x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t} \]
\[\begin{array}{l} \mathbf{if}\;e^{z} \leq 1.025:\\ \;\;\;\;x - \frac{\mathsf{log1p}\left(y \cdot \mathsf{expm1}\left(z\right)\right)}{t}\\ \mathbf{else}:\\ \;\;\;\;x - \frac{y}{\frac{t}{z}}\\ \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (- x (/ (log (+ (- 1.0 y) (* y (exp z)))) t)))
(FPCore (x y z t)
 :precision binary64
 (if (<= (exp z) 1.025)
   (- x (/ (log1p (* y (expm1 z))) t))
   (- x (/ y (/ t z)))))
double code(double x, double y, double z, double t) {
	return x - (log(((1.0 - y) + (y * exp(z)))) / t);
}
double code(double x, double y, double z, double t) {
	double tmp;
	if (exp(z) <= 1.025) {
		tmp = x - (log1p((y * expm1(z))) / t);
	} else {
		tmp = x - (y / (t / z));
	}
	return tmp;
}
public static double code(double x, double y, double z, double t) {
	return x - (Math.log(((1.0 - y) + (y * Math.exp(z)))) / t);
}
public static double code(double x, double y, double z, double t) {
	double tmp;
	if (Math.exp(z) <= 1.025) {
		tmp = x - (Math.log1p((y * Math.expm1(z))) / t);
	} else {
		tmp = x - (y / (t / z));
	}
	return tmp;
}
def code(x, y, z, t):
	return x - (math.log(((1.0 - y) + (y * math.exp(z)))) / t)
def code(x, y, z, t):
	tmp = 0
	if math.exp(z) <= 1.025:
		tmp = x - (math.log1p((y * math.expm1(z))) / t)
	else:
		tmp = x - (y / (t / z))
	return tmp
function code(x, y, z, t)
	return Float64(x - Float64(log(Float64(Float64(1.0 - y) + Float64(y * exp(z)))) / t))
end
function code(x, y, z, t)
	tmp = 0.0
	if (exp(z) <= 1.025)
		tmp = Float64(x - Float64(log1p(Float64(y * expm1(z))) / t));
	else
		tmp = Float64(x - Float64(y / Float64(t / z)));
	end
	return tmp
end
code[x_, y_, z_, t_] := N[(x - N[(N[Log[N[(N[(1.0 - y), $MachinePrecision] + N[(y * N[Exp[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / t), $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_, t_] := If[LessEqual[N[Exp[z], $MachinePrecision], 1.025], N[(x - N[(N[Log[1 + N[(y * N[(Exp[z] - 1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / t), $MachinePrecision]), $MachinePrecision], N[(x - N[(y / N[(t / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t}
\begin{array}{l}
\mathbf{if}\;e^{z} \leq 1.025:\\
\;\;\;\;x - \frac{\mathsf{log1p}\left(y \cdot \mathsf{expm1}\left(z\right)\right)}{t}\\

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


\end{array}

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.

Herbie found 8 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original56.4%
Target68.9%
Herbie90.5%
\[\begin{array}{l} \mathbf{if}\;z < -2.8874623088207947 \cdot 10^{+119}:\\ \;\;\;\;\left(x - \frac{\frac{-0.5}{y \cdot t}}{z \cdot z}\right) - \frac{-0.5}{y \cdot t} \cdot \frac{\frac{2}{z}}{z \cdot z}\\ \mathbf{else}:\\ \;\;\;\;x - \frac{\log \left(1 + z \cdot y\right)}{t}\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if (exp.f64 z) < 1.0249999999999999

    1. Initial program 62.2%

      \[x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t} \]
    2. Simplified98.6%

      \[\leadsto \color{blue}{x - \frac{\mathsf{log1p}\left(y \cdot \mathsf{expm1}\left(z\right)\right)}{t}} \]
      Step-by-step derivation

      [Start]62.2%

      \[ x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t} \]

      associate-+l- [=>]79.6%

      \[ x - \frac{\log \color{blue}{\left(1 - \left(y - y \cdot e^{z}\right)\right)}}{t} \]

      sub-neg [=>]79.6%

      \[ x - \frac{\log \color{blue}{\left(1 + \left(-\left(y - y \cdot e^{z}\right)\right)\right)}}{t} \]

      log1p-def [=>]86.3%

      \[ x - \frac{\color{blue}{\mathsf{log1p}\left(-\left(y - y \cdot e^{z}\right)\right)}}{t} \]

      neg-sub0 [=>]86.3%

      \[ x - \frac{\mathsf{log1p}\left(\color{blue}{0 - \left(y - y \cdot e^{z}\right)}\right)}{t} \]

      associate-+l- [<=]86.3%

      \[ x - \frac{\mathsf{log1p}\left(\color{blue}{\left(0 - y\right) + y \cdot e^{z}}\right)}{t} \]

      neg-sub0 [<=]86.3%

      \[ x - \frac{\mathsf{log1p}\left(\color{blue}{\left(-y\right)} + y \cdot e^{z}\right)}{t} \]

      neg-mul-1 [=>]86.3%

      \[ x - \frac{\mathsf{log1p}\left(\color{blue}{-1 \cdot y} + y \cdot e^{z}\right)}{t} \]

      *-commutative [=>]86.3%

      \[ x - \frac{\mathsf{log1p}\left(-1 \cdot y + \color{blue}{e^{z} \cdot y}\right)}{t} \]

      distribute-rgt-out [=>]86.3%

      \[ x - \frac{\mathsf{log1p}\left(\color{blue}{y \cdot \left(-1 + e^{z}\right)}\right)}{t} \]

      +-commutative [=>]86.3%

      \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\left(e^{z} + -1\right)}\right)}{t} \]

      metadata-eval [<=]86.3%

      \[ x - \frac{\mathsf{log1p}\left(y \cdot \left(e^{z} + \color{blue}{\left(-1\right)}\right)\right)}{t} \]

      sub-neg [<=]86.3%

      \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\left(e^{z} - 1\right)}\right)}{t} \]

      expm1-def [=>]98.6%

      \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\mathsf{expm1}\left(z\right)}\right)}{t} \]

    if 1.0249999999999999 < (exp.f64 z)

    1. Initial program 34.6%

      \[x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t} \]
    2. Simplified34.6%

      \[\leadsto \color{blue}{x - \frac{\mathsf{log1p}\left(y \cdot \mathsf{expm1}\left(z\right)\right)}{t}} \]
      Step-by-step derivation

      [Start]34.6%

      \[ x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t} \]

      associate-+l- [=>]34.6%

      \[ x - \frac{\log \color{blue}{\left(1 - \left(y - y \cdot e^{z}\right)\right)}}{t} \]

      sub-neg [=>]34.6%

      \[ x - \frac{\log \color{blue}{\left(1 + \left(-\left(y - y \cdot e^{z}\right)\right)\right)}}{t} \]

      log1p-def [=>]34.6%

      \[ x - \frac{\color{blue}{\mathsf{log1p}\left(-\left(y - y \cdot e^{z}\right)\right)}}{t} \]

      neg-sub0 [=>]34.6%

      \[ x - \frac{\mathsf{log1p}\left(\color{blue}{0 - \left(y - y \cdot e^{z}\right)}\right)}{t} \]

      associate-+l- [<=]34.6%

      \[ x - \frac{\mathsf{log1p}\left(\color{blue}{\left(0 - y\right) + y \cdot e^{z}}\right)}{t} \]

      neg-sub0 [<=]34.6%

      \[ x - \frac{\mathsf{log1p}\left(\color{blue}{\left(-y\right)} + y \cdot e^{z}\right)}{t} \]

      neg-mul-1 [=>]34.6%

      \[ x - \frac{\mathsf{log1p}\left(\color{blue}{-1 \cdot y} + y \cdot e^{z}\right)}{t} \]

      *-commutative [=>]34.6%

      \[ x - \frac{\mathsf{log1p}\left(-1 \cdot y + \color{blue}{e^{z} \cdot y}\right)}{t} \]

      distribute-rgt-out [=>]34.6%

      \[ x - \frac{\mathsf{log1p}\left(\color{blue}{y \cdot \left(-1 + e^{z}\right)}\right)}{t} \]

      +-commutative [=>]34.6%

      \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\left(e^{z} + -1\right)}\right)}{t} \]

      metadata-eval [<=]34.6%

      \[ x - \frac{\mathsf{log1p}\left(y \cdot \left(e^{z} + \color{blue}{\left(-1\right)}\right)\right)}{t} \]

      sub-neg [<=]34.6%

      \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\left(e^{z} - 1\right)}\right)}{t} \]

      expm1-def [=>]34.6%

      \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\mathsf{expm1}\left(z\right)}\right)}{t} \]
    3. Taylor expanded in z around 0 45.7%

      \[\leadsto x - \color{blue}{\frac{y \cdot z}{t}} \]
    4. Simplified51.3%

      \[\leadsto x - \color{blue}{\frac{y}{\frac{t}{z}}} \]
      Step-by-step derivation

      [Start]45.7%

      \[ x - \frac{y \cdot z}{t} \]

      associate-/l* [=>]51.3%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{z} \leq 1.025:\\ \;\;\;\;x - \frac{\mathsf{log1p}\left(y \cdot \mathsf{expm1}\left(z\right)\right)}{t}\\ \mathbf{else}:\\ \;\;\;\;x - \frac{y}{\frac{t}{z}}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy90.5%
Cost19780
\[\begin{array}{l} \mathbf{if}\;e^{z} \leq 1.025:\\ \;\;\;\;x - \frac{\mathsf{log1p}\left(y \cdot \mathsf{expm1}\left(z\right)\right)}{t}\\ \mathbf{else}:\\ \;\;\;\;x - \frac{y}{\frac{t}{z}}\\ \end{array} \]
Alternative 2
Accuracy80.7%
Cost13448
\[\begin{array}{l} \mathbf{if}\;t \leq -3.6 \cdot 10^{+24}:\\ \;\;\;\;x\\ \mathbf{elif}\;t \leq 1.9 \cdot 10^{+26}:\\ \;\;\;\;\mathsf{fma}\left(y, \frac{-\mathsf{expm1}\left(z\right)}{t}, x\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 3
Accuracy78.3%
Cost7112
\[\begin{array}{l} \mathbf{if}\;t \leq -2.15 \cdot 10^{+24}:\\ \;\;\;\;x\\ \mathbf{elif}\;t \leq 9.5 \cdot 10^{+28}:\\ \;\;\;\;x - \frac{\mathsf{expm1}\left(z\right)}{\frac{t}{y}}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 4
Accuracy79.8%
Cost7112
\[\begin{array}{l} \mathbf{if}\;t \leq -9 \cdot 10^{+24}:\\ \;\;\;\;x\\ \mathbf{elif}\;t \leq 2.3 \cdot 10^{+26}:\\ \;\;\;\;x - \frac{y \cdot \mathsf{expm1}\left(z\right)}{t}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 5
Accuracy66.4%
Cost648
\[\begin{array}{l} \mathbf{if}\;t \leq -4.6 \cdot 10^{-203}:\\ \;\;\;\;x\\ \mathbf{elif}\;t \leq 3.85 \cdot 10^{-177}:\\ \;\;\;\;-\frac{y}{\frac{t}{z}}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
Alternative 6
Accuracy73.3%
Cost580
\[\begin{array}{l} \mathbf{if}\;z \leq -2.35 \cdot 10^{-17}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;x - z \cdot \frac{y}{t}\\ \end{array} \]
Alternative 7
Accuracy76.8%
Cost580
\[\begin{array}{l} \mathbf{if}\;z \leq -1.18 \cdot 10^{-17}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;x - \frac{y}{\frac{t}{z}}\\ \end{array} \]
Alternative 8
Accuracy65.2%
Cost64
\[x \]

Reproduce?

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

  :herbie-target
  (if (< z -2.8874623088207947e+119) (- (- x (/ (/ (- 0.5) (* y t)) (* z z))) (* (/ (- 0.5) (* y t)) (/ (/ 2.0 z) (* z z)))) (- x (/ (log (+ 1.0 (* z y))) t)))

  (- x (/ (log (+ (- 1.0 y) (* y (exp z)))) t)))