
(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 7 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 -1.65e+109)
(pow
(*
(*
(cbrt 0.5)
(exp
(*
0.16666666666666666
(+ (log (* 0.5 (pow im 2.0))) (log (/ -1.0 re))))))
(cbrt (sqrt 2.0)))
3.0)
(sqrt (* 0.5 (+ re (hypot im re))))))
double code(double re, double im) {
double tmp;
if (re <= -1.65e+109) {
tmp = pow(((cbrt(0.5) * exp((0.16666666666666666 * (log((0.5 * pow(im, 2.0))) + log((-1.0 / re)))))) * cbrt(sqrt(2.0))), 3.0);
} else {
tmp = sqrt((0.5 * (re + hypot(im, re))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= -1.65e+109) {
tmp = Math.pow(((Math.cbrt(0.5) * Math.exp((0.16666666666666666 * (Math.log((0.5 * Math.pow(im, 2.0))) + Math.log((-1.0 / re)))))) * Math.cbrt(Math.sqrt(2.0))), 3.0);
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(im, re))));
}
return tmp;
}
function code(re, im) tmp = 0.0 if (re <= -1.65e+109) tmp = Float64(Float64(cbrt(0.5) * exp(Float64(0.16666666666666666 * Float64(log(Float64(0.5 * (im ^ 2.0))) + log(Float64(-1.0 / re)))))) * cbrt(sqrt(2.0))) ^ 3.0; else tmp = sqrt(Float64(0.5 * Float64(re + hypot(im, re)))); end return tmp end
code[re_, im_] := If[LessEqual[re, -1.65e+109], N[Power[N[(N[(N[Power[0.5, 1/3], $MachinePrecision] * N[Exp[N[(0.16666666666666666 * N[(N[Log[N[(0.5 * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Power[N[Sqrt[2.0], $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision], 3.0], $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 -1.65 \cdot 10^{+109}:\\
\;\;\;\;{\left(\left(\sqrt[3]{0.5} \cdot e^{0.16666666666666666 \cdot \left(\log \left(0.5 \cdot {im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right) \cdot \sqrt[3]{\sqrt{2}}\right)}^{3}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(im, re\right)\right)}\\
\end{array}
\end{array}
if re < -1.6499999999999999e109Initial program 6.5%
sqr-neg6.5%
+-commutative6.5%
sqr-neg6.5%
+-commutative6.5%
distribute-rgt-in6.5%
cancel-sign-sub6.5%
distribute-rgt-out--6.5%
sub-neg6.5%
remove-double-neg6.5%
+-commutative6.5%
hypot-define27.6%
Simplified27.6%
add-cube-cbrt27.4%
pow327.4%
*-commutative27.4%
Applied egg-rr27.4%
Taylor expanded in re around -inf 58.5%
if -1.6499999999999999e109 < re Initial program 51.8%
sqr-neg51.8%
+-commutative51.8%
sqr-neg51.8%
+-commutative51.8%
distribute-rgt-in51.8%
cancel-sign-sub51.8%
distribute-rgt-out--51.8%
sub-neg51.8%
remove-double-neg51.8%
+-commutative51.8%
hypot-define93.1%
Simplified93.1%
add-sqr-sqrt92.4%
sqrt-unprod93.1%
*-commutative93.1%
*-commutative93.1%
swap-sqr93.1%
add-sqr-sqrt93.1%
metadata-eval93.1%
Applied egg-rr93.1%
*-commutative93.1%
associate-*r*93.1%
metadata-eval93.1%
hypot-undefine51.8%
unpow251.8%
unpow251.8%
+-commutative51.8%
unpow251.8%
unpow251.8%
hypot-undefine93.1%
Simplified93.1%
Final simplification86.5%
(FPCore (re im) :precision binary64 (if (<= re -2.6e+109) (* 0.5 (sqrt (* 2.0 (/ (* (pow im 2.0) -0.5) re)))) (sqrt (* 0.5 (+ re (hypot im re))))))
double code(double re, double im) {
double tmp;
if (re <= -2.6e+109) {
tmp = 0.5 * sqrt((2.0 * ((pow(im, 2.0) * -0.5) / 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.6e+109) {
tmp = 0.5 * Math.sqrt((2.0 * ((Math.pow(im, 2.0) * -0.5) / re)));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(im, re))));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.6e+109: tmp = 0.5 * math.sqrt((2.0 * ((math.pow(im, 2.0) * -0.5) / re))) else: tmp = math.sqrt((0.5 * (re + math.hypot(im, re)))) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.6e+109) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(Float64((im ^ 2.0) * -0.5) / 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.6e+109) tmp = 0.5 * sqrt((2.0 * (((im ^ 2.0) * -0.5) / re))); else tmp = sqrt((0.5 * (re + hypot(im, re)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.6e+109], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[(N[Power[im, 2.0], $MachinePrecision] * -0.5), $MachinePrecision] / 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}\;re \leq -2.6 \cdot 10^{+109}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{{im}^{2} \cdot -0.5}{re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(im, re\right)\right)}\\
\end{array}
\end{array}
if re < -2.5999999999999998e109Initial program 6.5%
sqr-neg6.5%
+-commutative6.5%
sqr-neg6.5%
+-commutative6.5%
distribute-rgt-in6.5%
cancel-sign-sub6.5%
distribute-rgt-out--6.5%
sub-neg6.5%
remove-double-neg6.5%
+-commutative6.5%
hypot-define27.6%
Simplified27.6%
Taylor expanded in re around -inf 42.3%
*-commutative42.3%
associate-*l/42.3%
Simplified42.3%
if -2.5999999999999998e109 < re Initial program 51.8%
sqr-neg51.8%
+-commutative51.8%
sqr-neg51.8%
+-commutative51.8%
distribute-rgt-in51.8%
cancel-sign-sub51.8%
distribute-rgt-out--51.8%
sub-neg51.8%
remove-double-neg51.8%
+-commutative51.8%
hypot-define93.1%
Simplified93.1%
add-sqr-sqrt92.4%
sqrt-unprod93.1%
*-commutative93.1%
*-commutative93.1%
swap-sqr93.1%
add-sqr-sqrt93.1%
metadata-eval93.1%
Applied egg-rr93.1%
*-commutative93.1%
associate-*r*93.1%
metadata-eval93.1%
hypot-undefine51.8%
unpow251.8%
unpow251.8%
+-commutative51.8%
unpow251.8%
unpow251.8%
hypot-undefine93.1%
Simplified93.1%
Final simplification83.4%
(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 43.1%
sqr-neg43.1%
+-commutative43.1%
sqr-neg43.1%
+-commutative43.1%
distribute-rgt-in43.1%
cancel-sign-sub43.1%
distribute-rgt-out--43.1%
sub-neg43.1%
remove-double-neg43.1%
+-commutative43.1%
hypot-define80.6%
Simplified80.6%
add-sqr-sqrt79.9%
sqrt-unprod80.6%
*-commutative80.6%
*-commutative80.6%
swap-sqr80.6%
add-sqr-sqrt80.6%
metadata-eval80.6%
Applied egg-rr80.6%
*-commutative80.6%
associate-*r*80.6%
metadata-eval80.6%
hypot-undefine43.1%
unpow243.1%
unpow243.1%
+-commutative43.1%
unpow243.1%
unpow243.1%
hypot-undefine80.6%
Simplified80.6%
Final simplification80.6%
(FPCore (re im)
:precision binary64
(if (<= re -5.8e+93)
(* 0.5 (sqrt (* 2.0 (- re re))))
(if (<= re 390.0)
(* 0.5 (sqrt (* 2.0 (+ im (* re (+ 1.0 (* 0.5 (/ re im))))))))
(sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -5.8e+93) {
tmp = 0.5 * sqrt((2.0 * (re - re)));
} else if (re <= 390.0) {
tmp = 0.5 * sqrt((2.0 * (im + (re * (1.0 + (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 <= (-5.8d+93)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re - re)))
else if (re <= 390.0d0) then
tmp = 0.5d0 * sqrt((2.0d0 * (im + (re * (1.0d0 + (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 <= -5.8e+93) {
tmp = 0.5 * Math.sqrt((2.0 * (re - re)));
} else if (re <= 390.0) {
tmp = 0.5 * Math.sqrt((2.0 * (im + (re * (1.0 + (0.5 * (re / im)))))));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -5.8e+93: tmp = 0.5 * math.sqrt((2.0 * (re - re))) elif re <= 390.0: tmp = 0.5 * math.sqrt((2.0 * (im + (re * (1.0 + (0.5 * (re / im))))))) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -5.8e+93) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - re)))); elseif (re <= 390.0) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im + Float64(re * Float64(1.0 + 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 <= -5.8e+93) tmp = 0.5 * sqrt((2.0 * (re - re))); elseif (re <= 390.0) tmp = 0.5 * sqrt((2.0 * (im + (re * (1.0 + (0.5 * (re / im))))))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -5.8e+93], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 390.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(im + N[(re * N[(1.0 + N[(0.5 * N[(re / im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -5.8 \cdot 10^{+93}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - re\right)}\\
\mathbf{elif}\;re \leq 390:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im + re \cdot \left(1 + 0.5 \cdot \frac{re}{im}\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -5.7999999999999997e93Initial program 8.4%
Taylor expanded in re around -inf 19.8%
mul-1-neg19.8%
Simplified19.8%
if -5.7999999999999997e93 < re < 390Initial program 55.1%
sqr-neg55.1%
+-commutative55.1%
sqr-neg55.1%
+-commutative55.1%
distribute-rgt-in55.1%
cancel-sign-sub55.1%
distribute-rgt-out--55.1%
sub-neg55.1%
remove-double-neg55.1%
+-commutative55.1%
hypot-define90.3%
Simplified90.3%
Taylor expanded in re around 0 37.9%
if 390 < 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-define100.0%
Simplified100.0%
Taylor expanded in re around inf 77.6%
*-commutative77.6%
unpow277.6%
rem-square-sqrt79.0%
associate-*r*79.0%
metadata-eval79.0%
Simplified79.0%
Final simplification44.9%
(FPCore (re im) :precision binary64 (if (<= re -2.5e+94) (* 0.5 (sqrt (* 2.0 (- re re)))) (if (<= re 72.0) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -2.5e+94) {
tmp = 0.5 * sqrt((2.0 * (re - re)));
} else if (re <= 72.0) {
tmp = 0.5 * sqrt((2.0 * (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 <= (-2.5d+94)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re - re)))
else if (re <= 72.0d0) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.5e+94) {
tmp = 0.5 * Math.sqrt((2.0 * (re - re)));
} else if (re <= 72.0) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.5e+94: tmp = 0.5 * math.sqrt((2.0 * (re - re))) elif re <= 72.0: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.5e+94) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - re)))); elseif (re <= 72.0) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.5e+94) tmp = 0.5 * sqrt((2.0 * (re - re))); elseif (re <= 72.0) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.5e+94], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 72.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.5 \cdot 10^{+94}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - re\right)}\\
\mathbf{elif}\;re \leq 72:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -2.50000000000000005e94Initial program 8.4%
Taylor expanded in re around -inf 19.8%
mul-1-neg19.8%
Simplified19.8%
if -2.50000000000000005e94 < re < 72Initial program 55.1%
sqr-neg55.1%
+-commutative55.1%
sqr-neg55.1%
+-commutative55.1%
distribute-rgt-in55.1%
cancel-sign-sub55.1%
distribute-rgt-out--55.1%
sub-neg55.1%
remove-double-neg55.1%
+-commutative55.1%
hypot-define90.3%
Simplified90.3%
Taylor expanded in re around 0 38.7%
if 72 < 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-define100.0%
Simplified100.0%
Taylor expanded in re around inf 77.6%
*-commutative77.6%
unpow277.6%
rem-square-sqrt79.0%
associate-*r*79.0%
metadata-eval79.0%
Simplified79.0%
Final simplification45.3%
(FPCore (re im) :precision binary64 (if (<= re 235.0) (* 0.5 (sqrt (* im 2.0))) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= 235.0) {
tmp = 0.5 * sqrt((im * 2.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 <= 235.0d0) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 235.0) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 235.0: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 235.0) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 235.0) tmp = 0.5 * sqrt((im * 2.0)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 235.0], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 235:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 235Initial program 42.6%
sqr-neg42.6%
+-commutative42.6%
sqr-neg42.6%
+-commutative42.6%
distribute-rgt-in42.6%
cancel-sign-sub42.6%
distribute-rgt-out--42.6%
sub-neg42.6%
remove-double-neg42.6%
+-commutative42.6%
hypot-define73.8%
Simplified73.8%
Taylor expanded in re around 0 29.4%
if 235 < 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-define100.0%
Simplified100.0%
Taylor expanded in re around inf 77.6%
*-commutative77.6%
unpow277.6%
rem-square-sqrt79.0%
associate-*r*79.0%
metadata-eval79.0%
Simplified79.0%
Final simplification42.2%
(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 43.1%
sqr-neg43.1%
+-commutative43.1%
sqr-neg43.1%
+-commutative43.1%
distribute-rgt-in43.1%
cancel-sign-sub43.1%
distribute-rgt-out--43.1%
sub-neg43.1%
remove-double-neg43.1%
+-commutative43.1%
hypot-define80.6%
Simplified80.6%
Taylor expanded in re around inf 24.7%
*-commutative24.7%
unpow224.7%
rem-square-sqrt25.2%
associate-*r*25.2%
metadata-eval25.2%
Simplified25.2%
Final simplification25.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 2024050
(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)))))