| Alternative 1 | |
|---|---|
| Error | 12.7 |
| Cost | 260 |
\[\begin{array}{l}
\mathbf{if}\;x \leq -135000000:\\
\;\;\;\;-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 x) 5e-99)
z
(if (<= (* x x) 5e+22)
t_0
(if (<= (* x x) 1e+32)
z
(if (<= (* x x) 5e+115)
t_0
(if (<= (* x x) 5e+122)
z
(if (<= (* x x) 2e+158)
(- x)
(if (<= (* x x) 1e+211) t_0 (- 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) {
double t_0 = sqrt(((x * x) + ((y * y) + (z * z))));
double tmp;
if ((x * x) <= 5e-99) {
tmp = z;
} else if ((x * x) <= 5e+22) {
tmp = t_0;
} else if ((x * x) <= 1e+32) {
tmp = z;
} else if ((x * x) <= 5e+115) {
tmp = t_0;
} else if ((x * x) <= 5e+122) {
tmp = z;
} else if ((x * x) <= 2e+158) {
tmp = -x;
} else if ((x * x) <= 1e+211) {
tmp = t_0;
} else {
tmp = -x;
}
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 * x) <= 5d-99) then
tmp = z
else if ((x * x) <= 5d+22) then
tmp = t_0
else if ((x * x) <= 1d+32) then
tmp = z
else if ((x * x) <= 5d+115) then
tmp = t_0
else if ((x * x) <= 5d+122) then
tmp = z
else if ((x * x) <= 2d+158) then
tmp = -x
else if ((x * x) <= 1d+211) then
tmp = t_0
else
tmp = -x
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 * x) <= 5e-99) {
tmp = z;
} else if ((x * x) <= 5e+22) {
tmp = t_0;
} else if ((x * x) <= 1e+32) {
tmp = z;
} else if ((x * x) <= 5e+115) {
tmp = t_0;
} else if ((x * x) <= 5e+122) {
tmp = z;
} else if ((x * x) <= 2e+158) {
tmp = -x;
} else if ((x * x) <= 1e+211) {
tmp = t_0;
} else {
tmp = -x;
}
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 * x) <= 5e-99: tmp = z elif (x * x) <= 5e+22: tmp = t_0 elif (x * x) <= 1e+32: tmp = z elif (x * x) <= 5e+115: tmp = t_0 elif (x * x) <= 5e+122: tmp = z elif (x * x) <= 2e+158: tmp = -x elif (x * x) <= 1e+211: tmp = t_0 else: tmp = -x 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(x * x) <= 5e-99) tmp = z; elseif (Float64(x * x) <= 5e+22) tmp = t_0; elseif (Float64(x * x) <= 1e+32) tmp = z; elseif (Float64(x * x) <= 5e+115) tmp = t_0; elseif (Float64(x * x) <= 5e+122) tmp = z; elseif (Float64(x * x) <= 2e+158) tmp = Float64(-x); elseif (Float64(x * x) <= 1e+211) tmp = t_0; else tmp = Float64(-x); 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 * x) <= 5e-99) tmp = z; elseif ((x * x) <= 5e+22) tmp = t_0; elseif ((x * x) <= 1e+32) tmp = z; elseif ((x * x) <= 5e+115) tmp = t_0; elseif ((x * x) <= 5e+122) tmp = z; elseif ((x * x) <= 2e+158) tmp = -x; elseif ((x * x) <= 1e+211) tmp = t_0; else tmp = -x; 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[(x * x), $MachinePrecision], 5e-99], z, If[LessEqual[N[(x * x), $MachinePrecision], 5e+22], t$95$0, If[LessEqual[N[(x * x), $MachinePrecision], 1e+32], z, If[LessEqual[N[(x * x), $MachinePrecision], 5e+115], t$95$0, If[LessEqual[N[(x * x), $MachinePrecision], 5e+122], z, If[LessEqual[N[(x * x), $MachinePrecision], 2e+158], (-x), If[LessEqual[N[(x * x), $MachinePrecision], 1e+211], t$95$0, (-x)]]]]]]]]
\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 \cdot x \leq 5 \cdot 10^{-99}:\\
\;\;\;\;z\\
\mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+22}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;x \cdot x \leq 10^{+32}:\\
\;\;\;\;z\\
\mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+115}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+122}:\\
\;\;\;\;z\\
\mathbf{elif}\;x \cdot x \leq 2 \cdot 10^{+158}:\\
\;\;\;\;-x\\
\mathbf{elif}\;x \cdot x \leq 10^{+211}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;-x\\
\end{array}
Results
| Original | 38.1 |
|---|---|
| Target | 0 |
| Herbie | 12.4 |
if (*.f64 x x) < 4.99999999999999969e-99 or 4.9999999999999996e22 < (*.f64 x x) < 1.00000000000000005e32 or 5.00000000000000008e115 < (*.f64 x x) < 4.99999999999999989e122Initial program 28.7
Simplified28.7
[Start]28.7 | \[ \sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\] |
|---|---|
rational.json-simplify-41 [<=]28.7 | \[ \sqrt{\color{blue}{z \cdot z + \left(x \cdot x + y \cdot y\right)}}
\] |
Taylor expanded in z around inf 7.8
if 4.99999999999999969e-99 < (*.f64 x x) < 4.9999999999999996e22 or 1.00000000000000005e32 < (*.f64 x x) < 5.00000000000000008e115 or 1.99999999999999991e158 < (*.f64 x x) < 9.9999999999999996e210Initial program 21.3
if 4.99999999999999989e122 < (*.f64 x x) < 1.99999999999999991e158 or 9.9999999999999996e210 < (*.f64 x x) Initial program 52.5
Simplified52.5
[Start]52.5 | \[ \sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\] |
|---|---|
rational.json-simplify-41 [<=]52.5 | \[ \sqrt{\color{blue}{z \cdot z + \left(x \cdot x + y \cdot y\right)}}
\] |
Taylor expanded in x around -inf 11.8
Simplified11.8
[Start]11.8 | \[ -1 \cdot x
\] |
|---|---|
rational.json-simplify-2 [=>]11.8 | \[ \color{blue}{x \cdot -1}
\] |
rational.json-simplify-9 [=>]11.8 | \[ \color{blue}{-x}
\] |
Final simplification12.4
| Alternative 1 | |
|---|---|
| Error | 12.7 |
| Cost | 260 |
| Alternative 2 | |
|---|---|
| Error | 31.6 |
| Cost | 64 |
herbie shell --seed 2023063
(FPCore (x y z)
:name "bug366 (missed optimization)"
:precision binary64
:herbie-target
(hypot x (hypot y z))
(sqrt (+ (* x x) (+ (* y y) (* z z)))))