
(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 -5.2e+16) (* 0.5 (pow (exp (* 0.25 (+ (log (/ -1.0 re)) (log (pow im 2.0))))) 2.0)) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
double code(double re, double im) {
double tmp;
if (re <= -5.2e+16) {
tmp = 0.5 * pow(exp((0.25 * (log((-1.0 / re)) + log(pow(im, 2.0))))), 2.0);
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= -5.2e+16) {
tmp = 0.5 * Math.pow(Math.exp((0.25 * (Math.log((-1.0 / re)) + Math.log(Math.pow(im, 2.0))))), 2.0);
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -5.2e+16: tmp = 0.5 * math.pow(math.exp((0.25 * (math.log((-1.0 / re)) + math.log(math.pow(im, 2.0))))), 2.0) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) return tmp
function code(re, im) tmp = 0.0 if (re <= -5.2e+16) tmp = Float64(0.5 * (exp(Float64(0.25 * Float64(log(Float64(-1.0 / re)) + log((im ^ 2.0))))) ^ 2.0)); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -5.2e+16) tmp = 0.5 * (exp((0.25 * (log((-1.0 / re)) + log((im ^ 2.0))))) ^ 2.0); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -5.2e+16], N[(0.5 * N[Power[N[Exp[N[(0.25 * N[(N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision] + N[Log[N[Power[im, 2.0], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -5.2 \cdot 10^{+16}:\\
\;\;\;\;0.5 \cdot {\left(e^{0.25 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left({im}^{2}\right)\right)}\right)}^{2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -5.2e16Initial 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%
Simplified29.4%
hypot-define6.9%
+-commutative6.9%
add-sqr-sqrt6.9%
pow26.9%
pow1/26.9%
sqrt-pow16.9%
+-commutative6.9%
hypot-define29.1%
metadata-eval29.1%
Applied egg-rr29.1%
Taylor expanded in re around -inf 62.9%
if -5.2e16 < re Initial program 53.1%
sqr-neg53.1%
+-commutative53.1%
sqr-neg53.1%
+-commutative53.1%
distribute-rgt-in53.1%
cancel-sign-sub53.1%
distribute-rgt-out--53.1%
sub-neg53.1%
remove-double-neg53.1%
+-commutative53.1%
Simplified87.9%
Final simplification82.3%
(FPCore (re im) :precision binary64 (if (<= re -1.3e+26) (* 0.5 (sqrt (/ (pow im 2.0) (- re)))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
double code(double re, double im) {
double tmp;
if (re <= -1.3e+26) {
tmp = 0.5 * sqrt((pow(im, 2.0) / -re));
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= -1.3e+26) {
tmp = 0.5 * Math.sqrt((Math.pow(im, 2.0) / -re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.3e+26: tmp = 0.5 * math.sqrt((math.pow(im, 2.0) / -re)) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.3e+26) tmp = Float64(0.5 * sqrt(Float64((im ^ 2.0) / Float64(-re)))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.3e+26) tmp = 0.5 * sqrt(((im ^ 2.0) / -re)); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.3e+26], N[(0.5 * N[Sqrt[N[(N[Power[im, 2.0], $MachinePrecision] / (-re)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.3 \cdot 10^{+26}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{{im}^{2}}{-re}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -1.30000000000000001e26Initial 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%
Simplified29.4%
Taylor expanded in re around -inf 56.5%
mul-1-neg56.5%
distribute-neg-frac256.5%
Simplified56.5%
if -1.30000000000000001e26 < re Initial program 53.1%
sqr-neg53.1%
+-commutative53.1%
sqr-neg53.1%
+-commutative53.1%
distribute-rgt-in53.1%
cancel-sign-sub53.1%
distribute-rgt-out--53.1%
sub-neg53.1%
remove-double-neg53.1%
+-commutative53.1%
Simplified87.9%
Final simplification80.9%
(FPCore (re im)
:precision binary64
(if (<= re -3e+22)
(* 0.5 (sqrt (/ (pow im 2.0) (- re))))
(if (<= re 6.2e-63)
(* 0.5 (sqrt (+ (* im 2.0) (* re (+ 2.0 (/ re im))))))
(* 0.5 (* 2.0 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -3e+22) {
tmp = 0.5 * sqrt((pow(im, 2.0) / -re));
} else if (re <= 6.2e-63) {
tmp = 0.5 * sqrt(((im * 2.0) + (re * (2.0 + (re / im)))));
} else {
tmp = 0.5 * (2.0 * 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 <= (-3d+22)) then
tmp = 0.5d0 * sqrt(((im ** 2.0d0) / -re))
else if (re <= 6.2d-63) then
tmp = 0.5d0 * sqrt(((im * 2.0d0) + (re * (2.0d0 + (re / im)))))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3e+22) {
tmp = 0.5 * Math.sqrt((Math.pow(im, 2.0) / -re));
} else if (re <= 6.2e-63) {
tmp = 0.5 * Math.sqrt(((im * 2.0) + (re * (2.0 + (re / im)))));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3e+22: tmp = 0.5 * math.sqrt((math.pow(im, 2.0) / -re)) elif re <= 6.2e-63: tmp = 0.5 * math.sqrt(((im * 2.0) + (re * (2.0 + (re / im))))) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3e+22) tmp = Float64(0.5 * sqrt(Float64((im ^ 2.0) / Float64(-re)))); elseif (re <= 6.2e-63) tmp = Float64(0.5 * sqrt(Float64(Float64(im * 2.0) + Float64(re * Float64(2.0 + Float64(re / im)))))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3e+22) tmp = 0.5 * sqrt(((im ^ 2.0) / -re)); elseif (re <= 6.2e-63) tmp = 0.5 * sqrt(((im * 2.0) + (re * (2.0 + (re / im))))); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3e+22], N[(0.5 * N[Sqrt[N[(N[Power[im, 2.0], $MachinePrecision] / (-re)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 6.2e-63], N[(0.5 * N[Sqrt[N[(N[(im * 2.0), $MachinePrecision] + N[(re * N[(2.0 + N[(re / im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3 \cdot 10^{+22}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{{im}^{2}}{-re}}\\
\mathbf{elif}\;re \leq 6.2 \cdot 10^{-63}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2 + re \cdot \left(2 + \frac{re}{im}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -3e22Initial 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%
Simplified29.4%
Taylor expanded in re around -inf 56.5%
mul-1-neg56.5%
distribute-neg-frac256.5%
Simplified56.5%
if -3e22 < re < 6.19999999999999968e-63Initial program 51.3%
sqr-neg51.3%
+-commutative51.3%
sqr-neg51.3%
+-commutative51.3%
distribute-rgt-in51.3%
cancel-sign-sub51.3%
distribute-rgt-out--51.3%
sub-neg51.3%
remove-double-neg51.3%
+-commutative51.3%
Simplified81.1%
Taylor expanded in re around 0 37.7%
if 6.19999999999999968e-63 < re Initial program 55.9%
sqr-neg55.9%
+-commutative55.9%
sqr-neg55.9%
+-commutative55.9%
distribute-rgt-in55.9%
cancel-sign-sub55.9%
distribute-rgt-out--55.9%
sub-neg55.9%
remove-double-neg55.9%
+-commutative55.9%
Simplified98.7%
Taylor expanded in re around inf 74.4%
*-commutative74.4%
unpow274.4%
rem-square-sqrt76.0%
Simplified76.0%
Final simplification53.2%
(FPCore (re im) :precision binary64 (if (<= re 5.6e-63) (* 0.5 (* (sqrt im) (sqrt 2.0))) (* 0.5 (* 2.0 (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= 5.6e-63) {
tmp = 0.5 * (sqrt(im) * sqrt(2.0));
} else {
tmp = 0.5 * (2.0 * 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 <= 5.6d-63) then
tmp = 0.5d0 * (sqrt(im) * sqrt(2.0d0))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 5.6e-63) {
tmp = 0.5 * (Math.sqrt(im) * Math.sqrt(2.0));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 5.6e-63: tmp = 0.5 * (math.sqrt(im) * math.sqrt(2.0)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= 5.6e-63) tmp = Float64(0.5 * Float64(sqrt(im) * sqrt(2.0))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 5.6e-63) tmp = 0.5 * (sqrt(im) * sqrt(2.0)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 5.6e-63], N[(0.5 * N[(N[Sqrt[im], $MachinePrecision] * N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 5.6 \cdot 10^{-63}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{im} \cdot \sqrt{2}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < 5.6000000000000005e-63Initial program 37.3%
sqr-neg37.3%
+-commutative37.3%
sqr-neg37.3%
+-commutative37.3%
distribute-rgt-in37.3%
cancel-sign-sub37.3%
distribute-rgt-out--37.3%
sub-neg37.3%
remove-double-neg37.3%
+-commutative37.3%
Simplified64.8%
Taylor expanded in re around 0 29.8%
if 5.6000000000000005e-63 < re Initial program 55.9%
sqr-neg55.9%
+-commutative55.9%
sqr-neg55.9%
+-commutative55.9%
distribute-rgt-in55.9%
cancel-sign-sub55.9%
distribute-rgt-out--55.9%
sub-neg55.9%
remove-double-neg55.9%
+-commutative55.9%
Simplified98.7%
Taylor expanded in re around inf 74.4%
*-commutative74.4%
unpow274.4%
rem-square-sqrt76.0%
Simplified76.0%
Final simplification43.5%
(FPCore (re im) :precision binary64 (if (<= re 1.6e-63) (* 0.5 (sqrt (* im 2.0))) (* 0.5 (* 2.0 (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= 1.6e-63) {
tmp = 0.5 * sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * 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 <= 1.6d-63) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 1.6e-63) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 1.6e-63: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= 1.6e-63) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 1.6e-63) tmp = 0.5 * sqrt((im * 2.0)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 1.6e-63], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 1.6 \cdot 10^{-63}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < 1.59999999999999994e-63Initial program 37.3%
sqr-neg37.3%
+-commutative37.3%
sqr-neg37.3%
+-commutative37.3%
distribute-rgt-in37.3%
cancel-sign-sub37.3%
distribute-rgt-out--37.3%
sub-neg37.3%
remove-double-neg37.3%
+-commutative37.3%
Simplified64.8%
Taylor expanded in re around 0 29.5%
*-commutative29.5%
Simplified29.5%
if 1.59999999999999994e-63 < re Initial program 55.9%
sqr-neg55.9%
+-commutative55.9%
sqr-neg55.9%
+-commutative55.9%
distribute-rgt-in55.9%
cancel-sign-sub55.9%
distribute-rgt-out--55.9%
sub-neg55.9%
remove-double-neg55.9%
+-commutative55.9%
Simplified98.7%
Taylor expanded in re around inf 74.4%
*-commutative74.4%
unpow274.4%
rem-square-sqrt76.0%
Simplified76.0%
Final simplification43.3%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* im 2.0))))
double code(double re, double im) {
return 0.5 * sqrt((im * 2.0));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((im * 2.0d0))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((im * 2.0));
}
def code(re, im): return 0.5 * math.sqrt((im * 2.0))
function code(re, im) return Float64(0.5 * sqrt(Float64(im * 2.0))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((im * 2.0)); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{im \cdot 2}
\end{array}
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%
Simplified74.8%
Taylor expanded in re around 0 24.2%
*-commutative24.2%
Simplified24.2%
Final simplification24.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 2024076
(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)))))