
(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 6 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}
(FPCore (re im)
:precision binary64
(if (<= re -7.5e+157)
(pow
(*
(* (pow (* 0.5 (pow im 2.0)) 0.25) (pow (/ -1.0 re) 0.25))
(* (sqrt 0.5) (sqrt (sqrt 2.0))))
2.0)
(sqrt (* 0.5 (+ re (hypot re im))))))
double code(double re, double im) {
double tmp;
if (re <= -7.5e+157) {
tmp = pow(((pow((0.5 * pow(im, 2.0)), 0.25) * pow((-1.0 / re), 0.25)) * (sqrt(0.5) * sqrt(sqrt(2.0)))), 2.0);
} else {
tmp = sqrt((0.5 * (re + hypot(re, im))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= -7.5e+157) {
tmp = Math.pow(((Math.pow((0.5 * Math.pow(im, 2.0)), 0.25) * Math.pow((-1.0 / re), 0.25)) * (Math.sqrt(0.5) * Math.sqrt(Math.sqrt(2.0)))), 2.0);
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -7.5e+157: tmp = math.pow(((math.pow((0.5 * math.pow(im, 2.0)), 0.25) * math.pow((-1.0 / re), 0.25)) * (math.sqrt(0.5) * math.sqrt(math.sqrt(2.0)))), 2.0) else: tmp = math.sqrt((0.5 * (re + math.hypot(re, im)))) return tmp
function code(re, im) tmp = 0.0 if (re <= -7.5e+157) tmp = Float64(Float64((Float64(0.5 * (im ^ 2.0)) ^ 0.25) * (Float64(-1.0 / re) ^ 0.25)) * Float64(sqrt(0.5) * sqrt(sqrt(2.0)))) ^ 2.0; else tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -7.5e+157) tmp = ((((0.5 * (im ^ 2.0)) ^ 0.25) * ((-1.0 / re) ^ 0.25)) * (sqrt(0.5) * sqrt(sqrt(2.0)))) ^ 2.0; else tmp = sqrt((0.5 * (re + hypot(re, im)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -7.5e+157], N[Power[N[(N[(N[Power[N[(0.5 * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision], 0.25], $MachinePrecision] * N[Power[N[(-1.0 / re), $MachinePrecision], 0.25], $MachinePrecision]), $MachinePrecision] * N[(N[Sqrt[0.5], $MachinePrecision] * N[Sqrt[N[Sqrt[2.0], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -7.5 \cdot 10^{+157}:\\
\;\;\;\;{\left(\left({\left(0.5 \cdot {im}^{2}\right)}^{0.25} \cdot {\left(\frac{-1}{re}\right)}^{0.25}\right) \cdot \left(\sqrt{0.5} \cdot \sqrt{\sqrt{2}}\right)\right)}^{2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -7.5e157Initial program 2.7%
sqr-neg2.7%
+-commutative2.7%
sqr-neg2.7%
+-commutative2.7%
distribute-rgt-in2.7%
cancel-sign-sub2.7%
distribute-rgt-out--2.7%
sub-neg2.7%
remove-double-neg2.7%
+-commutative2.7%
Simplified18.7%
add-sqr-sqrt18.7%
pow218.7%
*-commutative18.7%
Applied egg-rr18.7%
Taylor expanded in re around -inf 62.9%
associate-*l*62.9%
distribute-rgt-in62.9%
exp-sum63.6%
*-commutative63.6%
exp-to-pow62.7%
*-commutative62.7%
exp-to-pow67.5%
Simplified67.5%
if -7.5e157 < re Initial program 45.8%
sqr-neg45.8%
+-commutative45.8%
sqr-neg45.8%
+-commutative45.8%
distribute-rgt-in45.8%
cancel-sign-sub45.8%
distribute-rgt-out--45.8%
sub-neg45.8%
remove-double-neg45.8%
+-commutative45.8%
Simplified85.7%
add-sqr-sqrt85.2%
sqrt-unprod85.7%
*-commutative85.7%
*-commutative85.7%
swap-sqr85.7%
add-sqr-sqrt85.7%
metadata-eval85.7%
Applied egg-rr85.7%
*-commutative85.7%
associate-*r*85.7%
metadata-eval85.7%
Simplified85.7%
Final simplification83.4%
(FPCore (re im) :precision binary64 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0) (* 0.5 (sqrt (/ (pow im 2.0) (- re)))) (sqrt (* 0.5 (+ re (hypot re im))))))
double code(double re, double im) {
double tmp;
if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) {
tmp = 0.5 * sqrt((pow(im, 2.0) / -re));
} else {
tmp = sqrt((0.5 * (re + hypot(re, im))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (Math.sqrt((2.0 * (re + Math.sqrt(((re * re) + (im * im)))))) <= 0.0) {
tmp = 0.5 * Math.sqrt((Math.pow(im, 2.0) / -re));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
return tmp;
}
def code(re, im): tmp = 0 if math.sqrt((2.0 * (re + math.sqrt(((re * re) + (im * im)))))) <= 0.0: tmp = 0.5 * math.sqrt((math.pow(im, 2.0) / -re)) else: tmp = math.sqrt((0.5 * (re + math.hypot(re, im)))) return tmp
function code(re, im) tmp = 0.0 if (sqrt(Float64(2.0 * Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))))) <= 0.0) tmp = Float64(0.5 * sqrt(Float64((im ^ 2.0) / Float64(-re)))); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) tmp = 0.5 * sqrt(((im ^ 2.0) / -re)); else tmp = sqrt((0.5 * (re + hypot(re, im)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[N[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(0.5 * N[Sqrt[N[(N[Power[im, 2.0], $MachinePrecision] / (-re)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{{im}^{2}}{-re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\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 7.1%
sqr-neg7.1%
+-commutative7.1%
sqr-neg7.1%
+-commutative7.1%
distribute-rgt-in7.1%
cancel-sign-sub7.1%
distribute-rgt-out--7.1%
sub-neg7.1%
remove-double-neg7.1%
+-commutative7.1%
Simplified7.1%
Taylor expanded in re around -inf 51.7%
mul-1-neg51.7%
distribute-neg-frac251.7%
Simplified51.7%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 44.7%
sqr-neg44.7%
+-commutative44.7%
sqr-neg44.7%
+-commutative44.7%
distribute-rgt-in44.7%
cancel-sign-sub44.7%
distribute-rgt-out--44.7%
sub-neg44.7%
remove-double-neg44.7%
+-commutative44.7%
Simplified86.4%
add-sqr-sqrt85.8%
sqrt-unprod86.4%
*-commutative86.4%
*-commutative86.4%
swap-sqr86.4%
add-sqr-sqrt86.4%
metadata-eval86.4%
Applied egg-rr86.4%
*-commutative86.4%
associate-*r*86.4%
metadata-eval86.4%
Simplified86.4%
Final simplification82.3%
(FPCore (re im) :precision binary64 (if (<= re -5.5e+172) (exp (* 0.5 (+ (log (* (pow im 2.0) 0.25)) (log (/ -1.0 re))))) (sqrt (* 0.5 (+ re (hypot re im))))))
double code(double re, double im) {
double tmp;
if (re <= -5.5e+172) {
tmp = exp((0.5 * (log((pow(im, 2.0) * 0.25)) + log((-1.0 / re)))));
} else {
tmp = sqrt((0.5 * (re + hypot(re, im))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= -5.5e+172) {
tmp = Math.exp((0.5 * (Math.log((Math.pow(im, 2.0) * 0.25)) + Math.log((-1.0 / re)))));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -5.5e+172: tmp = math.exp((0.5 * (math.log((math.pow(im, 2.0) * 0.25)) + math.log((-1.0 / re))))) else: tmp = math.sqrt((0.5 * (re + math.hypot(re, im)))) return tmp
function code(re, im) tmp = 0.0 if (re <= -5.5e+172) tmp = exp(Float64(0.5 * Float64(log(Float64((im ^ 2.0) * 0.25)) + log(Float64(-1.0 / re))))); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -5.5e+172) tmp = exp((0.5 * (log(((im ^ 2.0) * 0.25)) + log((-1.0 / re))))); else tmp = sqrt((0.5 * (re + hypot(re, im)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -5.5e+172], N[Exp[N[(0.5 * N[(N[Log[N[(N[Power[im, 2.0], $MachinePrecision] * 0.25), $MachinePrecision]], $MachinePrecision] + N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -5.5 \cdot 10^{+172}:\\
\;\;\;\;e^{0.5 \cdot \left(\log \left({im}^{2} \cdot 0.25\right) + \log \left(\frac{-1}{re}\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -5.4999999999999999e172Initial program 2.7%
sqr-neg2.7%
+-commutative2.7%
sqr-neg2.7%
+-commutative2.7%
distribute-rgt-in2.7%
cancel-sign-sub2.7%
distribute-rgt-out--2.7%
sub-neg2.7%
remove-double-neg2.7%
+-commutative2.7%
Simplified16.9%
add-sqr-sqrt16.9%
sqrt-unprod16.9%
*-commutative16.9%
*-commutative16.9%
swap-sqr16.9%
add-sqr-sqrt16.9%
metadata-eval16.9%
Applied egg-rr16.9%
*-commutative16.9%
associate-*r*16.9%
metadata-eval16.9%
Simplified16.9%
pow1/216.9%
pow-to-exp16.6%
Applied egg-rr16.6%
Taylor expanded in re around -inf 64.8%
if -5.4999999999999999e172 < re Initial program 45.3%
sqr-neg45.3%
+-commutative45.3%
sqr-neg45.3%
+-commutative45.3%
distribute-rgt-in45.3%
cancel-sign-sub45.3%
distribute-rgt-out--45.3%
sub-neg45.3%
remove-double-neg45.3%
+-commutative45.3%
Simplified85.1%
add-sqr-sqrt84.5%
sqrt-unprod85.1%
*-commutative85.1%
*-commutative85.1%
swap-sqr85.1%
add-sqr-sqrt85.1%
metadata-eval85.1%
Applied egg-rr85.1%
*-commutative85.1%
associate-*r*85.1%
metadata-eval85.1%
Simplified85.1%
Final simplification82.7%
(FPCore (re im) :precision binary64 (sqrt (* 0.5 (+ re (hypot re im)))))
double code(double re, double im) {
return sqrt((0.5 * (re + hypot(re, im))));
}
public static double code(double re, double im) {
return Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
def code(re, im): return math.sqrt((0.5 * (re + math.hypot(re, im))))
function code(re, im) return sqrt(Float64(0.5 * Float64(re + hypot(re, im)))) end
function tmp = code(re, im) tmp = sqrt((0.5 * (re + hypot(re, im)))); end
code[re_, im_] := N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}
\end{array}
Initial program 40.3%
sqr-neg40.3%
+-commutative40.3%
sqr-neg40.3%
+-commutative40.3%
distribute-rgt-in40.3%
cancel-sign-sub40.3%
distribute-rgt-out--40.3%
sub-neg40.3%
remove-double-neg40.3%
+-commutative40.3%
Simplified77.1%
add-sqr-sqrt76.6%
sqrt-unprod77.1%
*-commutative77.1%
*-commutative77.1%
swap-sqr77.1%
add-sqr-sqrt77.1%
metadata-eval77.1%
Applied egg-rr77.1%
*-commutative77.1%
associate-*r*77.1%
metadata-eval77.1%
Simplified77.1%
Final simplification77.1%
(FPCore (re im) :precision binary64 (if (<= re 9.6e-8) (sqrt (* 0.5 im)) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= 9.6e-8) {
tmp = sqrt((0.5 * im));
} else {
tmp = sqrt(re);
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= 9.6d-8) then
tmp = sqrt((0.5d0 * im))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 9.6e-8) {
tmp = Math.sqrt((0.5 * im));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 9.6e-8: tmp = math.sqrt((0.5 * im)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 9.6e-8) tmp = sqrt(Float64(0.5 * im)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 9.6e-8) tmp = sqrt((0.5 * im)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 9.6e-8], N[Sqrt[N[(0.5 * im), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 9.6 \cdot 10^{-8}:\\
\;\;\;\;\sqrt{0.5 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 9.59999999999999994e-8Initial program 39.3%
sqr-neg39.3%
+-commutative39.3%
sqr-neg39.3%
+-commutative39.3%
distribute-rgt-in39.3%
cancel-sign-sub39.3%
distribute-rgt-out--39.3%
sub-neg39.3%
remove-double-neg39.3%
+-commutative39.3%
Simplified68.2%
add-sqr-sqrt67.7%
sqrt-unprod68.2%
*-commutative68.2%
*-commutative68.2%
swap-sqr68.2%
add-sqr-sqrt68.2%
metadata-eval68.2%
Applied egg-rr68.2%
*-commutative68.2%
associate-*r*68.2%
metadata-eval68.2%
Simplified68.2%
Taylor expanded in re around 0 31.4%
if 9.59999999999999994e-8 < re Initial program 42.8%
sqr-neg42.8%
+-commutative42.8%
sqr-neg42.8%
+-commutative42.8%
distribute-rgt-in42.8%
cancel-sign-sub42.8%
distribute-rgt-out--42.8%
sub-neg42.8%
remove-double-neg42.8%
+-commutative42.8%
Simplified100.0%
add-sqr-sqrt99.3%
pow299.3%
*-commutative99.3%
Applied egg-rr99.3%
Taylor expanded in re around inf 74.4%
unpow274.4%
rem-square-sqrt75.3%
*-commutative75.3%
unpow275.3%
rem-square-sqrt76.7%
metadata-eval76.7%
*-rgt-identity76.7%
Simplified76.7%
Final simplification44.1%
(FPCore (re im) :precision binary64 (sqrt re))
double code(double re, double im) {
return sqrt(re);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt(re)
end function
public static double code(double re, double im) {
return Math.sqrt(re);
}
def code(re, im): return math.sqrt(re)
function code(re, im) return sqrt(re) end
function tmp = code(re, im) tmp = sqrt(re); end
code[re_, im_] := N[Sqrt[re], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{re}
\end{array}
Initial program 40.3%
sqr-neg40.3%
+-commutative40.3%
sqr-neg40.3%
+-commutative40.3%
distribute-rgt-in40.3%
cancel-sign-sub40.3%
distribute-rgt-out--40.3%
sub-neg40.3%
remove-double-neg40.3%
+-commutative40.3%
Simplified77.1%
add-sqr-sqrt76.6%
pow276.6%
*-commutative76.6%
Applied egg-rr76.6%
Taylor expanded in re around inf 26.0%
unpow226.0%
rem-square-sqrt26.3%
*-commutative26.3%
unpow226.3%
rem-square-sqrt26.7%
metadata-eval26.7%
*-rgt-identity26.7%
Simplified26.7%
Final simplification26.7%
(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 2024110
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:alt
(if (< re 0.0) (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))