
(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 -2.7e-16) (* 0.5 (sqrt (* 2.0 (* -0.5 (/ (pow im 2.0) re))))) (sqrt (* 0.5 (+ re (hypot im re))))))
double code(double re, double im) {
double tmp;
if (re <= -2.7e-16) {
tmp = 0.5 * sqrt((2.0 * (-0.5 * (pow(im, 2.0) / re))));
} else {
tmp = sqrt((0.5 * (re + hypot(im, re))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= -2.7e-16) {
tmp = 0.5 * Math.sqrt((2.0 * (-0.5 * (Math.pow(im, 2.0) / re))));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(im, re))));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.7e-16: tmp = 0.5 * math.sqrt((2.0 * (-0.5 * (math.pow(im, 2.0) / re)))) else: tmp = math.sqrt((0.5 * (re + math.hypot(im, re)))) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.7e-16) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(-0.5 * Float64((im ^ 2.0) / re))))); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(im, re)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.7e-16) tmp = 0.5 * sqrt((2.0 * (-0.5 * ((im ^ 2.0) / re)))); else tmp = sqrt((0.5 * (re + hypot(im, re)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.7e-16], N[(0.5 * N[Sqrt[N[(2.0 * N[(-0.5 * N[(N[Power[im, 2.0], $MachinePrecision] / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[im ^ 2 + re ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.7 \cdot 10^{-16}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(im, re\right)\right)}\\
\end{array}
\end{array}
if re < -2.69999999999999999e-16Initial program 8.4%
sqr-neg8.4%
+-commutative8.4%
sqr-neg8.4%
+-commutative8.4%
distribute-rgt-in8.4%
cancel-sign-sub8.4%
distribute-rgt-out--8.4%
sub-neg8.4%
remove-double-neg8.4%
+-commutative8.4%
hypot-define30.4%
Simplified30.4%
Taylor expanded in re around -inf 52.0%
if -2.69999999999999999e-16 < re Initial program 51.2%
sqr-neg51.2%
+-commutative51.2%
sqr-neg51.2%
+-commutative51.2%
distribute-rgt-in51.2%
cancel-sign-sub51.2%
distribute-rgt-out--51.2%
sub-neg51.2%
remove-double-neg51.2%
+-commutative51.2%
hypot-define91.4%
Simplified91.4%
*-commutative91.4%
hypot-define51.2%
+-commutative51.2%
*-commutative51.2%
add-sqr-sqrt50.9%
sqrt-unprod51.2%
*-commutative51.2%
*-commutative51.2%
swap-sqr51.2%
Applied egg-rr91.4%
*-commutative91.4%
associate-*r*91.9%
metadata-eval91.9%
hypot-undefine51.2%
unpow251.2%
unpow251.2%
+-commutative51.2%
unpow251.2%
unpow251.2%
hypot-undefine91.9%
Simplified91.9%
(FPCore (re im) :precision binary64 (sqrt (* 0.5 (+ re (hypot im re)))))
double code(double re, double im) {
return sqrt((0.5 * (re + hypot(im, re))));
}
public static double code(double re, double im) {
return Math.sqrt((0.5 * (re + Math.hypot(im, re))));
}
def code(re, im): return math.sqrt((0.5 * (re + math.hypot(im, re))))
function code(re, im) return sqrt(Float64(0.5 * Float64(re + hypot(im, re)))) end
function tmp = code(re, im) tmp = sqrt((0.5 * (re + hypot(im, re)))); end
code[re_, im_] := N[Sqrt[N[(0.5 * N[(re + N[Sqrt[im ^ 2 + re ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(im, re\right)\right)}
\end{array}
Initial program 39.7%
sqr-neg39.7%
+-commutative39.7%
sqr-neg39.7%
+-commutative39.7%
distribute-rgt-in39.7%
cancel-sign-sub39.7%
distribute-rgt-out--39.7%
sub-neg39.7%
remove-double-neg39.7%
+-commutative39.7%
hypot-define74.9%
Simplified74.9%
*-commutative74.9%
hypot-define39.7%
+-commutative39.7%
*-commutative39.7%
add-sqr-sqrt39.4%
sqrt-unprod39.7%
*-commutative39.7%
*-commutative39.7%
swap-sqr39.7%
Applied egg-rr74.9%
*-commutative74.9%
associate-*r*75.3%
metadata-eval75.3%
hypot-undefine39.7%
unpow239.7%
unpow239.7%
+-commutative39.7%
unpow239.7%
unpow239.7%
hypot-undefine75.3%
Simplified75.3%
(FPCore (re im)
:precision binary64
(if (or (<= re 3.6e-187)
(and (not (<= re 4.3e-157))
(or (<= re 3e-6) (and (not (<= re 4.7e+75)) (<= re 1.62e+93)))))
(* 0.5 (sqrt (* 2.0 im)))
(sqrt re)))
double code(double re, double im) {
double tmp;
if ((re <= 3.6e-187) || (!(re <= 4.3e-157) && ((re <= 3e-6) || (!(re <= 4.7e+75) && (re <= 1.62e+93))))) {
tmp = 0.5 * sqrt((2.0 * 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 <= 3.6d-187) .or. (.not. (re <= 4.3d-157)) .and. (re <= 3d-6) .or. (.not. (re <= 4.7d+75)) .and. (re <= 1.62d+93)) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if ((re <= 3.6e-187) || (!(re <= 4.3e-157) && ((re <= 3e-6) || (!(re <= 4.7e+75) && (re <= 1.62e+93))))) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if (re <= 3.6e-187) or (not (re <= 4.3e-157) and ((re <= 3e-6) or (not (re <= 4.7e+75) and (re <= 1.62e+93)))): tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if ((re <= 3.6e-187) || (!(re <= 4.3e-157) && ((re <= 3e-6) || (!(re <= 4.7e+75) && (re <= 1.62e+93))))) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if ((re <= 3.6e-187) || (~((re <= 4.3e-157)) && ((re <= 3e-6) || (~((re <= 4.7e+75)) && (re <= 1.62e+93))))) tmp = 0.5 * sqrt((2.0 * im)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[Or[LessEqual[re, 3.6e-187], And[N[Not[LessEqual[re, 4.3e-157]], $MachinePrecision], Or[LessEqual[re, 3e-6], And[N[Not[LessEqual[re, 4.7e+75]], $MachinePrecision], LessEqual[re, 1.62e+93]]]]], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 3.6 \cdot 10^{-187} \lor \neg \left(re \leq 4.3 \cdot 10^{-157}\right) \land \left(re \leq 3 \cdot 10^{-6} \lor \neg \left(re \leq 4.7 \cdot 10^{+75}\right) \land re \leq 1.62 \cdot 10^{+93}\right):\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 3.59999999999999994e-187 or 4.2999999999999998e-157 < re < 3.0000000000000001e-6 or 4.69999999999999984e75 < re < 1.62e93Initial program 37.7%
sqr-neg37.7%
+-commutative37.7%
sqr-neg37.7%
+-commutative37.7%
distribute-rgt-in37.7%
cancel-sign-sub37.7%
distribute-rgt-out--37.7%
sub-neg37.7%
remove-double-neg37.7%
+-commutative37.7%
hypot-define65.7%
Simplified65.7%
Taylor expanded in re around 0 29.9%
if 3.59999999999999994e-187 < re < 4.2999999999999998e-157 or 3.0000000000000001e-6 < re < 4.69999999999999984e75 or 1.62e93 < re Initial program 44.8%
sqr-neg44.8%
+-commutative44.8%
sqr-neg44.8%
+-commutative44.8%
distribute-rgt-in44.8%
cancel-sign-sub44.8%
distribute-rgt-out--44.8%
sub-neg44.8%
remove-double-neg44.8%
+-commutative44.8%
hypot-define98.7%
Simplified98.7%
Taylor expanded in re around inf 85.4%
*-commutative85.4%
unpow285.4%
rem-square-sqrt87.0%
associate-*r*87.0%
metadata-eval87.0%
*-lft-identity87.0%
Simplified87.0%
Final simplification45.9%
(FPCore (re im)
:precision binary64
(if (<= re 3.6e-187)
(* 0.5 (sqrt (* 2.0 im)))
(if (or (<= re 4.3e-157) (not (<= re 8.8e-5)))
(sqrt re)
(* 0.5 (sqrt (* 2.0 (+ re im)))))))
double code(double re, double im) {
double tmp;
if (re <= 3.6e-187) {
tmp = 0.5 * sqrt((2.0 * im));
} else if ((re <= 4.3e-157) || !(re <= 8.8e-5)) {
tmp = sqrt(re);
} else {
tmp = 0.5 * sqrt((2.0 * (re + im)));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= 3.6d-187) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else if ((re <= 4.3d-157) .or. (.not. (re <= 8.8d-5))) then
tmp = sqrt(re)
else
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 3.6e-187) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else if ((re <= 4.3e-157) || !(re <= 8.8e-5)) {
tmp = Math.sqrt(re);
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 3.6e-187: tmp = 0.5 * math.sqrt((2.0 * im)) elif (re <= 4.3e-157) or not (re <= 8.8e-5): tmp = math.sqrt(re) else: tmp = 0.5 * math.sqrt((2.0 * (re + im))) return tmp
function code(re, im) tmp = 0.0 if (re <= 3.6e-187) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); elseif ((re <= 4.3e-157) || !(re <= 8.8e-5)) tmp = sqrt(re); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 3.6e-187) tmp = 0.5 * sqrt((2.0 * im)); elseif ((re <= 4.3e-157) || ~((re <= 8.8e-5))) tmp = sqrt(re); else tmp = 0.5 * sqrt((2.0 * (re + im))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 3.6e-187], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 4.3e-157], N[Not[LessEqual[re, 8.8e-5]], $MachinePrecision]], N[Sqrt[re], $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 3.6 \cdot 10^{-187}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{elif}\;re \leq 4.3 \cdot 10^{-157} \lor \neg \left(re \leq 8.8 \cdot 10^{-5}\right):\\
\;\;\;\;\sqrt{re}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\end{array}
\end{array}
if re < 3.59999999999999994e-187Initial program 31.9%
sqr-neg31.9%
+-commutative31.9%
sqr-neg31.9%
+-commutative31.9%
distribute-rgt-in31.9%
cancel-sign-sub31.9%
distribute-rgt-out--31.9%
sub-neg31.9%
remove-double-neg31.9%
+-commutative31.9%
hypot-define58.7%
Simplified58.7%
Taylor expanded in re around 0 28.4%
if 3.59999999999999994e-187 < re < 4.2999999999999998e-157 or 8.7999999999999998e-5 < 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%
hypot-define98.7%
Simplified98.7%
Taylor expanded in re around inf 80.3%
*-commutative80.3%
unpow280.3%
rem-square-sqrt81.8%
associate-*r*81.8%
metadata-eval81.8%
*-lft-identity81.8%
Simplified81.8%
if 4.2999999999999998e-157 < re < 8.7999999999999998e-5Initial program 70.5%
sqr-neg70.5%
+-commutative70.5%
sqr-neg70.5%
+-commutative70.5%
distribute-rgt-in70.5%
cancel-sign-sub70.5%
distribute-rgt-out--70.5%
sub-neg70.5%
remove-double-neg70.5%
+-commutative70.5%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around 0 40.8%
Final simplification45.7%
(FPCore (re im) :precision binary64 (if (<= re -1e-310) (* 0.5 (sqrt 0.0)) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= -1e-310) {
tmp = 0.5 * sqrt(0.0);
} 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 <= (-1d-310)) then
tmp = 0.5d0 * sqrt(0.0d0)
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1e-310) {
tmp = 0.5 * Math.sqrt(0.0);
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1e-310: tmp = 0.5 * math.sqrt(0.0) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -1e-310) tmp = Float64(0.5 * sqrt(0.0)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1e-310) tmp = 0.5 * sqrt(0.0); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1e-310], N[(0.5 * N[Sqrt[0.0], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1 \cdot 10^{-310}:\\
\;\;\;\;0.5 \cdot \sqrt{0}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -9.999999999999969e-311Initial program 28.5%
sqr-neg28.5%
+-commutative28.5%
sqr-neg28.5%
+-commutative28.5%
distribute-rgt-in28.5%
cancel-sign-sub28.5%
distribute-rgt-out--28.5%
sub-neg28.5%
remove-double-neg28.5%
+-commutative28.5%
hypot-define52.8%
Simplified52.8%
add-cube-cbrt46.5%
hypot-define26.5%
fma-define26.4%
pow226.4%
hypot-define46.0%
Applied egg-rr46.0%
Taylor expanded in re around -inf 9.4%
associate-*r*9.4%
rem-cube-cbrt9.4%
metadata-eval9.4%
mul0-rgt9.4%
Simplified9.4%
if -9.999999999999969e-311 < re Initial program 51.9%
sqr-neg51.9%
+-commutative51.9%
sqr-neg51.9%
+-commutative51.9%
distribute-rgt-in51.9%
cancel-sign-sub51.9%
distribute-rgt-out--51.9%
sub-neg51.9%
remove-double-neg51.9%
+-commutative51.9%
hypot-define99.2%
Simplified99.2%
Taylor expanded in re around inf 59.6%
*-commutative59.6%
unpow259.6%
rem-square-sqrt60.7%
associate-*r*60.7%
metadata-eval60.7%
*-lft-identity60.7%
Simplified60.7%
Final simplification33.8%
(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 39.7%
sqr-neg39.7%
+-commutative39.7%
sqr-neg39.7%
+-commutative39.7%
distribute-rgt-in39.7%
cancel-sign-sub39.7%
distribute-rgt-out--39.7%
sub-neg39.7%
remove-double-neg39.7%
+-commutative39.7%
hypot-define74.9%
Simplified74.9%
Taylor expanded in re around inf 28.4%
*-commutative28.4%
unpow228.4%
rem-square-sqrt28.9%
associate-*r*28.9%
metadata-eval28.9%
*-lft-identity28.9%
Simplified28.9%
(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 2024089
(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)))))