| Alternative 1 | |
|---|---|
| Accuracy | 91.6% |
| Cost | 20036 |

(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.0000006) (- x (/ (log1p (* y (expm1 z))) t)) (- x (/ (+ (log z) (+ (* z 0.5) (log y))) t))))
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.0000006) {
tmp = x - (log1p((y * expm1(z))) / t);
} else {
tmp = x - ((log(z) + ((z * 0.5) + log(y))) / t);
}
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.0000006) {
tmp = x - (Math.log1p((y * Math.expm1(z))) / t);
} else {
tmp = x - ((Math.log(z) + ((z * 0.5) + Math.log(y))) / t);
}
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.0000006: tmp = x - (math.log1p((y * math.expm1(z))) / t) else: tmp = x - ((math.log(z) + ((z * 0.5) + math.log(y))) / t) 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.0000006) tmp = Float64(x - Float64(log1p(Float64(y * expm1(z))) / t)); else tmp = Float64(x - Float64(Float64(log(z) + Float64(Float64(z * 0.5) + log(y))) / t)); 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.0000006], N[(x - N[(N[Log[1 + N[(y * N[(Exp[z] - 1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / t), $MachinePrecision]), $MachinePrecision], N[(x - N[(N[(N[Log[z], $MachinePrecision] + N[(N[(z * 0.5), $MachinePrecision] + N[Log[y], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / t), $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.0000006:\\
\;\;\;\;x - \frac{\mathsf{log1p}\left(y \cdot \mathsf{expm1}\left(z\right)\right)}{t}\\
\mathbf{else}:\\
\;\;\;\;x - \frac{\log z + \left(z \cdot 0.5 + \log y\right)}{t}\\
\end{array}
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
| Original | 52.7% |
|---|---|
| Target | 53.1% |
| Herbie | 91.6% |
if (exp.f64 z) < 1.0000006Initial program 60.7%
Simplified96.4%
[Start]60.7% | \[ x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t}
\] |
|---|---|
associate-+l- [=>]67.9% | \[ x - \frac{\log \color{blue}{\left(1 - \left(y - y \cdot e^{z}\right)\right)}}{t}
\] |
sub-neg [=>]67.9% | \[ x - \frac{\log \color{blue}{\left(1 + \left(-\left(y - y \cdot e^{z}\right)\right)\right)}}{t}
\] |
log1p-def [=>]74.2% | \[ x - \frac{\color{blue}{\mathsf{log1p}\left(-\left(y - y \cdot e^{z}\right)\right)}}{t}
\] |
neg-sub0 [=>]74.2% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{0 - \left(y - y \cdot e^{z}\right)}\right)}{t}
\] |
associate-+l- [<=]74.2% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{\left(0 - y\right) + y \cdot e^{z}}\right)}{t}
\] |
neg-sub0 [<=]74.2% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{\left(-y\right)} + y \cdot e^{z}\right)}{t}
\] |
neg-mul-1 [=>]74.2% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{-1 \cdot y} + y \cdot e^{z}\right)}{t}
\] |
*-commutative [=>]74.2% | \[ x - \frac{\mathsf{log1p}\left(-1 \cdot y + \color{blue}{e^{z} \cdot y}\right)}{t}
\] |
distribute-rgt-out [=>]74.2% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{y \cdot \left(-1 + e^{z}\right)}\right)}{t}
\] |
+-commutative [=>]74.2% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\left(e^{z} + -1\right)}\right)}{t}
\] |
metadata-eval [<=]74.2% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \left(e^{z} + \color{blue}{\left(-1\right)}\right)\right)}{t}
\] |
sub-neg [<=]74.2% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\left(e^{z} - 1\right)}\right)}{t}
\] |
expm1-def [=>]96.4% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\mathsf{expm1}\left(z\right)}\right)}{t}
\] |
if 1.0000006 < (exp.f64 z) Initial program 47.2%
Simplified47.2%
[Start]47.2% | \[ x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t}
\] |
|---|---|
associate-+l- [=>]47.2% | \[ x - \frac{\log \color{blue}{\left(1 - \left(y - y \cdot e^{z}\right)\right)}}{t}
\] |
sub-neg [=>]47.2% | \[ x - \frac{\log \color{blue}{\left(1 + \left(-\left(y - y \cdot e^{z}\right)\right)\right)}}{t}
\] |
log1p-def [=>]47.2% | \[ x - \frac{\color{blue}{\mathsf{log1p}\left(-\left(y - y \cdot e^{z}\right)\right)}}{t}
\] |
neg-sub0 [=>]47.2% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{0 - \left(y - y \cdot e^{z}\right)}\right)}{t}
\] |
associate-+l- [<=]47.2% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{\left(0 - y\right) + y \cdot e^{z}}\right)}{t}
\] |
neg-sub0 [<=]47.2% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{\left(-y\right)} + y \cdot e^{z}\right)}{t}
\] |
neg-mul-1 [=>]47.2% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{-1 \cdot y} + y \cdot e^{z}\right)}{t}
\] |
*-commutative [=>]47.2% | \[ x - \frac{\mathsf{log1p}\left(-1 \cdot y + \color{blue}{e^{z} \cdot y}\right)}{t}
\] |
distribute-rgt-out [=>]47.2% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{y \cdot \left(-1 + e^{z}\right)}\right)}{t}
\] |
+-commutative [=>]47.2% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\left(e^{z} + -1\right)}\right)}{t}
\] |
metadata-eval [<=]47.2% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \left(e^{z} + \color{blue}{\left(-1\right)}\right)\right)}{t}
\] |
sub-neg [<=]47.2% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\left(e^{z} - 1\right)}\right)}{t}
\] |
expm1-def [=>]47.2% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\mathsf{expm1}\left(z\right)}\right)}{t}
\] |
Taylor expanded in y around inf 47.2%
Simplified47.2%
[Start]47.2% | \[ x - \frac{\log \left(e^{z} - 1\right) + -1 \cdot \log \left(\frac{1}{y}\right)}{t}
\] |
|---|---|
mul-1-neg [=>]47.2% | \[ x - \frac{\log \left(e^{z} - 1\right) + \color{blue}{\left(-\log \left(\frac{1}{y}\right)\right)}}{t}
\] |
expm1-def [=>]47.2% | \[ x - \frac{\log \color{blue}{\left(\mathsf{expm1}\left(z\right)\right)} + \left(-\log \left(\frac{1}{y}\right)\right)}{t}
\] |
log-rec [=>]47.2% | \[ x - \frac{\log \left(\mathsf{expm1}\left(z\right)\right) + \left(-\color{blue}{\left(-\log y\right)}\right)}{t}
\] |
Taylor expanded in z around 0 58.6%
Final simplification90.3%
| Alternative 1 | |
|---|---|
| Accuracy | 91.6% |
| Cost | 20036 |
| Alternative 2 | |
|---|---|
| Accuracy | 82.5% |
| Cost | 13449 |
| Alternative 3 | |
|---|---|
| Accuracy | 89.9% |
| Cost | 13248 |
| Alternative 4 | |
|---|---|
| Accuracy | 79.5% |
| Cost | 7364 |
| Alternative 5 | |
|---|---|
| Accuracy | 76.9% |
| Cost | 6848 |
| Alternative 6 | |
|---|---|
| Accuracy | 64.2% |
| Cost | 712 |
| Alternative 7 | |
|---|---|
| Accuracy | 70.0% |
| Cost | 580 |
| Alternative 8 | |
|---|---|
| Accuracy | 47.8% |
| Cost | 516 |
| Alternative 9 | |
|---|---|
| Accuracy | 49.7% |
| Cost | 516 |
| Alternative 10 | |
|---|---|
| Accuracy | 46.5% |
| Cost | 64 |
herbie shell --seed 2023277
(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)))