
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im * im))) + re)))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re)))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re))); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re)));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * (sqrt(((re * re) + (im * im))) + re)))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re)))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re))); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\end{array}
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im_m im_m)))))) 0.0) (* 0.5 (exp (* 0.5 (+ (log (/ -1.0 re)) (* 2.0 (log im_m)))))) (sqrt (* 0.5 (+ re (hypot im_m re))))))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (sqrt((2.0 * (re + sqrt(((re * re) + (im_m * im_m)))))) <= 0.0) {
tmp = 0.5 * exp((0.5 * (log((-1.0 / re)) + (2.0 * log(im_m)))));
} else {
tmp = sqrt((0.5 * (re + hypot(im_m, re))));
}
return tmp;
}
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (Math.sqrt((2.0 * (re + Math.sqrt(((re * re) + (im_m * im_m)))))) <= 0.0) {
tmp = 0.5 * Math.exp((0.5 * (Math.log((-1.0 / re)) + (2.0 * Math.log(im_m)))));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(im_m, re))));
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if math.sqrt((2.0 * (re + math.sqrt(((re * re) + (im_m * im_m)))))) <= 0.0: tmp = 0.5 * math.exp((0.5 * (math.log((-1.0 / re)) + (2.0 * math.log(im_m))))) else: tmp = math.sqrt((0.5 * (re + math.hypot(im_m, re)))) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (sqrt(Float64(2.0 * Float64(re + sqrt(Float64(Float64(re * re) + Float64(im_m * im_m)))))) <= 0.0) tmp = Float64(0.5 * exp(Float64(0.5 * Float64(log(Float64(-1.0 / re)) + Float64(2.0 * log(im_m)))))); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(im_m, re)))); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (sqrt((2.0 * (re + sqrt(((re * re) + (im_m * im_m)))))) <= 0.0) tmp = 0.5 * exp((0.5 * (log((-1.0 / re)) + (2.0 * log(im_m))))); else tmp = sqrt((0.5 * (re + hypot(im_m, re)))); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[N[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im$95$m * im$95$m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(0.5 * N[Exp[N[(0.5 * N[(N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision] + N[(2.0 * N[Log[im$95$m], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[im$95$m ^ 2 + re ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im\_m \cdot im\_m}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot e^{0.5 \cdot \left(\log \left(\frac{-1}{re}\right) + 2 \cdot \log im\_m\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(im\_m, re\right)\right)}\\
\end{array}
\end{array}
if (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 6.9%
sqr-neg6.9%
+-commutative6.9%
sqr-neg6.9%
+-commutative6.9%
distribute-rgt-in6.9%
cancel-sign-sub6.9%
distribute-rgt-out--6.9%
sub-neg6.9%
remove-double-neg6.9%
+-commutative6.9%
Simplified6.9%
hypot-define6.9%
+-commutative6.9%
add-sqr-sqrt6.9%
pow26.9%
pow1/26.9%
sqrt-pow16.9%
+-commutative6.9%
hypot-define6.9%
metadata-eval6.9%
Applied egg-rr6.9%
Taylor expanded in re around -inf 51.9%
exp-prod50.2%
Simplified50.2%
pow150.2%
unpow250.2%
pow-prod-down50.6%
prod-exp51.8%
metadata-eval51.8%
sum-log42.4%
frac-2neg42.4%
metadata-eval42.4%
associate-*l/42.4%
*-un-lft-identity42.4%
Applied egg-rr42.4%
unpow142.4%
Simplified42.4%
Taylor expanded in im around 0 45.2%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 50.7%
sqr-neg50.7%
+-commutative50.7%
sqr-neg50.7%
+-commutative50.7%
distribute-rgt-in50.7%
cancel-sign-sub50.7%
distribute-rgt-out--50.7%
sub-neg50.7%
remove-double-neg50.7%
+-commutative50.7%
Simplified88.5%
hypot-define50.7%
+-commutative50.7%
add-sqr-sqrt50.3%
sqrt-unprod50.7%
*-commutative50.7%
*-commutative50.7%
swap-sqr50.7%
Applied egg-rr88.5%
*-commutative88.5%
associate-*r*88.5%
metadata-eval88.5%
hypot-undefine50.7%
unpow250.7%
unpow250.7%
+-commutative50.7%
unpow250.7%
unpow250.7%
hypot-undefine88.5%
Simplified88.5%
Final simplification83.1%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im_m im_m)))))) 0.0) (* 0.5 (* (pow (/ -1.0 re) 0.5) (pow (pow im_m 2.0) 0.5))) (sqrt (* 0.5 (+ re (hypot im_m re))))))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (sqrt((2.0 * (re + sqrt(((re * re) + (im_m * im_m)))))) <= 0.0) {
tmp = 0.5 * (pow((-1.0 / re), 0.5) * pow(pow(im_m, 2.0), 0.5));
} else {
tmp = sqrt((0.5 * (re + hypot(im_m, re))));
}
return tmp;
}
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (Math.sqrt((2.0 * (re + Math.sqrt(((re * re) + (im_m * im_m)))))) <= 0.0) {
tmp = 0.5 * (Math.pow((-1.0 / re), 0.5) * Math.pow(Math.pow(im_m, 2.0), 0.5));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(im_m, re))));
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if math.sqrt((2.0 * (re + math.sqrt(((re * re) + (im_m * im_m)))))) <= 0.0: tmp = 0.5 * (math.pow((-1.0 / re), 0.5) * math.pow(math.pow(im_m, 2.0), 0.5)) else: tmp = math.sqrt((0.5 * (re + math.hypot(im_m, re)))) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (sqrt(Float64(2.0 * Float64(re + sqrt(Float64(Float64(re * re) + Float64(im_m * im_m)))))) <= 0.0) tmp = Float64(0.5 * Float64((Float64(-1.0 / re) ^ 0.5) * ((im_m ^ 2.0) ^ 0.5))); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(im_m, re)))); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (sqrt((2.0 * (re + sqrt(((re * re) + (im_m * im_m)))))) <= 0.0) tmp = 0.5 * (((-1.0 / re) ^ 0.5) * ((im_m ^ 2.0) ^ 0.5)); else tmp = sqrt((0.5 * (re + hypot(im_m, re)))); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[N[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im$95$m * im$95$m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(0.5 * N[(N[Power[N[(-1.0 / re), $MachinePrecision], 0.5], $MachinePrecision] * N[Power[N[Power[im$95$m, 2.0], $MachinePrecision], 0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[im$95$m ^ 2 + re ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im\_m \cdot im\_m}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \left({\left(\frac{-1}{re}\right)}^{0.5} \cdot {\left({im\_m}^{2}\right)}^{0.5}\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(im\_m, re\right)\right)}\\
\end{array}
\end{array}
if (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 6.9%
sqr-neg6.9%
+-commutative6.9%
sqr-neg6.9%
+-commutative6.9%
distribute-rgt-in6.9%
cancel-sign-sub6.9%
distribute-rgt-out--6.9%
sub-neg6.9%
remove-double-neg6.9%
+-commutative6.9%
Simplified6.9%
hypot-define6.9%
+-commutative6.9%
add-sqr-sqrt6.9%
pow26.9%
pow1/26.9%
sqrt-pow16.9%
+-commutative6.9%
hypot-define6.9%
metadata-eval6.9%
Applied egg-rr6.9%
Taylor expanded in re around -inf 51.9%
exp-prod50.2%
Simplified50.2%
pow150.2%
unpow250.2%
pow-prod-down50.6%
prod-exp51.8%
metadata-eval51.8%
sum-log42.4%
frac-2neg42.4%
metadata-eval42.4%
associate-*l/42.4%
*-un-lft-identity42.4%
Applied egg-rr42.4%
unpow142.4%
Simplified42.4%
Taylor expanded in re around -inf 51.9%
distribute-rgt-in51.9%
exp-sum52.5%
exp-to-pow52.8%
exp-to-pow56.3%
Simplified56.3%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 50.7%
sqr-neg50.7%
+-commutative50.7%
sqr-neg50.7%
+-commutative50.7%
distribute-rgt-in50.7%
cancel-sign-sub50.7%
distribute-rgt-out--50.7%
sub-neg50.7%
remove-double-neg50.7%
+-commutative50.7%
Simplified88.5%
hypot-define50.7%
+-commutative50.7%
add-sqr-sqrt50.3%
sqrt-unprod50.7%
*-commutative50.7%
*-commutative50.7%
swap-sqr50.7%
Applied egg-rr88.5%
*-commutative88.5%
associate-*r*88.5%
metadata-eval88.5%
hypot-undefine50.7%
unpow250.7%
unpow250.7%
+-commutative50.7%
unpow250.7%
unpow250.7%
hypot-undefine88.5%
Simplified88.5%
Final simplification84.5%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im_m im_m)))))) 0.0) (* 0.5 (sqrt (/ (pow im_m 2.0) (- re)))) (sqrt (* 0.5 (+ re (hypot im_m re))))))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (sqrt((2.0 * (re + sqrt(((re * re) + (im_m * im_m)))))) <= 0.0) {
tmp = 0.5 * sqrt((pow(im_m, 2.0) / -re));
} else {
tmp = sqrt((0.5 * (re + hypot(im_m, re))));
}
return tmp;
}
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (Math.sqrt((2.0 * (re + Math.sqrt(((re * re) + (im_m * im_m)))))) <= 0.0) {
tmp = 0.5 * Math.sqrt((Math.pow(im_m, 2.0) / -re));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(im_m, re))));
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if math.sqrt((2.0 * (re + math.sqrt(((re * re) + (im_m * im_m)))))) <= 0.0: tmp = 0.5 * math.sqrt((math.pow(im_m, 2.0) / -re)) else: tmp = math.sqrt((0.5 * (re + math.hypot(im_m, re)))) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (sqrt(Float64(2.0 * Float64(re + sqrt(Float64(Float64(re * re) + Float64(im_m * im_m)))))) <= 0.0) tmp = Float64(0.5 * sqrt(Float64((im_m ^ 2.0) / Float64(-re)))); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(im_m, re)))); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (sqrt((2.0 * (re + sqrt(((re * re) + (im_m * im_m)))))) <= 0.0) tmp = 0.5 * sqrt(((im_m ^ 2.0) / -re)); else tmp = sqrt((0.5 * (re + hypot(im_m, re)))); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[N[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im$95$m * im$95$m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(0.5 * N[Sqrt[N[(N[Power[im$95$m, 2.0], $MachinePrecision] / (-re)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[im$95$m ^ 2 + re ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im\_m \cdot im\_m}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{{im\_m}^{2}}{-re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(im\_m, re\right)\right)}\\
\end{array}
\end{array}
if (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 6.9%
sqr-neg6.9%
+-commutative6.9%
sqr-neg6.9%
+-commutative6.9%
distribute-rgt-in6.9%
cancel-sign-sub6.9%
distribute-rgt-out--6.9%
sub-neg6.9%
remove-double-neg6.9%
+-commutative6.9%
Simplified6.9%
Taylor expanded in re around -inf 45.0%
mul-1-neg45.0%
distribute-neg-frac245.0%
Simplified45.0%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 50.7%
sqr-neg50.7%
+-commutative50.7%
sqr-neg50.7%
+-commutative50.7%
distribute-rgt-in50.7%
cancel-sign-sub50.7%
distribute-rgt-out--50.7%
sub-neg50.7%
remove-double-neg50.7%
+-commutative50.7%
Simplified88.5%
hypot-define50.7%
+-commutative50.7%
add-sqr-sqrt50.3%
sqrt-unprod50.7%
*-commutative50.7%
*-commutative50.7%
swap-sqr50.7%
Applied egg-rr88.5%
*-commutative88.5%
associate-*r*88.5%
metadata-eval88.5%
hypot-undefine50.7%
unpow250.7%
unpow250.7%
+-commutative50.7%
unpow250.7%
unpow250.7%
hypot-undefine88.5%
Simplified88.5%
Final simplification83.1%
im_m = (fabs.f64 im)
(FPCore (re im_m)
:precision binary64
(if (<= re -1.25e+15)
(sqrt (* -0.25 (/ (pow im_m 2.0) re)))
(if (<= re 2.55e-39)
(* 0.5 (sqrt (+ (* 2.0 im_m) (* re (+ 2.0 (/ re im_m))))))
(sqrt re))))im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= -1.25e+15) {
tmp = sqrt((-0.25 * (pow(im_m, 2.0) / re)));
} else if (re <= 2.55e-39) {
tmp = 0.5 * sqrt(((2.0 * im_m) + (re * (2.0 + (re / im_m)))));
} else {
tmp = sqrt(re);
}
return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (re <= (-1.25d+15)) then
tmp = sqrt(((-0.25d0) * ((im_m ** 2.0d0) / re)))
else if (re <= 2.55d-39) then
tmp = 0.5d0 * sqrt(((2.0d0 * im_m) + (re * (2.0d0 + (re / im_m)))))
else
tmp = sqrt(re)
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= -1.25e+15) {
tmp = Math.sqrt((-0.25 * (Math.pow(im_m, 2.0) / re)));
} else if (re <= 2.55e-39) {
tmp = 0.5 * Math.sqrt(((2.0 * im_m) + (re * (2.0 + (re / im_m)))));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= -1.25e+15: tmp = math.sqrt((-0.25 * (math.pow(im_m, 2.0) / re))) elif re <= 2.55e-39: tmp = 0.5 * math.sqrt(((2.0 * im_m) + (re * (2.0 + (re / im_m))))) else: tmp = math.sqrt(re) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= -1.25e+15) tmp = sqrt(Float64(-0.25 * Float64((im_m ^ 2.0) / re))); elseif (re <= 2.55e-39) tmp = Float64(0.5 * sqrt(Float64(Float64(2.0 * im_m) + Float64(re * Float64(2.0 + Float64(re / im_m)))))); else tmp = sqrt(re); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= -1.25e+15) tmp = sqrt((-0.25 * ((im_m ^ 2.0) / re))); elseif (re <= 2.55e-39) tmp = 0.5 * sqrt(((2.0 * im_m) + (re * (2.0 + (re / im_m))))); else tmp = sqrt(re); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, -1.25e+15], N[Sqrt[N[(-0.25 * N[(N[Power[im$95$m, 2.0], $MachinePrecision] / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[re, 2.55e-39], N[(0.5 * N[Sqrt[N[(N[(2.0 * im$95$m), $MachinePrecision] + N[(re * N[(2.0 + N[(re / im$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.25 \cdot 10^{+15}:\\
\;\;\;\;\sqrt{-0.25 \cdot \frac{{im\_m}^{2}}{re}}\\
\mathbf{elif}\;re \leq 2.55 \cdot 10^{-39}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im\_m + re \cdot \left(2 + \frac{re}{im\_m}\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -1.25e15Initial program 6.4%
sqr-neg6.4%
+-commutative6.4%
sqr-neg6.4%
+-commutative6.4%
distribute-rgt-in6.4%
cancel-sign-sub6.4%
distribute-rgt-out--6.4%
sub-neg6.4%
remove-double-neg6.4%
+-commutative6.4%
Simplified39.2%
hypot-define6.4%
+-commutative6.4%
add-sqr-sqrt6.4%
sqrt-unprod6.4%
*-commutative6.4%
*-commutative6.4%
swap-sqr6.4%
Applied egg-rr39.2%
*-commutative39.2%
associate-*r*39.2%
metadata-eval39.2%
hypot-undefine6.4%
unpow26.4%
unpow26.4%
+-commutative6.4%
unpow26.4%
unpow26.4%
hypot-undefine39.2%
Simplified39.2%
Taylor expanded in re around -inf 43.0%
if -1.25e15 < re < 2.54999999999999994e-39Initial program 58.7%
sqr-neg58.7%
+-commutative58.7%
sqr-neg58.7%
+-commutative58.7%
distribute-rgt-in58.7%
cancel-sign-sub58.7%
distribute-rgt-out--58.7%
sub-neg58.7%
remove-double-neg58.7%
+-commutative58.7%
Simplified84.2%
Taylor expanded in re around 0 29.6%
if 2.54999999999999994e-39 < re Initial program 50.6%
sqr-neg50.6%
+-commutative50.6%
sqr-neg50.6%
+-commutative50.6%
distribute-rgt-in50.6%
cancel-sign-sub50.6%
distribute-rgt-out--50.6%
sub-neg50.6%
remove-double-neg50.6%
+-commutative50.6%
Simplified100.0%
hypot-define50.6%
+-commutative50.6%
add-sqr-sqrt50.4%
sqrt-unprod50.6%
*-commutative50.6%
*-commutative50.6%
swap-sqr50.6%
Applied egg-rr100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine50.6%
unpow250.6%
unpow250.6%
+-commutative50.6%
unpow250.6%
unpow250.6%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around inf 80.2%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (sqrt (* 0.5 (+ re (hypot im_m re)))))
im_m = fabs(im);
double code(double re, double im_m) {
return sqrt((0.5 * (re + hypot(im_m, re))));
}
im_m = Math.abs(im);
public static double code(double re, double im_m) {
return Math.sqrt((0.5 * (re + Math.hypot(im_m, re))));
}
im_m = math.fabs(im) def code(re, im_m): return math.sqrt((0.5 * (re + math.hypot(im_m, re))))
im_m = abs(im) function code(re, im_m) return sqrt(Float64(0.5 * Float64(re + hypot(im_m, re)))) end
im_m = abs(im); function tmp = code(re, im_m) tmp = sqrt((0.5 * (re + hypot(im_m, re)))); end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := N[Sqrt[N[(0.5 * N[(re + N[Sqrt[im$95$m ^ 2 + re ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|
\\
\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(im\_m, re\right)\right)}
\end{array}
Initial program 45.2%
sqr-neg45.2%
+-commutative45.2%
sqr-neg45.2%
+-commutative45.2%
distribute-rgt-in45.2%
cancel-sign-sub45.2%
distribute-rgt-out--45.2%
sub-neg45.2%
remove-double-neg45.2%
+-commutative45.2%
Simplified78.3%
hypot-define45.2%
+-commutative45.2%
add-sqr-sqrt44.9%
sqrt-unprod45.2%
*-commutative45.2%
*-commutative45.2%
swap-sqr45.2%
Applied egg-rr78.3%
*-commutative78.3%
associate-*r*78.3%
metadata-eval78.3%
hypot-undefine45.2%
unpow245.2%
unpow245.2%
+-commutative45.2%
unpow245.2%
unpow245.2%
hypot-undefine78.3%
Simplified78.3%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= re 4.3e-39) (* 0.5 (sqrt (+ (* 2.0 im_m) (* re (+ 2.0 (/ re im_m)))))) (sqrt re)))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= 4.3e-39) {
tmp = 0.5 * sqrt(((2.0 * im_m) + (re * (2.0 + (re / im_m)))));
} else {
tmp = sqrt(re);
}
return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (re <= 4.3d-39) then
tmp = 0.5d0 * sqrt(((2.0d0 * im_m) + (re * (2.0d0 + (re / im_m)))))
else
tmp = sqrt(re)
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= 4.3e-39) {
tmp = 0.5 * Math.sqrt(((2.0 * im_m) + (re * (2.0 + (re / im_m)))));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= 4.3e-39: tmp = 0.5 * math.sqrt(((2.0 * im_m) + (re * (2.0 + (re / im_m))))) else: tmp = math.sqrt(re) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= 4.3e-39) tmp = Float64(0.5 * sqrt(Float64(Float64(2.0 * im_m) + Float64(re * Float64(2.0 + Float64(re / im_m)))))); else tmp = sqrt(re); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= 4.3e-39) tmp = 0.5 * sqrt(((2.0 * im_m) + (re * (2.0 + (re / im_m))))); else tmp = sqrt(re); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, 4.3e-39], N[(0.5 * N[Sqrt[N[(N[(2.0 * im$95$m), $MachinePrecision] + N[(re * N[(2.0 + N[(re / im$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq 4.3 \cdot 10^{-39}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im\_m + re \cdot \left(2 + \frac{re}{im\_m}\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 4.2999999999999999e-39Initial program 43.4%
sqr-neg43.4%
+-commutative43.4%
sqr-neg43.4%
+-commutative43.4%
distribute-rgt-in43.4%
cancel-sign-sub43.4%
distribute-rgt-out--43.4%
sub-neg43.4%
remove-double-neg43.4%
+-commutative43.4%
Simplified71.1%
Taylor expanded in re around 0 26.0%
if 4.2999999999999999e-39 < re Initial program 50.6%
sqr-neg50.6%
+-commutative50.6%
sqr-neg50.6%
+-commutative50.6%
distribute-rgt-in50.6%
cancel-sign-sub50.6%
distribute-rgt-out--50.6%
sub-neg50.6%
remove-double-neg50.6%
+-commutative50.6%
Simplified100.0%
hypot-define50.6%
+-commutative50.6%
add-sqr-sqrt50.4%
sqrt-unprod50.6%
*-commutative50.6%
*-commutative50.6%
swap-sqr50.6%
Applied egg-rr100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine50.6%
unpow250.6%
unpow250.6%
+-commutative50.6%
unpow250.6%
unpow250.6%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around inf 80.2%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= re 5e-39) (sqrt (* im_m 0.5)) (sqrt re)))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= 5e-39) {
tmp = sqrt((im_m * 0.5));
} else {
tmp = sqrt(re);
}
return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (re <= 5d-39) then
tmp = sqrt((im_m * 0.5d0))
else
tmp = sqrt(re)
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= 5e-39) {
tmp = Math.sqrt((im_m * 0.5));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= 5e-39: tmp = math.sqrt((im_m * 0.5)) else: tmp = math.sqrt(re) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= 5e-39) tmp = sqrt(Float64(im_m * 0.5)); else tmp = sqrt(re); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= 5e-39) tmp = sqrt((im_m * 0.5)); else tmp = sqrt(re); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, 5e-39], N[Sqrt[N[(im$95$m * 0.5), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq 5 \cdot 10^{-39}:\\
\;\;\;\;\sqrt{im\_m \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 4.9999999999999998e-39Initial program 43.4%
sqr-neg43.4%
+-commutative43.4%
sqr-neg43.4%
+-commutative43.4%
distribute-rgt-in43.4%
cancel-sign-sub43.4%
distribute-rgt-out--43.4%
sub-neg43.4%
remove-double-neg43.4%
+-commutative43.4%
Simplified71.1%
hypot-define43.4%
+-commutative43.4%
add-sqr-sqrt43.1%
sqrt-unprod43.4%
*-commutative43.4%
*-commutative43.4%
swap-sqr43.4%
Applied egg-rr71.1%
*-commutative71.1%
associate-*r*71.1%
metadata-eval71.1%
hypot-undefine43.4%
unpow243.4%
unpow243.4%
+-commutative43.4%
unpow243.4%
unpow243.4%
hypot-undefine71.1%
Simplified71.1%
Taylor expanded in re around 0 26.3%
*-commutative26.3%
Simplified26.3%
if 4.9999999999999998e-39 < re Initial program 50.6%
sqr-neg50.6%
+-commutative50.6%
sqr-neg50.6%
+-commutative50.6%
distribute-rgt-in50.6%
cancel-sign-sub50.6%
distribute-rgt-out--50.6%
sub-neg50.6%
remove-double-neg50.6%
+-commutative50.6%
Simplified100.0%
hypot-define50.6%
+-commutative50.6%
add-sqr-sqrt50.4%
sqrt-unprod50.6%
*-commutative50.6%
*-commutative50.6%
swap-sqr50.6%
Applied egg-rr100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine50.6%
unpow250.6%
unpow250.6%
+-commutative50.6%
unpow250.6%
unpow250.6%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around inf 80.2%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (sqrt re))
im_m = fabs(im);
double code(double re, double im_m) {
return sqrt(re);
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
code = sqrt(re)
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
return Math.sqrt(re);
}
im_m = math.fabs(im) def code(re, im_m): return math.sqrt(re)
im_m = abs(im) function code(re, im_m) return sqrt(re) end
im_m = abs(im); function tmp = code(re, im_m) tmp = sqrt(re); end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := N[Sqrt[re], $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|
\\
\sqrt{re}
\end{array}
Initial program 45.2%
sqr-neg45.2%
+-commutative45.2%
sqr-neg45.2%
+-commutative45.2%
distribute-rgt-in45.2%
cancel-sign-sub45.2%
distribute-rgt-out--45.2%
sub-neg45.2%
remove-double-neg45.2%
+-commutative45.2%
Simplified78.3%
hypot-define45.2%
+-commutative45.2%
add-sqr-sqrt44.9%
sqrt-unprod45.2%
*-commutative45.2%
*-commutative45.2%
swap-sqr45.2%
Applied egg-rr78.3%
*-commutative78.3%
associate-*r*78.3%
metadata-eval78.3%
hypot-undefine45.2%
unpow245.2%
unpow245.2%
+-commutative45.2%
unpow245.2%
unpow245.2%
hypot-undefine78.3%
Simplified78.3%
Taylor expanded in re around inf 24.2%
(FPCore (re im)
:precision binary64
(let* ((t_0 (sqrt (+ (* re re) (* im im)))))
(if (< re 0.0)
(* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- t_0 re)))))
(* 0.5 (sqrt (* 2.0 (+ t_0 re)))))))
double code(double re, double im) {
double t_0 = sqrt(((re * re) + (im * im)));
double tmp;
if (re < 0.0) {
tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
} else {
tmp = 0.5 * sqrt((2.0 * (t_0 + re)));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt(((re * re) + (im * im)))
if (re < 0.0d0) then
tmp = 0.5d0 * (sqrt(2.0d0) * sqrt(((im * im) / (t_0 - re))))
else
tmp = 0.5d0 * sqrt((2.0d0 * (t_0 + re)))
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = Math.sqrt(((re * re) + (im * im)));
double tmp;
if (re < 0.0) {
tmp = 0.5 * (Math.sqrt(2.0) * Math.sqrt(((im * im) / (t_0 - re))));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (t_0 + re)));
}
return tmp;
}
def code(re, im): t_0 = math.sqrt(((re * re) + (im * im))) tmp = 0 if re < 0.0: tmp = 0.5 * (math.sqrt(2.0) * math.sqrt(((im * im) / (t_0 - re)))) else: tmp = 0.5 * math.sqrt((2.0 * (t_0 + re))) return tmp
function code(re, im) t_0 = sqrt(Float64(Float64(re * re) + Float64(im * im))) tmp = 0.0 if (re < 0.0) tmp = Float64(0.5 * Float64(sqrt(2.0) * sqrt(Float64(Float64(im * im) / Float64(t_0 - re))))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(t_0 + re)))); end return tmp end
function tmp_2 = code(re, im) t_0 = sqrt(((re * re) + (im * im))); tmp = 0.0; if (re < 0.0) tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re)))); else tmp = 0.5 * sqrt((2.0 * (t_0 + re))); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[Less[re, 0.0], N[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[N[(N[(im * im), $MachinePrecision] / N[(t$95$0 - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(t$95$0 + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{re \cdot re + im \cdot im}\\
\mathbf{if}\;re < 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t\_0 - re}}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t\_0 + re\right)}\\
\end{array}
\end{array}
herbie shell --seed 2024180
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:alt
(! :herbie-platform default (if (< re 0) (* 1/2 (* (sqrt 2) (sqrt (/ (* im im) (- (modulus re im) re))))) (* 1/2 (sqrt (* 2 (+ (modulus re im) re))))))
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))