| Alternative 1 | |
|---|---|
| Error | 19.7 |
| Cost | 1112 |
(FPCore (x y z) :precision binary64 (/ (* x (+ (- y z) 1.0)) z))
(FPCore (x y z) :precision binary64 (if (<= z -2e+20) (* (/ (+ y (- 1.0 z)) z) x) (if (<= z 2.4e+21) (- (/ (fma x y x) z) x) (- (/ x (/ z y)) x))))
double code(double x, double y, double z) {
return (x * ((y - z) + 1.0)) / z;
}
double code(double x, double y, double z) {
double tmp;
if (z <= -2e+20) {
tmp = ((y + (1.0 - z)) / z) * x;
} else if (z <= 2.4e+21) {
tmp = (fma(x, y, x) / z) - x;
} else {
tmp = (x / (z / y)) - x;
}
return tmp;
}
function code(x, y, z) return Float64(Float64(x * Float64(Float64(y - z) + 1.0)) / z) end
function code(x, y, z) tmp = 0.0 if (z <= -2e+20) tmp = Float64(Float64(Float64(y + Float64(1.0 - z)) / z) * x); elseif (z <= 2.4e+21) tmp = Float64(Float64(fma(x, y, x) / z) - x); else tmp = Float64(Float64(x / Float64(z / y)) - x); end return tmp end
code[x_, y_, z_] := N[(N[(x * N[(N[(y - z), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
code[x_, y_, z_] := If[LessEqual[z, -2e+20], N[(N[(N[(y + N[(1.0 - z), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision] * x), $MachinePrecision], If[LessEqual[z, 2.4e+21], N[(N[(N[(x * y + x), $MachinePrecision] / z), $MachinePrecision] - x), $MachinePrecision], N[(N[(x / N[(z / y), $MachinePrecision]), $MachinePrecision] - x), $MachinePrecision]]]
\frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}
\begin{array}{l}
\mathbf{if}\;z \leq -2 \cdot 10^{+20}:\\
\;\;\;\;\frac{y + \left(1 - z\right)}{z} \cdot x\\
\mathbf{elif}\;z \leq 2.4 \cdot 10^{+21}:\\
\;\;\;\;\frac{\mathsf{fma}\left(x, y, x\right)}{z} - x\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{\frac{z}{y}} - x\\
\end{array}
| Original | 10.4 |
|---|---|
| Target | 0.4 |
| Herbie | 0.1 |
if z < -2e20Initial program 17.5
Applied egg-rr0.1
if -2e20 < z < 2.4e21Initial program 0.2
Simplified0.2
[Start]0.2 | \[ \frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}
\] |
|---|---|
associate-*r/ [<=]7.9 | \[ \color{blue}{x \cdot \frac{\left(y - z\right) + 1}{z}}
\] |
+-commutative [=>]7.9 | \[ x \cdot \frac{\color{blue}{1 + \left(y - z\right)}}{z}
\] |
associate-+r- [=>]7.9 | \[ x \cdot \frac{\color{blue}{\left(1 + y\right) - z}}{z}
\] |
div-sub [=>]7.9 | \[ x \cdot \color{blue}{\left(\frac{1 + y}{z} - \frac{z}{z}\right)}
\] |
*-inverses [=>]7.9 | \[ x \cdot \left(\frac{1 + y}{z} - \color{blue}{1}\right)
\] |
distribute-rgt-out-- [<=]7.9 | \[ \color{blue}{\frac{1 + y}{z} \cdot x - 1 \cdot x}
\] |
*-lft-identity [=>]7.9 | \[ \frac{1 + y}{z} \cdot x - \color{blue}{x}
\] |
*-commutative [=>]7.9 | \[ \color{blue}{x \cdot \frac{1 + y}{z}} - x
\] |
associate-*r/ [=>]0.2 | \[ \color{blue}{\frac{x \cdot \left(1 + y\right)}{z}} - x
\] |
*-commutative [=>]0.2 | \[ \frac{\color{blue}{\left(1 + y\right) \cdot x}}{z} - x
\] |
+-commutative [=>]0.2 | \[ \frac{\color{blue}{\left(y + 1\right)} \cdot x}{z} - x
\] |
distribute-lft1-in [<=]0.2 | \[ \frac{\color{blue}{y \cdot x + x}}{z} - x
\] |
*-commutative [=>]0.2 | \[ \frac{\color{blue}{x \cdot y} + x}{z} - x
\] |
fma-def [=>]0.2 | \[ \frac{\color{blue}{\mathsf{fma}\left(x, y, x\right)}}{z} - x
\] |
if 2.4e21 < z Initial program 18.9
Simplified6.2
[Start]18.9 | \[ \frac{x \cdot \left(\left(y - z\right) + 1\right)}{z}
\] |
|---|---|
associate-*r/ [<=]0.1 | \[ \color{blue}{x \cdot \frac{\left(y - z\right) + 1}{z}}
\] |
+-commutative [=>]0.1 | \[ x \cdot \frac{\color{blue}{1 + \left(y - z\right)}}{z}
\] |
associate-+r- [=>]0.1 | \[ x \cdot \frac{\color{blue}{\left(1 + y\right) - z}}{z}
\] |
div-sub [=>]0.1 | \[ x \cdot \color{blue}{\left(\frac{1 + y}{z} - \frac{z}{z}\right)}
\] |
*-inverses [=>]0.1 | \[ x \cdot \left(\frac{1 + y}{z} - \color{blue}{1}\right)
\] |
distribute-rgt-out-- [<=]0.1 | \[ \color{blue}{\frac{1 + y}{z} \cdot x - 1 \cdot x}
\] |
*-lft-identity [=>]0.1 | \[ \frac{1 + y}{z} \cdot x - \color{blue}{x}
\] |
*-commutative [=>]0.1 | \[ \color{blue}{x \cdot \frac{1 + y}{z}} - x
\] |
associate-*r/ [=>]6.2 | \[ \color{blue}{\frac{x \cdot \left(1 + y\right)}{z}} - x
\] |
*-commutative [=>]6.2 | \[ \frac{\color{blue}{\left(1 + y\right) \cdot x}}{z} - x
\] |
+-commutative [=>]6.2 | \[ \frac{\color{blue}{\left(y + 1\right)} \cdot x}{z} - x
\] |
distribute-lft1-in [<=]6.2 | \[ \frac{\color{blue}{y \cdot x + x}}{z} - x
\] |
*-commutative [=>]6.2 | \[ \frac{\color{blue}{x \cdot y} + x}{z} - x
\] |
fma-def [=>]6.2 | \[ \frac{\color{blue}{\mathsf{fma}\left(x, y, x\right)}}{z} - x
\] |
Taylor expanded in y around inf 6.2
Simplified3.5
[Start]6.2 | \[ \frac{y \cdot x}{z} - x
\] |
|---|---|
associate-/l* [=>]3.5 | \[ \color{blue}{\frac{y}{\frac{z}{x}}} - x
\] |
Taylor expanded in y around 0 6.2
Simplified0.1
[Start]6.2 | \[ \frac{y \cdot x}{z} + -1 \cdot x
\] |
|---|---|
associate-*r/ [<=]3.3 | \[ \color{blue}{y \cdot \frac{x}{z}} + -1 \cdot x
\] |
mul-1-neg [=>]3.3 | \[ y \cdot \frac{x}{z} + \color{blue}{\left(-x\right)}
\] |
sub-neg [<=]3.3 | \[ \color{blue}{y \cdot \frac{x}{z} - x}
\] |
associate-*r/ [=>]6.2 | \[ \color{blue}{\frac{y \cdot x}{z}} - x
\] |
*-commutative [=>]6.2 | \[ \frac{\color{blue}{x \cdot y}}{z} - x
\] |
associate-/l* [=>]0.1 | \[ \color{blue}{\frac{x}{\frac{z}{y}}} - x
\] |
Final simplification0.1
| Alternative 1 | |
|---|---|
| Error | 19.7 |
| Cost | 1112 |
| Alternative 2 | |
|---|---|
| Error | 0.2 |
| Cost | 841 |
| Alternative 3 | |
|---|---|
| Error | 0.5 |
| Cost | 840 |
| Alternative 4 | |
|---|---|
| Error | 0.1 |
| Cost | 840 |
| Alternative 5 | |
|---|---|
| Error | 3.8 |
| Cost | 713 |
| Alternative 6 | |
|---|---|
| Error | 2.7 |
| Cost | 713 |
| Alternative 7 | |
|---|---|
| Error | 1.1 |
| Cost | 713 |
| Alternative 8 | |
|---|---|
| Error | 1.1 |
| Cost | 713 |
| Alternative 9 | |
|---|---|
| Error | 11.6 |
| Cost | 585 |
| Alternative 10 | |
|---|---|
| Error | 11.6 |
| Cost | 584 |
| Alternative 11 | |
|---|---|
| Error | 19.8 |
| Cost | 456 |
| Alternative 12 | |
|---|---|
| Error | 33.3 |
| Cost | 128 |
herbie shell --seed 2022364
(FPCore (x y z)
:name "Diagrams.TwoD.Segment.Bernstein:evaluateBernstein from diagrams-lib-1.3.0.3"
:precision binary64
:herbie-target
(if (< x -2.71483106713436e-162) (- (* (+ 1.0 y) (/ x z)) x) (if (< x 3.874108816439546e-197) (* (* x (+ (- y z) 1.0)) (/ 1.0 z)) (- (* (+ 1.0 y) (/ x z)) x)))
(/ (* x (+ (- y z) 1.0)) z))