
(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}
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0) (* 0.5 (exp (* 0.5 (+ (log (/ -1.0 re)) (* 2.0 (log im)))))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) {
tmp = 0.5 * exp((0.5 * (log((-1.0 / re)) + (2.0 * log(im)))));
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
return tmp;
}
im = Math.abs(im);
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.exp((0.5 * (Math.log((-1.0 / re)) + (2.0 * Math.log(im)))));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if math.sqrt((2.0 * (re + math.sqrt(((re * re) + (im * im)))))) <= 0.0: tmp = 0.5 * math.exp((0.5 * (math.log((-1.0 / re)) + (2.0 * math.log(im))))) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) return tmp
im = abs(im) 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 * exp(Float64(0.5 * Float64(log(Float64(-1.0 / re)) + Float64(2.0 * log(im)))))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) tmp = 0.5 * exp((0.5 * (log((-1.0 / re)) + (2.0 * log(im))))); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function 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[Exp[N[(0.5 * N[(N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision] + N[(2.0 * N[Log[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $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}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot e^{0.5 \cdot \left(\log \left(\frac{-1}{re}\right) + 2 \cdot \log im\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 8.5%
sqr-neg8.5%
+-commutative8.5%
sqr-neg8.5%
distribute-rgt-in8.5%
cancel-sign-sub8.5%
distribute-rgt-out--8.5%
sub-neg8.5%
remove-double-neg8.5%
hypot-def8.5%
Simplified8.5%
Taylor expanded in re around -inf 39.4%
+-commutative39.4%
mul-1-neg39.4%
unsub-neg39.4%
*-commutative39.4%
unpow239.4%
associate-/l*40.5%
Simplified40.5%
pow1/240.5%
metadata-eval40.5%
pow-to-exp38.9%
associate-/r/38.9%
*-commutative38.9%
div-inv38.9%
associate-*l*38.9%
pow-flip38.9%
metadata-eval38.9%
metadata-eval38.9%
Applied egg-rr38.9%
Taylor expanded in im around 0 40.1%
distribute-neg-frac40.1%
metadata-eval40.1%
Simplified40.1%
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 45.8%
sqr-neg45.8%
+-commutative45.8%
sqr-neg45.8%
distribute-rgt-in45.8%
cancel-sign-sub45.8%
distribute-rgt-out--45.8%
sub-neg45.8%
remove-double-neg45.8%
hypot-def89.0%
Simplified89.0%
Final simplification83.8%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -7.4e+42) (* 0.5 (sqrt (* (- im) (/ im re)))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -7.4e+42) {
tmp = 0.5 * sqrt((-im * (im / re)));
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
return tmp;
}
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -7.4e+42) {
tmp = 0.5 * Math.sqrt((-im * (im / re)));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -7.4e+42: tmp = 0.5 * math.sqrt((-im * (im / re))) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -7.4e+42) tmp = Float64(0.5 * sqrt(Float64(Float64(-im) * Float64(im / re)))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -7.4e+42) tmp = 0.5 * sqrt((-im * (im / re))); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -7.4e+42], N[(0.5 * N[Sqrt[N[((-im) * N[(im / re), $MachinePrecision]), $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}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -7.4 \cdot 10^{+42}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -7.39999999999999993e42Initial program 6.7%
sqr-neg6.7%
+-commutative6.7%
sqr-neg6.7%
distribute-rgt-in6.7%
cancel-sign-sub6.7%
distribute-rgt-out--6.7%
sub-neg6.7%
remove-double-neg6.7%
hypot-def40.8%
Simplified40.8%
Taylor expanded in re around -inf 40.9%
+-commutative40.9%
mul-1-neg40.9%
unsub-neg40.9%
*-commutative40.9%
unpow240.9%
associate-/l*40.9%
Simplified40.9%
Taylor expanded in im around 0 59.5%
mul-1-neg59.5%
unpow259.5%
associate-*r/66.3%
distribute-lft-neg-out66.3%
Simplified66.3%
if -7.39999999999999993e42 < re Initial program 51.9%
sqr-neg51.9%
+-commutative51.9%
sqr-neg51.9%
distribute-rgt-in51.9%
cancel-sign-sub51.9%
distribute-rgt-out--51.9%
sub-neg51.9%
remove-double-neg51.9%
hypot-def91.9%
Simplified91.9%
Final simplification86.2%
NOTE: im should be positive before calling this function
(FPCore (re im)
:precision binary64
(if (<= re -1.5e+45)
(* 0.5 (sqrt (* (- im) (/ im re))))
(if (<= re 4.6e+24)
(* 0.5 (sqrt (* 2.0 (+ re im))))
(* 0.5 (* 2.0 (sqrt re))))))im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -1.5e+45) {
tmp = 0.5 * sqrt((-im * (im / re)));
} else if (re <= 4.6e+24) {
tmp = 0.5 * sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-1.5d+45)) then
tmp = 0.5d0 * sqrt((-im * (im / re)))
else if (re <= 4.6d+24) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -1.5e+45) {
tmp = 0.5 * Math.sqrt((-im * (im / re)));
} else if (re <= 4.6e+24) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -1.5e+45: tmp = 0.5 * math.sqrt((-im * (im / re))) elif re <= 4.6e+24: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -1.5e+45) tmp = Float64(0.5 * sqrt(Float64(Float64(-im) * Float64(im / re)))); elseif (re <= 4.6e+24) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.5e+45) tmp = 0.5 * sqrt((-im * (im / re))); elseif (re <= 4.6e+24) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -1.5e+45], N[(0.5 * N[Sqrt[N[((-im) * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 4.6e+24], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.5 \cdot 10^{+45}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\
\mathbf{elif}\;re \leq 4.6 \cdot 10^{+24}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -1.50000000000000005e45Initial program 6.7%
sqr-neg6.7%
+-commutative6.7%
sqr-neg6.7%
distribute-rgt-in6.7%
cancel-sign-sub6.7%
distribute-rgt-out--6.7%
sub-neg6.7%
remove-double-neg6.7%
hypot-def40.8%
Simplified40.8%
Taylor expanded in re around -inf 40.9%
+-commutative40.9%
mul-1-neg40.9%
unsub-neg40.9%
*-commutative40.9%
unpow240.9%
associate-/l*40.9%
Simplified40.9%
Taylor expanded in im around 0 59.5%
mul-1-neg59.5%
unpow259.5%
associate-*r/66.3%
distribute-lft-neg-out66.3%
Simplified66.3%
if -1.50000000000000005e45 < re < 4.5999999999999998e24Initial program 55.6%
sqr-neg55.6%
+-commutative55.6%
sqr-neg55.6%
distribute-rgt-in55.6%
cancel-sign-sub55.6%
distribute-rgt-out--55.6%
sub-neg55.6%
remove-double-neg55.6%
hypot-def88.5%
Simplified88.5%
Taylor expanded in re around 0 41.2%
distribute-lft-out41.2%
*-commutative41.2%
Simplified41.2%
if 4.5999999999999998e24 < re Initial program 43.2%
sqr-neg43.2%
+-commutative43.2%
sqr-neg43.2%
distribute-rgt-in43.2%
cancel-sign-sub43.2%
distribute-rgt-out--43.2%
sub-neg43.2%
remove-double-neg43.2%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 84.2%
*-commutative84.2%
unpow284.2%
rem-square-sqrt86.0%
Simplified86.0%
Final simplification57.1%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re 43000000.0) (* 0.5 (sqrt (* 2.0 im))) (* 0.5 (* 2.0 (sqrt re)))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= 43000000.0) {
tmp = 0.5 * sqrt((2.0 * im));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= 43000000.0d0) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= 43000000.0) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= 43000000.0: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= 43000000.0) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= 43000000.0) tmp = 0.5 * sqrt((2.0 * im)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, 43000000.0], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq 43000000:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < 4.3e7Initial program 40.7%
sqr-neg40.7%
+-commutative40.7%
sqr-neg40.7%
distribute-rgt-in40.7%
cancel-sign-sub40.7%
distribute-rgt-out--40.7%
sub-neg40.7%
remove-double-neg40.7%
hypot-def74.1%
Simplified74.1%
Taylor expanded in re around 0 31.6%
*-commutative31.6%
Simplified31.6%
if 4.3e7 < re Initial program 45.3%
sqr-neg45.3%
+-commutative45.3%
sqr-neg45.3%
distribute-rgt-in45.3%
cancel-sign-sub45.3%
distribute-rgt-out--45.3%
sub-neg45.3%
remove-double-neg45.3%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 82.2%
*-commutative82.2%
unpow282.2%
rem-square-sqrt83.9%
Simplified83.9%
Final simplification44.5%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 im))))
im = abs(im);
double code(double re, double im) {
return 0.5 * sqrt((2.0 * im));
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * im))
end function
im = Math.abs(im);
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * im));
}
im = abs(im) def code(re, im): return 0.5 * math.sqrt((2.0 * im))
im = abs(im) function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * im))) end
im = abs(im) function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * im)); end
NOTE: im should be positive before calling this function code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Initial program 41.9%
sqr-neg41.9%
+-commutative41.9%
sqr-neg41.9%
distribute-rgt-in41.9%
cancel-sign-sub41.9%
distribute-rgt-out--41.9%
sub-neg41.9%
remove-double-neg41.9%
hypot-def80.5%
Simplified80.5%
Taylor expanded in re around 0 26.6%
*-commutative26.6%
Simplified26.6%
Final simplification26.6%
(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 2023275
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:herbie-target
(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)))))