| Alternative 1 | |
|---|---|
| Error | 12.9 |
| Cost | 260 |
\[\begin{array}{l}
\mathbf{if}\;x \leq -15500000000:\\
\;\;\;\;-x\\
\mathbf{else}:\\
\;\;\;\;z\\
\end{array}
\]
(FPCore (x y z) :precision binary64 (sqrt (+ (* x x) (+ (* y y) (* z z)))))
(FPCore (x y z)
:precision binary64
(let* ((t_0 (sqrt (+ (* x x) (+ (* y y) (* z z))))))
(if (<= x -7.2e+146)
(- x)
(if (<= x -132000000.0)
t_0
(if (<= x -8.6e-8) z (if (<= x -3.8e-65) t_0 z))))))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 t_0 = sqrt(((x * x) + ((y * y) + (z * z))));
double tmp;
if (x <= -7.2e+146) {
tmp = -x;
} else if (x <= -132000000.0) {
tmp = t_0;
} else if (x <= -8.6e-8) {
tmp = z;
} else if (x <= -3.8e-65) {
tmp = t_0;
} else {
tmp = z;
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = sqrt(((x * x) + ((y * y) + (z * z))))
end function
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt(((x * x) + ((y * y) + (z * z))))
if (x <= (-7.2d+146)) then
tmp = -x
else if (x <= (-132000000.0d0)) then
tmp = t_0
else if (x <= (-8.6d-8)) then
tmp = z
else if (x <= (-3.8d-65)) then
tmp = t_0
else
tmp = z
end if
code = tmp
end function
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 t_0 = Math.sqrt(((x * x) + ((y * y) + (z * z))));
double tmp;
if (x <= -7.2e+146) {
tmp = -x;
} else if (x <= -132000000.0) {
tmp = t_0;
} else if (x <= -8.6e-8) {
tmp = z;
} else if (x <= -3.8e-65) {
tmp = t_0;
} else {
tmp = z;
}
return tmp;
}
def code(x, y, z): return math.sqrt(((x * x) + ((y * y) + (z * z))))
def code(x, y, z): t_0 = math.sqrt(((x * x) + ((y * y) + (z * z)))) tmp = 0 if x <= -7.2e+146: tmp = -x elif x <= -132000000.0: tmp = t_0 elif x <= -8.6e-8: tmp = z elif x <= -3.8e-65: tmp = t_0 else: tmp = z 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) t_0 = sqrt(Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(z * z)))) tmp = 0.0 if (x <= -7.2e+146) tmp = Float64(-x); elseif (x <= -132000000.0) tmp = t_0; elseif (x <= -8.6e-8) tmp = z; elseif (x <= -3.8e-65) tmp = t_0; else tmp = z; 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) t_0 = sqrt(((x * x) + ((y * y) + (z * z)))); tmp = 0.0; if (x <= -7.2e+146) tmp = -x; elseif (x <= -132000000.0) tmp = t_0; elseif (x <= -8.6e-8) tmp = z; elseif (x <= -3.8e-65) tmp = t_0; else tmp = z; 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_] := Block[{t$95$0 = N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[x, -7.2e+146], (-x), If[LessEqual[x, -132000000.0], t$95$0, If[LessEqual[x, -8.6e-8], z, If[LessEqual[x, -3.8e-65], t$95$0, z]]]]]
\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\begin{array}{l}
t_0 := \sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\
\mathbf{if}\;x \leq -7.2 \cdot 10^{+146}:\\
\;\;\;\;-x\\
\mathbf{elif}\;x \leq -132000000:\\
\;\;\;\;t_0\\
\mathbf{elif}\;x \leq -8.6 \cdot 10^{-8}:\\
\;\;\;\;z\\
\mathbf{elif}\;x \leq -3.8 \cdot 10^{-65}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;z\\
\end{array}
Results
| Original | 38.3 |
|---|---|
| Target | 0.0 |
| Herbie | 11.3 |
if x < -7.1999999999999997e146Initial program 62.0
Simplified62.0
[Start]62.0 | \[ \sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\] |
|---|---|
rational.json-simplify-41 [<=]62.0 | \[ \sqrt{\color{blue}{z \cdot z + \left(x \cdot x + y \cdot y\right)}}
\] |
Taylor expanded in x around -inf 8.0
Simplified8.0
[Start]8.0 | \[ -1 \cdot x
\] |
|---|---|
rational.json-simplify-2 [=>]8.0 | \[ \color{blue}{x \cdot -1}
\] |
rational.json-simplify-9 [=>]8.0 | \[ \color{blue}{-x}
\] |
if -7.1999999999999997e146 < x < -1.32e8 or -8.6000000000000002e-8 < x < -3.8000000000000002e-65Initial program 19.9
if -1.32e8 < x < -8.6000000000000002e-8 or -3.8000000000000002e-65 < x Initial program 31.1
Simplified31.1
[Start]31.1 | \[ \sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\] |
|---|---|
rational.json-simplify-41 [<=]31.1 | \[ \sqrt{\color{blue}{z \cdot z + \left(x \cdot x + y \cdot y\right)}}
\] |
Taylor expanded in z around inf 7.1
Final simplification11.3
| Alternative 1 | |
|---|---|
| Error | 12.9 |
| Cost | 260 |
| Alternative 2 | |
|---|---|
| Error | 31.0 |
| Cost | 64 |
herbie shell --seed 2023074
(FPCore (x y z)
:name "bug366 (missed optimization)"
:precision binary64
:herbie-target
(hypot x (hypot y z))
(sqrt (+ (* x x) (+ (* y y) (* z z)))))