| Alternative 1 | |
|---|---|
| Accuracy | 80.4% |
| Cost | 6660 |
\[\begin{array}{l}
\mathbf{if}\;z \leq 2.4 \cdot 10^{+22}:\\
\;\;\;\;\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 (hypot z x))
double code(double x, double y, double z) {
return sqrt((((x * x) + (y * y)) + (z * z)));
}
double code(double x, double y, double z) {
return hypot(z, x);
}
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) {
return Math.hypot(z, x);
}
def code(x, y, z): return math.sqrt((((x * x) + (y * y)) + (z * z)))
def code(x, y, z): return math.hypot(z, x)
function code(x, y, z) return sqrt(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(z * z))) end
function code(x, y, z) return hypot(z, x) end
function tmp = code(x, y, z) tmp = sqrt((((x * x) + (y * y)) + (z * z))); end
function tmp = code(x, y, z) tmp = hypot(z, x); end
code[x_, y_, z_] := N[Sqrt[N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[x_, y_, z_] := N[Sqrt[z ^ 2 + x ^ 2], $MachinePrecision]
\sqrt{\left(x \cdot x + y \cdot y\right) + z \cdot z}
\mathsf{hypot}\left(z, x\right)
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
| Original | 44.4% |
|---|---|
| Target | 71.6% |
| Herbie | 99.2% |
Initial program 49.5%
Taylor expanded in y around 0 31.8%
Simplified62.9%
[Start]31.8 | \[ \sqrt{{z}^{2} + {x}^{2}}
\] |
|---|---|
unpow2 [=>]31.8 | \[ \sqrt{\color{blue}{z \cdot z} + {x}^{2}}
\] |
unpow2 [=>]31.8 | \[ \sqrt{z \cdot z + \color{blue}{x \cdot x}}
\] |
hypot-def [=>]62.9 | \[ \color{blue}{\mathsf{hypot}\left(z, x\right)}
\] |
Final simplification62.9%
| Alternative 1 | |
|---|---|
| Accuracy | 80.4% |
| Cost | 6660 |
| Alternative 2 | |
|---|---|
| Accuracy | 79.9% |
| Cost | 260 |
| Alternative 3 | |
|---|---|
| Accuracy | 52.4% |
| Cost | 64 |
herbie shell --seed 2023163
(FPCore (x y z)
:name "FRP.Yampa.Vector3:vector3Rho from Yampa-0.10.2"
:precision binary64
:herbie-target
(if (< z -6.396479394109776e+136) (- z) (if (< z 7.320293694404182e+117) (sqrt (+ (+ (* z z) (* x x)) (* y y))) z))
(sqrt (+ (+ (* x x) (* y y)) (* z z))))