
(FPCore (x y z) :precision binary64 (sqrt (+ (* x x) (+ (* y y) (* z z)))))
double code(double x, double y, double z) {
return sqrt(((x * x) + ((y * y) + (z * z))));
}
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
public static double code(double x, double y, double z) {
return Math.sqrt(((x * x) + ((y * y) + (z * z))));
}
def code(x, y, z): return math.sqrt(((x * x) + ((y * y) + (z * z))))
function code(x, y, z) return sqrt(Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(z * z)))) end
function tmp = code(x, y, z) tmp = sqrt(((x * x) + ((y * y) + (z * z)))); end
code[x_, y_, z_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (sqrt (+ (* x x) (+ (* y y) (* z z)))))
double code(double x, double y, double z) {
return sqrt(((x * x) + ((y * y) + (z * z))));
}
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
public static double code(double x, double y, double z) {
return Math.sqrt(((x * x) + ((y * y) + (z * z))));
}
def code(x, y, z): return math.sqrt(((x * x) + ((y * y) + (z * z))))
function code(x, y, z) return sqrt(Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(z * z)))) end
function tmp = code(x, y, z) tmp = sqrt(((x * x) + ((y * y) + (z * z)))); end
code[x_, y_, z_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}
\end{array}
NOTE: x, y, and z should be sorted in increasing order before calling this function. (FPCore (x y z) :precision binary64 (if (<= z 1.85e-96) (hypot y x) (if (<= z 1.4e+148) (sqrt (+ (* x x) (+ (* y y) (* z z)))) (hypot z y))))
assert(x < y && y < z);
double code(double x, double y, double z) {
double tmp;
if (z <= 1.85e-96) {
tmp = hypot(y, x);
} else if (z <= 1.4e+148) {
tmp = sqrt(((x * x) + ((y * y) + (z * z))));
} else {
tmp = hypot(z, y);
}
return tmp;
}
assert x < y && y < z;
public static double code(double x, double y, double z) {
double tmp;
if (z <= 1.85e-96) {
tmp = Math.hypot(y, x);
} else if (z <= 1.4e+148) {
tmp = Math.sqrt(((x * x) + ((y * y) + (z * z))));
} else {
tmp = Math.hypot(z, y);
}
return tmp;
}
[x, y, z] = sort([x, y, z]) def code(x, y, z): tmp = 0 if z <= 1.85e-96: tmp = math.hypot(y, x) elif z <= 1.4e+148: tmp = math.sqrt(((x * x) + ((y * y) + (z * z)))) else: tmp = math.hypot(z, y) return tmp
x, y, z = sort([x, y, z]) function code(x, y, z) tmp = 0.0 if (z <= 1.85e-96) tmp = hypot(y, x); elseif (z <= 1.4e+148) tmp = sqrt(Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(z * z)))); else tmp = hypot(z, y); end return tmp end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp_2 = code(x, y, z)
tmp = 0.0;
if (z <= 1.85e-96)
tmp = hypot(y, x);
elseif (z <= 1.4e+148)
tmp = sqrt(((x * x) + ((y * y) + (z * z))));
else
tmp = hypot(z, y);
end
tmp_2 = tmp;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function. code[x_, y_, z_] := If[LessEqual[z, 1.85e-96], N[Sqrt[y ^ 2 + x ^ 2], $MachinePrecision], If[LessEqual[z, 1.4e+148], 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]]]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\begin{array}{l}
\mathbf{if}\;z \leq 1.85 \cdot 10^{-96}:\\
\;\;\;\;\mathsf{hypot}\left(y, x\right)\\
\mathbf{elif}\;z \leq 1.4 \cdot 10^{+148}:\\
\;\;\;\;\sqrt{x \cdot x + \left(y \cdot y + z \cdot z\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{hypot}\left(z, y\right)\\
\end{array}
\end{array}
if z < 1.84999999999999993e-96Initial program 41.9%
Taylor expanded in z around 0 36.5%
unpow236.5%
unpow236.5%
hypot-def77.5%
Simplified77.5%
if 1.84999999999999993e-96 < z < 1.3999999999999999e148Initial program 62.6%
if 1.3999999999999999e148 < z Initial program 10.2%
Taylor expanded in x around 0 7.4%
unpow27.4%
unpow27.4%
hypot-def88.2%
Simplified88.2%
Final simplification75.8%
NOTE: x, y, and z should be sorted in increasing order before calling this function. (FPCore (x y z) :precision binary64 (if (<= z 9.2e-23) (hypot y x) z))
assert(x < y && y < z);
double code(double x, double y, double z) {
double tmp;
if (z <= 9.2e-23) {
tmp = hypot(y, x);
} else {
tmp = z;
}
return tmp;
}
assert x < y && y < z;
public static double code(double x, double y, double z) {
double tmp;
if (z <= 9.2e-23) {
tmp = Math.hypot(y, x);
} else {
tmp = z;
}
return tmp;
}
[x, y, z] = sort([x, y, z]) def code(x, y, z): tmp = 0 if z <= 9.2e-23: tmp = math.hypot(y, x) else: tmp = z return tmp
x, y, z = sort([x, y, z]) function code(x, y, z) tmp = 0.0 if (z <= 9.2e-23) tmp = hypot(y, x); else tmp = z; end return tmp end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp_2 = code(x, y, z)
tmp = 0.0;
if (z <= 9.2e-23)
tmp = hypot(y, x);
else
tmp = z;
end
tmp_2 = tmp;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function. code[x_, y_, z_] := If[LessEqual[z, 9.2e-23], N[Sqrt[y ^ 2 + x ^ 2], $MachinePrecision], z]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\begin{array}{l}
\mathbf{if}\;z \leq 9.2 \cdot 10^{-23}:\\
\;\;\;\;\mathsf{hypot}\left(y, x\right)\\
\mathbf{else}:\\
\;\;\;\;z\\
\end{array}
\end{array}
if z < 9.2000000000000004e-23Initial program 45.0%
Taylor expanded in z around 0 38.9%
unpow238.9%
unpow238.9%
hypot-def78.9%
Simplified78.9%
if 9.2000000000000004e-23 < z Initial program 32.8%
Taylor expanded in z around inf 59.6%
Final simplification74.6%
NOTE: x, y, and z should be sorted in increasing order before calling this function. (FPCore (x y z) :precision binary64 (if (<= z 1.1e-22) (hypot y x) (hypot z y)))
assert(x < y && y < z);
double code(double x, double y, double z) {
double tmp;
if (z <= 1.1e-22) {
tmp = hypot(y, x);
} else {
tmp = hypot(z, y);
}
return tmp;
}
assert x < y && y < z;
public static double code(double x, double y, double z) {
double tmp;
if (z <= 1.1e-22) {
tmp = Math.hypot(y, x);
} else {
tmp = Math.hypot(z, y);
}
return tmp;
}
[x, y, z] = sort([x, y, z]) def code(x, y, z): tmp = 0 if z <= 1.1e-22: tmp = math.hypot(y, x) else: tmp = math.hypot(z, y) return tmp
x, y, z = sort([x, y, z]) function code(x, y, z) tmp = 0.0 if (z <= 1.1e-22) tmp = hypot(y, x); else tmp = hypot(z, y); end return tmp end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp_2 = code(x, y, z)
tmp = 0.0;
if (z <= 1.1e-22)
tmp = hypot(y, x);
else
tmp = hypot(z, y);
end
tmp_2 = tmp;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function. code[x_, y_, z_] := If[LessEqual[z, 1.1e-22], N[Sqrt[y ^ 2 + x ^ 2], $MachinePrecision], N[Sqrt[z ^ 2 + y ^ 2], $MachinePrecision]]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\begin{array}{l}
\mathbf{if}\;z \leq 1.1 \cdot 10^{-22}:\\
\;\;\;\;\mathsf{hypot}\left(y, x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{hypot}\left(z, y\right)\\
\end{array}
\end{array}
if z < 1.1e-22Initial program 45.0%
Taylor expanded in z around 0 38.9%
unpow238.9%
unpow238.9%
hypot-def78.9%
Simplified78.9%
if 1.1e-22 < z Initial program 32.8%
Taylor expanded in x around 0 24.8%
unpow224.8%
unpow224.8%
hypot-def80.8%
Simplified80.8%
Final simplification79.4%
NOTE: x, y, and z should be sorted in increasing order before calling this function. (FPCore (x y z) :precision binary64 (if (<= z 1.1e-22) (- x) z))
assert(x < y && y < z);
double code(double x, double y, double z) {
double tmp;
if (z <= 1.1e-22) {
tmp = -x;
} else {
tmp = z;
}
return tmp;
}
NOTE: x, y, and z should be sorted in increasing order before calling this 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) :: tmp
if (z <= 1.1d-22) then
tmp = -x
else
tmp = z
end if
code = tmp
end function
assert x < y && y < z;
public static double code(double x, double y, double z) {
double tmp;
if (z <= 1.1e-22) {
tmp = -x;
} else {
tmp = z;
}
return tmp;
}
[x, y, z] = sort([x, y, z]) def code(x, y, z): tmp = 0 if z <= 1.1e-22: tmp = -x else: tmp = z return tmp
x, y, z = sort([x, y, z]) function code(x, y, z) tmp = 0.0 if (z <= 1.1e-22) tmp = Float64(-x); else tmp = z; end return tmp end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp_2 = code(x, y, z)
tmp = 0.0;
if (z <= 1.1e-22)
tmp = -x;
else
tmp = z;
end
tmp_2 = tmp;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function. code[x_, y_, z_] := If[LessEqual[z, 1.1e-22], (-x), z]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\begin{array}{l}
\mathbf{if}\;z \leq 1.1 \cdot 10^{-22}:\\
\;\;\;\;-x\\
\mathbf{else}:\\
\;\;\;\;z\\
\end{array}
\end{array}
if z < 1.1e-22Initial program 45.0%
Taylor expanded in x around -inf 20.3%
mul-1-neg20.3%
Simplified20.3%
if 1.1e-22 < z Initial program 32.8%
Taylor expanded in z around inf 59.6%
Final simplification29.2%
NOTE: x, y, and z should be sorted in increasing order before calling this function. (FPCore (x y z) :precision binary64 z)
assert(x < y && y < z);
double code(double x, double y, double z) {
return z;
}
NOTE: x, y, and z should be sorted in increasing order before calling this function.
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = z
end function
assert x < y && y < z;
public static double code(double x, double y, double z) {
return z;
}
[x, y, z] = sort([x, y, z]) def code(x, y, z): return z
x, y, z = sort([x, y, z]) function code(x, y, z) return z end
x, y, z = num2cell(sort([x, y, z])){:}
function tmp = code(x, y, z)
tmp = z;
end
NOTE: x, y, and z should be sorted in increasing order before calling this function. code[x_, y_, z_] := z
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
z
\end{array}
Initial program 42.2%
Taylor expanded in z around inf 16.2%
Final simplification16.2%
(FPCore (x y z) :precision binary64 (hypot x (hypot y z)))
double code(double x, double y, double z) {
return hypot(x, hypot(y, z));
}
public static double code(double x, double y, double z) {
return Math.hypot(x, Math.hypot(y, z));
}
def code(x, y, z): return math.hypot(x, math.hypot(y, z))
function code(x, y, z) return hypot(x, hypot(y, z)) end
function tmp = code(x, y, z) tmp = hypot(x, hypot(y, z)); end
code[x_, y_, z_] := N[Sqrt[x ^ 2 + N[Sqrt[y ^ 2 + z ^ 2], $MachinePrecision] ^ 2], $MachinePrecision]
\begin{array}{l}
\\
\mathsf{hypot}\left(x, \mathsf{hypot}\left(y, z\right)\right)
\end{array}
herbie shell --seed 2023178
(FPCore (x y z)
:name "bug366 (missed optimization)"
:precision binary64
:herbie-target
(hypot x (hypot y z))
(sqrt (+ (* x x) (+ (* y y) (* z z)))))