| Alternative 1 | |
|---|---|
| Accuracy | 90.0% |
| Cost | 13380 |

(FPCore (x y z t) :precision binary64 (- x (/ (log (+ (- 1.0 y) (* y (exp z)))) t)))
(FPCore (x y z t) :precision binary64 (if (<= z 6000000.0) (- 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 (z <= 6000000.0) {
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 (z <= 6000000.0) {
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 z <= 6000000.0: 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 (z <= 6000000.0) 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[z, 6000000.0], 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}\;z \leq 6000000:\\
\;\;\;\;x - \frac{\mathsf{log1p}\left(y \cdot \mathsf{expm1}\left(z\right)\right)}{t}\\
\mathbf{else}:\\
\;\;\;\;x - \frac{y}{\frac{t}{z}}\\
\end{array}
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
| Original | 56.4% |
|---|---|
| Target | 67.6% |
| Herbie | 90.0% |
if z < 6e6Initial program 62.9%
Simplified98.1%
[Start]62.9% | \[ x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t}
\] |
|---|---|
associate-+l- [=>]75.4% | \[ x - \frac{\log \color{blue}{\left(1 - \left(y - y \cdot e^{z}\right)\right)}}{t}
\] |
sub-neg [=>]75.4% | \[ x - \frac{\log \color{blue}{\left(1 + \left(-\left(y - y \cdot e^{z}\right)\right)\right)}}{t}
\] |
log1p-def [=>]80.1% | \[ x - \frac{\color{blue}{\mathsf{log1p}\left(-\left(y - y \cdot e^{z}\right)\right)}}{t}
\] |
neg-sub0 [=>]80.1% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{0 - \left(y - y \cdot e^{z}\right)}\right)}{t}
\] |
associate-+l- [<=]80.1% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{\left(0 - y\right) + y \cdot e^{z}}\right)}{t}
\] |
neg-sub0 [<=]80.1% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{\left(-y\right)} + y \cdot e^{z}\right)}{t}
\] |
neg-mul-1 [=>]80.1% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{-1 \cdot y} + y \cdot e^{z}\right)}{t}
\] |
*-commutative [=>]80.1% | \[ x - \frac{\mathsf{log1p}\left(-1 \cdot y + \color{blue}{e^{z} \cdot y}\right)}{t}
\] |
distribute-rgt-out [=>]80.1% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{y \cdot \left(-1 + e^{z}\right)}\right)}{t}
\] |
+-commutative [=>]80.1% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\left(e^{z} + -1\right)}\right)}{t}
\] |
metadata-eval [<=]80.1% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \left(e^{z} + \color{blue}{\left(-1\right)}\right)\right)}{t}
\] |
sub-neg [<=]80.1% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\left(e^{z} - 1\right)}\right)}{t}
\] |
expm1-def [=>]98.1% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\mathsf{expm1}\left(z\right)}\right)}{t}
\] |
if 6e6 < z Initial program 27.8%
Simplified27.8%
[Start]27.8% | \[ x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t}
\] |
|---|---|
associate-+l- [=>]27.8% | \[ x - \frac{\log \color{blue}{\left(1 - \left(y - y \cdot e^{z}\right)\right)}}{t}
\] |
sub-neg [=>]27.8% | \[ x - \frac{\log \color{blue}{\left(1 + \left(-\left(y - y \cdot e^{z}\right)\right)\right)}}{t}
\] |
log1p-def [=>]27.8% | \[ x - \frac{\color{blue}{\mathsf{log1p}\left(-\left(y - y \cdot e^{z}\right)\right)}}{t}
\] |
neg-sub0 [=>]27.8% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{0 - \left(y - y \cdot e^{z}\right)}\right)}{t}
\] |
associate-+l- [<=]27.8% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{\left(0 - y\right) + y \cdot e^{z}}\right)}{t}
\] |
neg-sub0 [<=]27.8% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{\left(-y\right)} + y \cdot e^{z}\right)}{t}
\] |
neg-mul-1 [=>]27.8% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{-1 \cdot y} + y \cdot e^{z}\right)}{t}
\] |
*-commutative [=>]27.8% | \[ x - \frac{\mathsf{log1p}\left(-1 \cdot y + \color{blue}{e^{z} \cdot y}\right)}{t}
\] |
distribute-rgt-out [=>]27.8% | \[ x - \frac{\mathsf{log1p}\left(\color{blue}{y \cdot \left(-1 + e^{z}\right)}\right)}{t}
\] |
+-commutative [=>]27.8% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\left(e^{z} + -1\right)}\right)}{t}
\] |
metadata-eval [<=]27.8% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \left(e^{z} + \color{blue}{\left(-1\right)}\right)\right)}{t}
\] |
sub-neg [<=]27.8% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\left(e^{z} - 1\right)}\right)}{t}
\] |
expm1-def [=>]27.8% | \[ x - \frac{\mathsf{log1p}\left(y \cdot \color{blue}{\mathsf{expm1}\left(z\right)}\right)}{t}
\] |
Taylor expanded in z around 0 37.8%
Simplified51.4%
[Start]37.8% | \[ x - \frac{y \cdot z}{t}
\] |
|---|---|
associate-/l* [=>]51.4% | \[ x - \color{blue}{\frac{y}{\frac{t}{z}}}
\] |
Final simplification90.2%
| Alternative 1 | |
|---|---|
| Accuracy | 90.0% |
| Cost | 13380 |
| Alternative 2 | |
|---|---|
| Accuracy | 86.3% |
| Cost | 7496 |
| Alternative 3 | |
|---|---|
| Accuracy | 82.6% |
| Cost | 7364 |
| Alternative 4 | |
|---|---|
| Accuracy | 80.2% |
| Cost | 7112 |
| Alternative 5 | |
|---|---|
| Accuracy | 80.3% |
| Cost | 7112 |
| Alternative 6 | |
|---|---|
| Accuracy | 65.2% |
| Cost | 648 |
| Alternative 7 | |
|---|---|
| Accuracy | 72.7% |
| Cost | 580 |
| Alternative 8 | |
|---|---|
| Accuracy | 76.5% |
| Cost | 580 |
| Alternative 9 | |
|---|---|
| Accuracy | 76.3% |
| Cost | 580 |
| Alternative 10 | |
|---|---|
| Accuracy | 64.2% |
| Cost | 64 |
herbie shell --seed 2023263
(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)))