| Alternative 1 | |
|---|---|
| Error | 13.5 |
| Cost | 324 |
\[\begin{array}{l}
\mathbf{if}\;z \leq 5.5 \cdot 10^{-54}:\\
\;\;\;\;x \cdot -1\\
\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 (<= (* z z) 4e-110)
(* x -1.0)
(if (<= (* z z) 4e+28)
t_0
(if (<= (* z z) 2e+42) (* x -1.0) (if (<= (* z z) 5e+153) 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 ((z * z) <= 4e-110) {
tmp = x * -1.0;
} else if ((z * z) <= 4e+28) {
tmp = t_0;
} else if ((z * z) <= 2e+42) {
tmp = x * -1.0;
} else if ((z * z) <= 5e+153) {
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 ((z * z) <= 4d-110) then
tmp = x * (-1.0d0)
else if ((z * z) <= 4d+28) then
tmp = t_0
else if ((z * z) <= 2d+42) then
tmp = x * (-1.0d0)
else if ((z * z) <= 5d+153) 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 ((z * z) <= 4e-110) {
tmp = x * -1.0;
} else if ((z * z) <= 4e+28) {
tmp = t_0;
} else if ((z * z) <= 2e+42) {
tmp = x * -1.0;
} else if ((z * z) <= 5e+153) {
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 (z * z) <= 4e-110: tmp = x * -1.0 elif (z * z) <= 4e+28: tmp = t_0 elif (z * z) <= 2e+42: tmp = x * -1.0 elif (z * z) <= 5e+153: 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 (Float64(z * z) <= 4e-110) tmp = Float64(x * -1.0); elseif (Float64(z * z) <= 4e+28) tmp = t_0; elseif (Float64(z * z) <= 2e+42) tmp = Float64(x * -1.0); elseif (Float64(z * z) <= 5e+153) 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 ((z * z) <= 4e-110) tmp = x * -1.0; elseif ((z * z) <= 4e+28) tmp = t_0; elseif ((z * z) <= 2e+42) tmp = x * -1.0; elseif ((z * z) <= 5e+153) 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[N[(z * z), $MachinePrecision], 4e-110], N[(x * -1.0), $MachinePrecision], If[LessEqual[N[(z * z), $MachinePrecision], 4e+28], t$95$0, If[LessEqual[N[(z * z), $MachinePrecision], 2e+42], N[(x * -1.0), $MachinePrecision], If[LessEqual[N[(z * z), $MachinePrecision], 5e+153], 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}\;z \cdot z \leq 4 \cdot 10^{-110}:\\
\;\;\;\;x \cdot -1\\
\mathbf{elif}\;z \cdot z \leq 4 \cdot 10^{+28}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;z \cdot z \leq 2 \cdot 10^{+42}:\\
\;\;\;\;x \cdot -1\\
\mathbf{elif}\;z \cdot z \leq 5 \cdot 10^{+153}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;z\\
\end{array}
Results
| Original | 38.1 |
|---|---|
| Target | 0.0 |
| Herbie | 12.6 |
if (*.f64 z z) < 4.0000000000000002e-110 or 3.99999999999999983e28 < (*.f64 z z) < 2.00000000000000009e42Initial program 27.8
Taylor expanded in x around -inf 7.8
Simplified7.8
[Start]7.8 | \[ -1 \cdot x
\] |
|---|---|
rational.json-simplify-2 [=>]7.8 | \[ \color{blue}{x \cdot -1}
\] |
if 4.0000000000000002e-110 < (*.f64 z z) < 3.99999999999999983e28 or 2.00000000000000009e42 < (*.f64 z z) < 5.00000000000000018e153Initial program 22.6
if 5.00000000000000018e153 < (*.f64 z z) Initial program 51.2
Taylor expanded in z around inf 12.1
Final simplification12.6
| Alternative 1 | |
|---|---|
| Error | 13.5 |
| Cost | 324 |
| Alternative 2 | |
|---|---|
| Error | 30.8 |
| Cost | 64 |
herbie shell --seed 2023053
(FPCore (x y z)
:name "bug366 (missed optimization)"
:precision binary64
:herbie-target
(hypot x (hypot y z))
(sqrt (+ (* x x) (+ (* y y) (* z z)))))