
(FPCore (x y z) :precision binary64 (* 2.0 (sqrt (+ (+ (* x y) (* x z)) (* y z)))))
double code(double x, double y, double z) {
return 2.0 * sqrt((((x * y) + (x * z)) + (y * z)));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = 2.0d0 * sqrt((((x * y) + (x * z)) + (y * z)))
end function
public static double code(double x, double y, double z) {
return 2.0 * Math.sqrt((((x * y) + (x * z)) + (y * z)));
}
def code(x, y, z): return 2.0 * math.sqrt((((x * y) + (x * z)) + (y * z)))
function code(x, y, z) return Float64(2.0 * sqrt(Float64(Float64(Float64(x * y) + Float64(x * z)) + Float64(y * z)))) end
function tmp = code(x, y, z) tmp = 2.0 * sqrt((((x * y) + (x * z)) + (y * z))); end
code[x_, y_, z_] := N[(2.0 * N[Sqrt[N[(N[(N[(x * y), $MachinePrecision] + N[(x * z), $MachinePrecision]), $MachinePrecision] + N[(y * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \sqrt{\left(x \cdot y + x \cdot z\right) + y \cdot z}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (* 2.0 (sqrt (+ (+ (* x y) (* x z)) (* y z)))))
double code(double x, double y, double z) {
return 2.0 * sqrt((((x * y) + (x * z)) + (y * z)));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = 2.0d0 * sqrt((((x * y) + (x * z)) + (y * z)))
end function
public static double code(double x, double y, double z) {
return 2.0 * Math.sqrt((((x * y) + (x * z)) + (y * z)));
}
def code(x, y, z): return 2.0 * math.sqrt((((x * y) + (x * z)) + (y * z)))
function code(x, y, z) return Float64(2.0 * sqrt(Float64(Float64(Float64(x * y) + Float64(x * z)) + Float64(y * z)))) end
function tmp = code(x, y, z) tmp = 2.0 * sqrt((((x * y) + (x * z)) + (y * z))); end
code[x_, y_, z_] := N[(2.0 * N[Sqrt[N[(N[(N[(x * y), $MachinePrecision] + N[(x * z), $MachinePrecision]), $MachinePrecision] + N[(y * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \sqrt{\left(x \cdot y + x \cdot z\right) + y \cdot z}
\end{array}
NOTE: x, y, and z should be sorted in increasing order before calling this function.
(FPCore (x y z)
:precision binary64
(if (<= y -4e+38)
(* 2.0 (exp (* (+ (log (- (- z) y)) (log (- x))) 0.5)))
(if (<= y 380000.0)
(* 2.0 (sqrt (+ (+ (* y x) (* z x)) (* y z))))
(*
z
(fma
x
(* y (sqrt (/ 1.0 (* (* z (* z z)) (+ y x)))))
(* 2.0 (sqrt (/ (+ y x) z))))))))assert(x < y && y < z);
double code(double x, double y, double z) {
double tmp;
if (y <= -4e+38) {
tmp = 2.0 * exp(((log((-z - y)) + log(-x)) * 0.5));
} else if (y <= 380000.0) {
tmp = 2.0 * sqrt((((y * x) + (z * x)) + (y * z)));
} else {
tmp = z * fma(x, (y * sqrt((1.0 / ((z * (z * z)) * (y + x))))), (2.0 * sqrt(((y + x) / z))));
}
return tmp;
}
x, y, z = sort([x, y, z]) function code(x, y, z) tmp = 0.0 if (y <= -4e+38) tmp = Float64(2.0 * exp(Float64(Float64(log(Float64(Float64(-z) - y)) + log(Float64(-x))) * 0.5))); elseif (y <= 380000.0) tmp = Float64(2.0 * sqrt(Float64(Float64(Float64(y * x) + Float64(z * x)) + Float64(y * z)))); else tmp = Float64(z * fma(x, Float64(y * sqrt(Float64(1.0 / Float64(Float64(z * Float64(z * z)) * Float64(y + x))))), Float64(2.0 * sqrt(Float64(Float64(y + x) / z))))); end return tmp end
NOTE: x, y, and z should be sorted in increasing order before calling this function. code[x_, y_, z_] := If[LessEqual[y, -4e+38], N[(2.0 * N[Exp[N[(N[(N[Log[N[((-z) - y), $MachinePrecision]], $MachinePrecision] + N[Log[(-x)], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 380000.0], N[(2.0 * N[Sqrt[N[(N[(N[(y * x), $MachinePrecision] + N[(z * x), $MachinePrecision]), $MachinePrecision] + N[(y * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(z * N[(x * N[(y * N[Sqrt[N[(1.0 / N[(N[(z * N[(z * z), $MachinePrecision]), $MachinePrecision] * N[(y + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(2.0 * N[Sqrt[N[(N[(y + x), $MachinePrecision] / z), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq -4 \cdot 10^{+38}:\\
\;\;\;\;2 \cdot e^{\left(\log \left(\left(-z\right) - y\right) + \log \left(-x\right)\right) \cdot 0.5}\\
\mathbf{elif}\;y \leq 380000:\\
\;\;\;\;2 \cdot \sqrt{\left(y \cdot x + z \cdot x\right) + y \cdot z}\\
\mathbf{else}:\\
\;\;\;\;z \cdot \mathsf{fma}\left(x, y \cdot \sqrt{\frac{1}{\left(z \cdot \left(z \cdot z\right)\right) \cdot \left(y + x\right)}}, 2 \cdot \sqrt{\frac{y + x}{z}}\right)\\
\end{array}
\end{array}
if y < -3.99999999999999991e38Initial program 36.5%
Taylor expanded in z around inf
distribute-rgt-inN/A
+-commutativeN/A
associate-/l*N/A
associate-*l*N/A
distribute-lft-outN/A
lower-*.f64N/A
lower-fma.f64N/A
lower-/.f6431.7
Applied rewrites31.7%
lift-sqrt.f64N/A
pow1/2N/A
pow-to-expN/A
lower-exp.f64N/A
lower-*.f64N/A
Applied rewrites34.3%
Taylor expanded in x around -inf
mul-1-negN/A
unsub-negN/A
lower--.f64N/A
lower-log.f64N/A
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
lower--.f64N/A
mul-1-negN/A
lower-neg.f64N/A
lower-log.f64N/A
lower-/.f6489.7
Applied rewrites89.7%
Applied rewrites89.7%
if -3.99999999999999991e38 < y < 3.8e5Initial program 94.9%
if 3.8e5 < y Initial program 39.1%
Taylor expanded in x around 0
lower-*.f6439.6
Applied rewrites39.6%
Taylor expanded in z around inf
lower-*.f64N/A
+-commutativeN/A
associate-*l*N/A
lower-fma.f64N/A
Applied rewrites99.5%
Final simplification94.9%
NOTE: x, y, and z should be sorted in increasing order before calling this function.
(FPCore (x y z)
:precision binary64
(if (<= y 380000.0)
(* 2.0 (sqrt (+ (+ (* y x) (* z x)) (* y z))))
(*
z
(fma
x
(* y (sqrt (/ 1.0 (* (* z (* z z)) (+ y x)))))
(* 2.0 (sqrt (/ (+ y x) z)))))))assert(x < y && y < z);
double code(double x, double y, double z) {
double tmp;
if (y <= 380000.0) {
tmp = 2.0 * sqrt((((y * x) + (z * x)) + (y * z)));
} else {
tmp = z * fma(x, (y * sqrt((1.0 / ((z * (z * z)) * (y + x))))), (2.0 * sqrt(((y + x) / z))));
}
return tmp;
}
x, y, z = sort([x, y, z]) function code(x, y, z) tmp = 0.0 if (y <= 380000.0) tmp = Float64(2.0 * sqrt(Float64(Float64(Float64(y * x) + Float64(z * x)) + Float64(y * z)))); else tmp = Float64(z * fma(x, Float64(y * sqrt(Float64(1.0 / Float64(Float64(z * Float64(z * z)) * Float64(y + x))))), Float64(2.0 * sqrt(Float64(Float64(y + x) / z))))); end return tmp end
NOTE: x, y, and z should be sorted in increasing order before calling this function. code[x_, y_, z_] := If[LessEqual[y, 380000.0], N[(2.0 * N[Sqrt[N[(N[(N[(y * x), $MachinePrecision] + N[(z * x), $MachinePrecision]), $MachinePrecision] + N[(y * z), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(z * N[(x * N[(y * N[Sqrt[N[(1.0 / N[(N[(z * N[(z * z), $MachinePrecision]), $MachinePrecision] * N[(y + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(2.0 * N[Sqrt[N[(N[(y + x), $MachinePrecision] / z), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[x, y, z] = \mathsf{sort}([x, y, z])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 380000:\\
\;\;\;\;2 \cdot \sqrt{\left(y \cdot x + z \cdot x\right) + y \cdot z}\\
\mathbf{else}:\\
\;\;\;\;z \cdot \mathsf{fma}\left(x, y \cdot \sqrt{\frac{1}{\left(z \cdot \left(z \cdot z\right)\right) \cdot \left(y + x\right)}}, 2 \cdot \sqrt{\frac{y + x}{z}}\right)\\
\end{array}
\end{array}
if y < 3.8e5Initial program 79.0%
if 3.8e5 < y Initial program 46.7%
Taylor expanded in x around 0
lower-*.f6446.6
Applied rewrites46.6%
Taylor expanded in z around inf
lower-*.f64N/A
+-commutativeN/A
associate-*l*N/A
lower-fma.f64N/A
Applied rewrites99.6%
Final simplification84.1%
(FPCore (x y z)
:precision binary64
(let* ((t_0
(+
(* 0.25 (* (* (pow y -0.75) (* (pow z -0.75) x)) (+ y z)))
(* (pow z 0.25) (pow y 0.25)))))
(if (< z 7.636950090573675e+176)
(* 2.0 (sqrt (+ (* (+ x y) z) (* x y))))
(* (* t_0 t_0) 2.0))))
double code(double x, double y, double z) {
double t_0 = (0.25 * ((pow(y, -0.75) * (pow(z, -0.75) * x)) * (y + z))) + (pow(z, 0.25) * pow(y, 0.25));
double tmp;
if (z < 7.636950090573675e+176) {
tmp = 2.0 * sqrt((((x + y) * z) + (x * y)));
} else {
tmp = (t_0 * t_0) * 2.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) :: t_0
real(8) :: tmp
t_0 = (0.25d0 * (((y ** (-0.75d0)) * ((z ** (-0.75d0)) * x)) * (y + z))) + ((z ** 0.25d0) * (y ** 0.25d0))
if (z < 7.636950090573675d+176) then
tmp = 2.0d0 * sqrt((((x + y) * z) + (x * y)))
else
tmp = (t_0 * t_0) * 2.0d0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = (0.25 * ((Math.pow(y, -0.75) * (Math.pow(z, -0.75) * x)) * (y + z))) + (Math.pow(z, 0.25) * Math.pow(y, 0.25));
double tmp;
if (z < 7.636950090573675e+176) {
tmp = 2.0 * Math.sqrt((((x + y) * z) + (x * y)));
} else {
tmp = (t_0 * t_0) * 2.0;
}
return tmp;
}
def code(x, y, z): t_0 = (0.25 * ((math.pow(y, -0.75) * (math.pow(z, -0.75) * x)) * (y + z))) + (math.pow(z, 0.25) * math.pow(y, 0.25)) tmp = 0 if z < 7.636950090573675e+176: tmp = 2.0 * math.sqrt((((x + y) * z) + (x * y))) else: tmp = (t_0 * t_0) * 2.0 return tmp
function code(x, y, z) t_0 = Float64(Float64(0.25 * Float64(Float64((y ^ -0.75) * Float64((z ^ -0.75) * x)) * Float64(y + z))) + Float64((z ^ 0.25) * (y ^ 0.25))) tmp = 0.0 if (z < 7.636950090573675e+176) tmp = Float64(2.0 * sqrt(Float64(Float64(Float64(x + y) * z) + Float64(x * y)))); else tmp = Float64(Float64(t_0 * t_0) * 2.0); end return tmp end
function tmp_2 = code(x, y, z) t_0 = (0.25 * (((y ^ -0.75) * ((z ^ -0.75) * x)) * (y + z))) + ((z ^ 0.25) * (y ^ 0.25)); tmp = 0.0; if (z < 7.636950090573675e+176) tmp = 2.0 * sqrt((((x + y) * z) + (x * y))); else tmp = (t_0 * t_0) * 2.0; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(0.25 * N[(N[(N[Power[y, -0.75], $MachinePrecision] * N[(N[Power[z, -0.75], $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision] * N[(y + z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Power[z, 0.25], $MachinePrecision] * N[Power[y, 0.25], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[Less[z, 7.636950090573675e+176], N[(2.0 * N[Sqrt[N[(N[(N[(x + y), $MachinePrecision] * z), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(t$95$0 * t$95$0), $MachinePrecision] * 2.0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.25 \cdot \left(\left({y}^{-0.75} \cdot \left({z}^{-0.75} \cdot x\right)\right) \cdot \left(y + z\right)\right) + {z}^{0.25} \cdot {y}^{0.25}\\
\mathbf{if}\;z < 7.636950090573675 \cdot 10^{+176}:\\
\;\;\;\;2 \cdot \sqrt{\left(x + y\right) \cdot z + x \cdot y}\\
\mathbf{else}:\\
\;\;\;\;\left(t\_0 \cdot t\_0\right) \cdot 2\\
\end{array}
\end{array}
herbie shell --seed 2024227
(FPCore (x y z)
:name "Diagrams.TwoD.Apollonian:descartes from diagrams-contrib-1.3.0.5"
:precision binary64
:alt
(! :herbie-platform default (if (< z 763695009057367500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (* 2 (sqrt (+ (* (+ x y) z) (* x y)))) (* (* (+ (* 1/4 (* (* (pow y -3/4) (* (pow z -3/4) x)) (+ y z))) (* (pow z 1/4) (pow y 1/4))) (+ (* 1/4 (* (* (pow y -3/4) (* (pow z -3/4) x)) (+ y z))) (* (pow z 1/4) (pow y 1/4)))) 2)))
(* 2.0 (sqrt (+ (+ (* x y) (* x z)) (* y z)))))