| Alternative 1 | |
|---|---|
| Error | 12.8 |
| Cost | 6660 |
\[\begin{array}{l}
\mathbf{if}\;x \leq -2.6 \cdot 10^{-9}:\\
\;\;\;\;\mathsf{hypot}\left(y, x\right)\\
\mathbf{else}:\\
\;\;\;\;z\\
\end{array}
\]
(FPCore (x y z) :precision binary64 (sqrt (+ (* x x) (+ (* y y) (* z z)))))
(FPCore (x y z) :precision binary64 (if (<= z 6.5e-42) (hypot y x) (if (<= z 1.56e+97) (sqrt (+ (* x x) (+ (* y y) (* z z)))) (hypot z y))))
double code(double x, double y, double z) {
return sqrt(((x * x) + ((y * y) + (z * z))));
}
double code(double x, double y, double z) {
double tmp;
if (z <= 6.5e-42) {
tmp = hypot(y, x);
} else if (z <= 1.56e+97) {
tmp = sqrt(((x * x) + ((y * y) + (z * z))));
} else {
tmp = hypot(z, y);
}
return tmp;
}
public static double code(double x, double y, double z) {
return Math.sqrt(((x * x) + ((y * y) + (z * z))));
}
public static double code(double x, double y, double z) {
double tmp;
if (z <= 6.5e-42) {
tmp = Math.hypot(y, x);
} else if (z <= 1.56e+97) {
tmp = Math.sqrt(((x * x) + ((y * y) + (z * z))));
} else {
tmp = Math.hypot(z, y);
}
return tmp;
}
def code(x, y, z): return math.sqrt(((x * x) + ((y * y) + (z * z))))
def code(x, y, z): tmp = 0 if z <= 6.5e-42: tmp = math.hypot(y, x) elif z <= 1.56e+97: tmp = math.sqrt(((x * x) + ((y * y) + (z * z)))) else: tmp = math.hypot(z, y) return tmp
function code(x, y, z) return sqrt(Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(z * z)))) end
function code(x, y, z) tmp = 0.0 if (z <= 6.5e-42) tmp = hypot(y, x); elseif (z <= 1.56e+97) tmp = sqrt(Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(z * z)))); else tmp = hypot(z, y); end return tmp end
function tmp = code(x, y, z) tmp = sqrt(((x * x) + ((y * y) + (z * z)))); end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= 6.5e-42) tmp = hypot(y, x); elseif (z <= 1.56e+97) tmp = sqrt(((x * x) + ((y * y) + (z * z)))); else tmp = hypot(z, y); end tmp_2 = tmp; end
code[x_, y_, z_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[x_, y_, z_] := If[LessEqual[z, 6.5e-42], N[Sqrt[y ^ 2 + x ^ 2], $MachinePrecision], If[LessEqual[z, 1.56e+97], N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Sqrt[z ^ 2 + y ^ 2], $MachinePrecision]]]
\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\begin{array}{l}
\mathbf{if}\;z \leq 6.5 \cdot 10^{-42}:\\
\;\;\;\;\mathsf{hypot}\left(y, x\right)\\
\mathbf{elif}\;z \leq 1.56 \cdot 10^{+97}:\\
\;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{hypot}\left(z, y\right)\\
\end{array}
Results
| Original | 38.8 |
|---|---|
| Target | 0.0 |
| Herbie | 10.9 |
if z < 6.4999999999999998e-42Initial program 31.6
Taylor expanded in z around 0 37.1
Simplified6.7
[Start]37.1 | \[ \sqrt{{y}^{2} + {x}^{2}}
\] |
|---|---|
unpow2 [=>]37.1 | \[ \sqrt{\color{blue}{y \cdot y} + {x}^{2}}
\] |
unpow2 [=>]37.1 | \[ \sqrt{y \cdot y + \color{blue}{x \cdot x}}
\] |
hypot-def [=>]6.7 | \[ \color{blue}{\mathsf{hypot}\left(y, x\right)}
\] |
if 6.4999999999999998e-42 < z < 1.56e97Initial program 19.7
if 1.56e97 < z Initial program 53.0
Taylor expanded in x around 0 53.4
Simplified10.6
[Start]53.4 | \[ \sqrt{{z}^{2} + {y}^{2}}
\] |
|---|---|
unpow2 [=>]53.4 | \[ \sqrt{\color{blue}{z \cdot z} + {y}^{2}}
\] |
unpow2 [=>]53.4 | \[ \sqrt{z \cdot z + \color{blue}{y \cdot y}}
\] |
hypot-def [=>]10.6 | \[ \color{blue}{\mathsf{hypot}\left(z, y\right)}
\] |
Final simplification10.9
| Alternative 1 | |
|---|---|
| Error | 12.8 |
| Cost | 6660 |
| Alternative 2 | |
|---|---|
| Error | 12.5 |
| Cost | 6660 |
| Alternative 3 | |
|---|---|
| Error | 13.0 |
| Cost | 260 |
| Alternative 4 | |
|---|---|
| Error | 31.2 |
| Cost | 64 |
herbie shell --seed 2023002
(FPCore (x y z)
:name "bug366 (missed optimization)"
:precision binary64
:herbie-target
(hypot x (hypot y z))
(sqrt (+ (* x x) (+ (* y y) (* z z)))))