
(FPCore (x y z) :precision binary64 (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0)))
double code(double x, double y, double z) {
return sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
}
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)) / 3.0d0))
end function
public static double code(double x, double y, double z) {
return Math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
}
def code(x, y, z): return math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0))
function code(x, y, z) return sqrt(Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(z * z)) / 3.0)) end
function tmp = code(x, y, z) tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0)); end
code[x_, y_, z_] := N[Sqrt[N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] / 3.0), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0)))
double code(double x, double y, double z) {
return sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
}
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)) / 3.0d0))
end function
public static double code(double x, double y, double z) {
return Math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
}
def code(x, y, z): return math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0))
function code(x, y, z) return sqrt(Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(z * z)) / 3.0)) end
function tmp = code(x, y, z) tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0)); end
code[x_, y_, z_] := N[Sqrt[N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] / 3.0), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}
\end{array}
(FPCore (x y z) :precision binary64 (/ (hypot x (hypot z y)) (sqrt 3.0)))
double code(double x, double y, double z) {
return hypot(x, hypot(z, y)) / sqrt(3.0);
}
public static double code(double x, double y, double z) {
return Math.hypot(x, Math.hypot(z, y)) / Math.sqrt(3.0);
}
def code(x, y, z): return math.hypot(x, math.hypot(z, y)) / math.sqrt(3.0)
function code(x, y, z) return Float64(hypot(x, hypot(z, y)) / sqrt(3.0)) end
function tmp = code(x, y, z) tmp = hypot(x, hypot(z, y)) / sqrt(3.0); end
code[x_, y_, z_] := N[(N[Sqrt[x ^ 2 + N[Sqrt[z ^ 2 + y ^ 2], $MachinePrecision] ^ 2], $MachinePrecision] / N[Sqrt[3.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{hypot}\left(x, \mathsf{hypot}\left(z, y\right)\right)}{\sqrt{3}}
\end{array}
Initial program 50.0%
sqrt-div49.9%
div-inv49.5%
associate-+l+49.5%
add-sqr-sqrt49.5%
hypot-def59.5%
hypot-def98.7%
Applied egg-rr98.7%
associate-*r/99.4%
*-rgt-identity99.4%
hypot-def59.9%
+-commutative59.9%
hypot-def99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (x y z)
:precision binary64
(if (<= x -5.7e+138)
(* (sqrt 0.3333333333333333) (hypot y x))
(if (<= x -5e-59)
(sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0))
(/ z (sqrt 3.0)))))
double code(double x, double y, double z) {
double tmp;
if (x <= -5.7e+138) {
tmp = sqrt(0.3333333333333333) * hypot(y, x);
} else if (x <= -5e-59) {
tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
} else {
tmp = z / sqrt(3.0);
}
return tmp;
}
public static double code(double x, double y, double z) {
double tmp;
if (x <= -5.7e+138) {
tmp = Math.sqrt(0.3333333333333333) * Math.hypot(y, x);
} else if (x <= -5e-59) {
tmp = Math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
} else {
tmp = z / Math.sqrt(3.0);
}
return tmp;
}
def code(x, y, z): tmp = 0 if x <= -5.7e+138: tmp = math.sqrt(0.3333333333333333) * math.hypot(y, x) elif x <= -5e-59: tmp = math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0)) else: tmp = z / math.sqrt(3.0) return tmp
function code(x, y, z) tmp = 0.0 if (x <= -5.7e+138) tmp = Float64(sqrt(0.3333333333333333) * hypot(y, x)); elseif (x <= -5e-59) tmp = sqrt(Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(z * z)) / 3.0)); else tmp = Float64(z / sqrt(3.0)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (x <= -5.7e+138) tmp = sqrt(0.3333333333333333) * hypot(y, x); elseif (x <= -5e-59) tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0)); else tmp = z / sqrt(3.0); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[x, -5.7e+138], N[(N[Sqrt[0.3333333333333333], $MachinePrecision] * N[Sqrt[y ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision], If[LessEqual[x, -5e-59], N[Sqrt[N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] / 3.0), $MachinePrecision]], $MachinePrecision], N[(z / N[Sqrt[3.0], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5.7 \cdot 10^{+138}:\\
\;\;\;\;\sqrt{0.3333333333333333} \cdot \mathsf{hypot}\left(y, x\right)\\
\mathbf{elif}\;x \leq -5 \cdot 10^{-59}:\\
\;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\
\mathbf{else}:\\
\;\;\;\;\frac{z}{\sqrt{3}}\\
\end{array}
\end{array}
if x < -5.69999999999999986e138Initial program 13.6%
Taylor expanded in z around 0 13.8%
*-commutative13.8%
unpow213.8%
unpow213.8%
hypot-def82.1%
Simplified82.1%
if -5.69999999999999986e138 < x < -5.0000000000000001e-59Initial program 72.6%
if -5.0000000000000001e-59 < x Initial program 49.5%
sqrt-div49.5%
div-inv49.0%
associate-+l+49.0%
add-sqr-sqrt49.0%
hypot-def56.1%
hypot-def98.6%
Applied egg-rr98.6%
associate-*r/99.5%
*-rgt-identity99.5%
hypot-def56.5%
+-commutative56.5%
hypot-def99.5%
Simplified99.5%
Taylor expanded in z around inf 23.6%
Final simplification41.4%
(FPCore (x y z) :precision binary64 (* (sqrt 0.3333333333333333) (hypot z x)))
double code(double x, double y, double z) {
return sqrt(0.3333333333333333) * hypot(z, x);
}
public static double code(double x, double y, double z) {
return Math.sqrt(0.3333333333333333) * Math.hypot(z, x);
}
def code(x, y, z): return math.sqrt(0.3333333333333333) * math.hypot(z, x)
function code(x, y, z) return Float64(sqrt(0.3333333333333333) * hypot(z, x)) end
function tmp = code(x, y, z) tmp = sqrt(0.3333333333333333) * hypot(z, x); end
code[x_, y_, z_] := N[(N[Sqrt[0.3333333333333333], $MachinePrecision] * N[Sqrt[z ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.3333333333333333} \cdot \mathsf{hypot}\left(z, x\right)
\end{array}
Initial program 50.0%
Taylor expanded in y around 0 37.1%
*-commutative37.1%
unpow237.1%
unpow237.1%
hypot-def71.1%
Simplified71.1%
Final simplification71.1%
(FPCore (x y z)
:precision binary64
(if (<= x -1.42e+141)
(* x (- (sqrt 0.3333333333333333)))
(if (<= x -2.5e-58)
(sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0))
(/ z (sqrt 3.0)))))
double code(double x, double y, double z) {
double tmp;
if (x <= -1.42e+141) {
tmp = x * -sqrt(0.3333333333333333);
} else if (x <= -2.5e-58) {
tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
} else {
tmp = z / sqrt(3.0);
}
return tmp;
}
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 (x <= (-1.42d+141)) then
tmp = x * -sqrt(0.3333333333333333d0)
else if (x <= (-2.5d-58)) then
tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0d0))
else
tmp = z / sqrt(3.0d0)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (x <= -1.42e+141) {
tmp = x * -Math.sqrt(0.3333333333333333);
} else if (x <= -2.5e-58) {
tmp = Math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0));
} else {
tmp = z / Math.sqrt(3.0);
}
return tmp;
}
def code(x, y, z): tmp = 0 if x <= -1.42e+141: tmp = x * -math.sqrt(0.3333333333333333) elif x <= -2.5e-58: tmp = math.sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0)) else: tmp = z / math.sqrt(3.0) return tmp
function code(x, y, z) tmp = 0.0 if (x <= -1.42e+141) tmp = Float64(x * Float64(-sqrt(0.3333333333333333))); elseif (x <= -2.5e-58) tmp = sqrt(Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(z * z)) / 3.0)); else tmp = Float64(z / sqrt(3.0)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (x <= -1.42e+141) tmp = x * -sqrt(0.3333333333333333); elseif (x <= -2.5e-58) tmp = sqrt(((((x * x) + (y * y)) + (z * z)) / 3.0)); else tmp = z / sqrt(3.0); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[x, -1.42e+141], N[(x * (-N[Sqrt[0.3333333333333333], $MachinePrecision])), $MachinePrecision], If[LessEqual[x, -2.5e-58], N[Sqrt[N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] / 3.0), $MachinePrecision]], $MachinePrecision], N[(z / N[Sqrt[3.0], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.42 \cdot 10^{+141}:\\
\;\;\;\;x \cdot \left(-\sqrt{0.3333333333333333}\right)\\
\mathbf{elif}\;x \leq -2.5 \cdot 10^{-58}:\\
\;\;\;\;\sqrt{\frac{\left(x \cdot x + y \cdot y\right) + z \cdot z}{3}}\\
\mathbf{else}:\\
\;\;\;\;\frac{z}{\sqrt{3}}\\
\end{array}
\end{array}
if x < -1.42000000000000005e141Initial program 13.6%
Taylor expanded in x around -inf 69.5%
mul-1-neg69.5%
distribute-rgt-neg-in69.5%
Simplified69.5%
if -1.42000000000000005e141 < x < -2.49999999999999989e-58Initial program 72.6%
if -2.49999999999999989e-58 < x Initial program 49.5%
sqrt-div49.5%
div-inv49.0%
associate-+l+49.0%
add-sqr-sqrt49.0%
hypot-def56.1%
hypot-def98.6%
Applied egg-rr98.6%
associate-*r/99.5%
*-rgt-identity99.5%
hypot-def56.5%
+-commutative56.5%
hypot-def99.5%
Simplified99.5%
Taylor expanded in z around inf 23.6%
Final simplification39.9%
(FPCore (x y z) :precision binary64 (if (<= x -0.00085) (* x (- (sqrt 0.3333333333333333))) (* z (sqrt 0.3333333333333333))))
double code(double x, double y, double z) {
double tmp;
if (x <= -0.00085) {
tmp = x * -sqrt(0.3333333333333333);
} else {
tmp = z * sqrt(0.3333333333333333);
}
return tmp;
}
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 (x <= (-0.00085d0)) then
tmp = x * -sqrt(0.3333333333333333d0)
else
tmp = z * sqrt(0.3333333333333333d0)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (x <= -0.00085) {
tmp = x * -Math.sqrt(0.3333333333333333);
} else {
tmp = z * Math.sqrt(0.3333333333333333);
}
return tmp;
}
def code(x, y, z): tmp = 0 if x <= -0.00085: tmp = x * -math.sqrt(0.3333333333333333) else: tmp = z * math.sqrt(0.3333333333333333) return tmp
function code(x, y, z) tmp = 0.0 if (x <= -0.00085) tmp = Float64(x * Float64(-sqrt(0.3333333333333333))); else tmp = Float64(z * sqrt(0.3333333333333333)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (x <= -0.00085) tmp = x * -sqrt(0.3333333333333333); else tmp = z * sqrt(0.3333333333333333); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[x, -0.00085], N[(x * (-N[Sqrt[0.3333333333333333], $MachinePrecision])), $MachinePrecision], N[(z * N[Sqrt[0.3333333333333333], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.00085:\\
\;\;\;\;x \cdot \left(-\sqrt{0.3333333333333333}\right)\\
\mathbf{else}:\\
\;\;\;\;z \cdot \sqrt{0.3333333333333333}\\
\end{array}
\end{array}
if x < -8.49999999999999953e-4Initial program 49.1%
Taylor expanded in x around -inf 65.1%
mul-1-neg65.1%
distribute-rgt-neg-in65.1%
Simplified65.1%
if -8.49999999999999953e-4 < x Initial program 50.4%
Taylor expanded in z around inf 25.5%
Final simplification36.5%
(FPCore (x y z) :precision binary64 (* z (sqrt 0.3333333333333333)))
double code(double x, double y, double z) {
return z * sqrt(0.3333333333333333);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = z * sqrt(0.3333333333333333d0)
end function
public static double code(double x, double y, double z) {
return z * Math.sqrt(0.3333333333333333);
}
def code(x, y, z): return z * math.sqrt(0.3333333333333333)
function code(x, y, z) return Float64(z * sqrt(0.3333333333333333)) end
function tmp = code(x, y, z) tmp = z * sqrt(0.3333333333333333); end
code[x_, y_, z_] := N[(z * N[Sqrt[0.3333333333333333], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
z \cdot \sqrt{0.3333333333333333}
\end{array}
Initial program 50.0%
Taylor expanded in z around inf 22.3%
Final simplification22.3%
(FPCore (x y z)
:precision binary64
(if (< z -6.396479394109776e+136)
(/ (- z) (sqrt 3.0))
(if (< z 7.320293694404182e+117)
(/ (sqrt (+ (+ (* z z) (* x x)) (* y y))) (sqrt 3.0))
(* (sqrt 0.3333333333333333) z))))
double code(double x, double y, double z) {
double tmp;
if (z < -6.396479394109776e+136) {
tmp = -z / sqrt(3.0);
} else if (z < 7.320293694404182e+117) {
tmp = sqrt((((z * z) + (x * x)) + (y * y))) / sqrt(3.0);
} else {
tmp = sqrt(0.3333333333333333) * 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
real(8) :: tmp
if (z < (-6.396479394109776d+136)) then
tmp = -z / sqrt(3.0d0)
else if (z < 7.320293694404182d+117) then
tmp = sqrt((((z * z) + (x * x)) + (y * y))) / sqrt(3.0d0)
else
tmp = sqrt(0.3333333333333333d0) * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z < -6.396479394109776e+136) {
tmp = -z / Math.sqrt(3.0);
} else if (z < 7.320293694404182e+117) {
tmp = Math.sqrt((((z * z) + (x * x)) + (y * y))) / Math.sqrt(3.0);
} else {
tmp = Math.sqrt(0.3333333333333333) * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z < -6.396479394109776e+136: tmp = -z / math.sqrt(3.0) elif z < 7.320293694404182e+117: tmp = math.sqrt((((z * z) + (x * x)) + (y * y))) / math.sqrt(3.0) else: tmp = math.sqrt(0.3333333333333333) * z return tmp
function code(x, y, z) tmp = 0.0 if (z < -6.396479394109776e+136) tmp = Float64(Float64(-z) / sqrt(3.0)); elseif (z < 7.320293694404182e+117) tmp = Float64(sqrt(Float64(Float64(Float64(z * z) + Float64(x * x)) + Float64(y * y))) / sqrt(3.0)); else tmp = Float64(sqrt(0.3333333333333333) * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z < -6.396479394109776e+136) tmp = -z / sqrt(3.0); elseif (z < 7.320293694404182e+117) tmp = sqrt((((z * z) + (x * x)) + (y * y))) / sqrt(3.0); else tmp = sqrt(0.3333333333333333) * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Less[z, -6.396479394109776e+136], N[((-z) / N[Sqrt[3.0], $MachinePrecision]), $MachinePrecision], If[Less[z, 7.320293694404182e+117], N[(N[Sqrt[N[(N[(N[(z * z), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / N[Sqrt[3.0], $MachinePrecision]), $MachinePrecision], N[(N[Sqrt[0.3333333333333333], $MachinePrecision] * z), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z < -6.396479394109776 \cdot 10^{+136}:\\
\;\;\;\;\frac{-z}{\sqrt{3}}\\
\mathbf{elif}\;z < 7.320293694404182 \cdot 10^{+117}:\\
\;\;\;\;\frac{\sqrt{\left(z \cdot z + x \cdot x\right) + y \cdot y}}{\sqrt{3}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.3333333333333333} \cdot z\\
\end{array}
\end{array}
herbie shell --seed 2023185
(FPCore (x y z)
:name "Data.Array.Repa.Algorithms.Pixel:doubleRmsOfRGB8 from repa-algorithms-3.4.0.1"
:precision binary64
:herbie-target
(if (< z -6.396479394109776e+136) (/ (- z) (sqrt 3.0)) (if (< z 7.320293694404182e+117) (/ (sqrt (+ (+ (* z z) (* x x)) (* y y))) (sqrt 3.0)) (* (sqrt 0.3333333333333333) z)))
(sqrt (/ (+ (+ (* x x) (* y y)) (* z z)) 3.0)))