
(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 5 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 (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0) (sqrt (* -0.25 (* im (/ im re)))) (sqrt (* 0.5 (+ re (hypot im re))))))
double code(double re, double im) {
double tmp;
if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) {
tmp = sqrt((-0.25 * (im * (im / re))));
} else {
tmp = sqrt((0.5 * (re + hypot(im, re))));
}
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 = Math.sqrt((-0.25 * (im * (im / re))));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(im, re))));
}
return tmp;
}
def code(re, im): tmp = 0 if math.sqrt((2.0 * (re + math.sqrt(((re * re) + (im * im)))))) <= 0.0: tmp = math.sqrt((-0.25 * (im * (im / re)))) else: tmp = math.sqrt((0.5 * (re + math.hypot(im, re)))) 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 = sqrt(Float64(-0.25 * Float64(im * Float64(im / 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 (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) tmp = sqrt((-0.25 * (im * (im / re)))); else tmp = sqrt((0.5 * (re + hypot(im, re)))); 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[Sqrt[N[(-0.25 * N[(im * N[(im / re), $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}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;\sqrt{-0.25 \cdot \left(im \cdot \frac{im}{re}\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(im, 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 12.4%
sqr-neg12.4%
+-commutative12.4%
sqr-neg12.4%
+-commutative12.4%
distribute-rgt-in12.4%
cancel-sign-sub12.4%
distribute-rgt-out--12.4%
sub-neg12.4%
remove-double-neg12.4%
+-commutative12.4%
Simplified12.4%
hypot-define12.4%
+-commutative12.4%
add-sqr-sqrt12.4%
sqrt-unprod12.4%
*-commutative12.4%
*-commutative12.4%
swap-sqr12.4%
Applied egg-rr12.4%
*-commutative12.4%
associate-*r*12.4%
metadata-eval12.4%
hypot-undefine12.4%
unpow212.4%
unpow212.4%
+-commutative12.4%
unpow212.4%
unpow212.4%
hypot-undefine12.4%
Simplified12.4%
Taylor expanded in re around -inf 64.3%
unpow264.3%
associate-/l*66.8%
Applied egg-rr66.8%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 48.5%
sqr-neg48.5%
+-commutative48.5%
sqr-neg48.5%
+-commutative48.5%
distribute-rgt-in48.5%
cancel-sign-sub48.5%
distribute-rgt-out--48.5%
sub-neg48.5%
remove-double-neg48.5%
+-commutative48.5%
Simplified88.3%
hypot-define48.5%
+-commutative48.5%
add-sqr-sqrt48.1%
sqrt-unprod48.5%
*-commutative48.5%
*-commutative48.5%
swap-sqr48.5%
Applied egg-rr88.3%
*-commutative88.3%
associate-*r*88.3%
metadata-eval88.3%
hypot-undefine48.5%
unpow248.5%
unpow248.5%
+-commutative48.5%
unpow248.5%
unpow248.5%
hypot-undefine88.3%
Simplified88.3%
Final simplification85.2%
(FPCore (re im) :precision binary64 (if (<= re -6e-38) (sqrt (* -0.25 (* im (/ im re)))) (if (<= re 1.1e-41) (sqrt (* 0.5 (+ re im))) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -6e-38) {
tmp = sqrt((-0.25 * (im * (im / re))));
} else if (re <= 1.1e-41) {
tmp = sqrt((0.5 * (re + 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 <= (-6d-38)) then
tmp = sqrt(((-0.25d0) * (im * (im / re))))
else if (re <= 1.1d-41) then
tmp = sqrt((0.5d0 * (re + im)))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -6e-38) {
tmp = Math.sqrt((-0.25 * (im * (im / re))));
} else if (re <= 1.1e-41) {
tmp = Math.sqrt((0.5 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -6e-38: tmp = math.sqrt((-0.25 * (im * (im / re)))) elif re <= 1.1e-41: tmp = math.sqrt((0.5 * (re + im))) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -6e-38) tmp = sqrt(Float64(-0.25 * Float64(im * Float64(im / re)))); elseif (re <= 1.1e-41) tmp = sqrt(Float64(0.5 * Float64(re + im))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -6e-38) tmp = sqrt((-0.25 * (im * (im / re)))); elseif (re <= 1.1e-41) tmp = sqrt((0.5 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -6e-38], N[Sqrt[N[(-0.25 * N[(im * N[(im / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[re, 1.1e-41], N[Sqrt[N[(0.5 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -6 \cdot 10^{-38}:\\
\;\;\;\;\sqrt{-0.25 \cdot \left(im \cdot \frac{im}{re}\right)}\\
\mathbf{elif}\;re \leq 1.1 \cdot 10^{-41}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -5.99999999999999977e-38Initial program 10.3%
sqr-neg10.3%
+-commutative10.3%
sqr-neg10.3%
+-commutative10.3%
distribute-rgt-in10.3%
cancel-sign-sub10.3%
distribute-rgt-out--10.3%
sub-neg10.3%
remove-double-neg10.3%
+-commutative10.3%
Simplified27.1%
hypot-define10.3%
+-commutative10.3%
add-sqr-sqrt10.2%
sqrt-unprod10.3%
*-commutative10.3%
*-commutative10.3%
swap-sqr10.3%
Applied egg-rr27.1%
*-commutative27.1%
associate-*r*27.1%
metadata-eval27.1%
hypot-undefine10.3%
unpow210.3%
unpow210.3%
+-commutative10.3%
unpow210.3%
unpow210.3%
hypot-undefine27.1%
Simplified27.1%
Taylor expanded in re around -inf 47.1%
unpow247.1%
associate-/l*50.0%
Applied egg-rr50.0%
if -5.99999999999999977e-38 < re < 1.1e-41Initial program 59.7%
sqr-neg59.7%
+-commutative59.7%
sqr-neg59.7%
+-commutative59.7%
distribute-rgt-in59.7%
cancel-sign-sub59.7%
distribute-rgt-out--59.7%
sub-neg59.7%
remove-double-neg59.7%
+-commutative59.7%
Simplified95.9%
hypot-define59.7%
+-commutative59.7%
add-sqr-sqrt59.1%
sqrt-unprod59.7%
*-commutative59.7%
*-commutative59.7%
swap-sqr59.7%
Applied egg-rr95.9%
*-commutative95.9%
associate-*r*95.9%
metadata-eval95.9%
hypot-undefine59.7%
unpow259.7%
unpow259.7%
+-commutative59.7%
unpow259.7%
unpow259.7%
hypot-undefine95.9%
Simplified95.9%
Taylor expanded in re around 0 46.9%
distribute-lft-out46.9%
Simplified46.9%
if 1.1e-41 < 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.3%
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 76.4%
Final simplification55.4%
(FPCore (re im) :precision binary64 (if (<= re 2.4e-42) (sqrt (* im 0.5)) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= 2.4e-42) {
tmp = sqrt((im * 0.5));
} 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 <= 2.4d-42) then
tmp = sqrt((im * 0.5d0))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 2.4e-42) {
tmp = Math.sqrt((im * 0.5));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 2.4e-42: tmp = math.sqrt((im * 0.5)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 2.4e-42) tmp = sqrt(Float64(im * 0.5)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 2.4e-42) tmp = sqrt((im * 0.5)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 2.4e-42], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 2.4 \cdot 10^{-42}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 2.40000000000000003e-42Initial program 40.7%
sqr-neg40.7%
+-commutative40.7%
sqr-neg40.7%
+-commutative40.7%
distribute-rgt-in40.7%
cancel-sign-sub40.7%
distribute-rgt-out--40.7%
sub-neg40.7%
remove-double-neg40.7%
+-commutative40.7%
Simplified69.5%
hypot-define40.7%
+-commutative40.7%
add-sqr-sqrt40.3%
sqrt-unprod40.7%
*-commutative40.7%
*-commutative40.7%
swap-sqr40.7%
Applied egg-rr69.5%
*-commutative69.5%
associate-*r*69.5%
metadata-eval69.5%
hypot-undefine40.7%
unpow240.7%
unpow240.7%
+-commutative40.7%
unpow240.7%
unpow240.7%
hypot-undefine69.5%
Simplified69.5%
Taylor expanded in re around 0 31.5%
*-commutative31.5%
Simplified31.5%
if 2.40000000000000003e-42 < 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.3%
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 76.4%
(FPCore (re im) :precision binary64 (if (<= re -1e-311) 0.0 (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= -1e-311) {
tmp = 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-311)) then
tmp = 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-311) {
tmp = 0.0;
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1e-311: tmp = 0.0 else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -1e-311) tmp = 0.0; else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1e-311) tmp = 0.0; else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1e-311], 0.0, N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1 \cdot 10^{-311}:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -9.99999999999948e-312Initial program 34.2%
sqr-neg34.2%
+-commutative34.2%
sqr-neg34.2%
+-commutative34.2%
distribute-rgt-in34.2%
cancel-sign-sub34.2%
distribute-rgt-out--34.2%
sub-neg34.2%
remove-double-neg34.2%
+-commutative34.2%
Simplified57.1%
hypot-define34.2%
+-commutative34.2%
add-sqr-sqrt33.9%
sqrt-unprod34.2%
*-commutative34.2%
*-commutative34.2%
swap-sqr34.2%
Applied egg-rr57.1%
*-commutative57.1%
associate-*r*57.1%
metadata-eval57.1%
hypot-undefine34.2%
unpow234.2%
unpow234.2%
+-commutative34.2%
unpow234.2%
unpow234.2%
hypot-undefine57.1%
Simplified57.1%
Taylor expanded in re around -inf 7.5%
neg-mul-17.5%
Simplified7.5%
Taylor expanded in re around 0 7.5%
if -9.99999999999948e-312 < re Initial program 53.4%
sqr-neg53.4%
+-commutative53.4%
sqr-neg53.4%
+-commutative53.4%
distribute-rgt-in53.4%
cancel-sign-sub53.4%
distribute-rgt-out--53.4%
sub-neg53.4%
remove-double-neg53.4%
+-commutative53.4%
Simplified100.0%
hypot-define53.4%
+-commutative53.4%
add-sqr-sqrt53.0%
sqrt-unprod53.4%
*-commutative53.4%
*-commutative53.4%
swap-sqr53.4%
Applied egg-rr100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine53.4%
unpow253.4%
unpow253.4%
+-commutative53.4%
unpow253.4%
unpow253.4%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around inf 54.8%
(FPCore (re im) :precision binary64 0.0)
double code(double re, double im) {
return 0.0;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.0d0
end function
public static double code(double re, double im) {
return 0.0;
}
def code(re, im): return 0.0
function code(re, im) return 0.0 end
function tmp = code(re, im) tmp = 0.0; end
code[re_, im_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 43.3%
sqr-neg43.3%
+-commutative43.3%
sqr-neg43.3%
+-commutative43.3%
distribute-rgt-in43.3%
cancel-sign-sub43.3%
distribute-rgt-out--43.3%
sub-neg43.3%
remove-double-neg43.3%
+-commutative43.3%
Simplified77.3%
hypot-define43.3%
+-commutative43.3%
add-sqr-sqrt42.9%
sqrt-unprod43.3%
*-commutative43.3%
*-commutative43.3%
swap-sqr43.3%
Applied egg-rr77.3%
*-commutative77.3%
associate-*r*77.3%
metadata-eval77.3%
hypot-undefine43.3%
unpow243.3%
unpow243.3%
+-commutative43.3%
unpow243.3%
unpow243.3%
hypot-undefine77.3%
Simplified77.3%
Taylor expanded in re around -inf 5.3%
neg-mul-15.3%
Simplified5.3%
Taylor expanded in re around 0 5.3%
(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 2024191
(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)))))