
(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 (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0) (* 0.5 (exp (* 0.5 (+ (log (/ -1.0 re)) (log (pow im 2.0)))))) (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 = 0.5 * exp((0.5 * (log((-1.0 / re)) + log(pow(im, 2.0)))));
} 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 = 0.5 * Math.exp((0.5 * (Math.log((-1.0 / re)) + Math.log(Math.pow(im, 2.0)))));
} 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 = 0.5 * math.exp((0.5 * (math.log((-1.0 / re)) + math.log(math.pow(im, 2.0))))) 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 = Float64(0.5 * exp(Float64(0.5 * Float64(log(Float64(-1.0 / re)) + log((im ^ 2.0)))))); 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 = 0.5 * exp((0.5 * (log((-1.0 / re)) + log((im ^ 2.0))))); 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[(0.5 * N[Exp[N[(0.5 * N[(N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision] + N[Log[N[Power[im, 2.0], $MachinePrecision]], $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}\;\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) + \log \left({im}^{2}\right)\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 18.6%
sqr-neg18.6%
+-commutative18.6%
sqr-neg18.6%
+-commutative18.6%
distribute-rgt-in18.6%
cancel-sign-sub18.6%
distribute-rgt-out--18.6%
sub-neg18.6%
remove-double-neg18.6%
+-commutative18.6%
hypot-define18.6%
Simplified18.6%
pow1/218.6%
hypot-define18.6%
+-commutative18.6%
pow-to-exp18.6%
+-commutative18.6%
hypot-define18.6%
Applied egg-rr18.6%
Taylor expanded in re around -inf 67.3%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 41.5%
sqr-neg41.5%
+-commutative41.5%
sqr-neg41.5%
+-commutative41.5%
distribute-rgt-in41.5%
cancel-sign-sub41.5%
distribute-rgt-out--41.5%
sub-neg41.5%
remove-double-neg41.5%
+-commutative41.5%
hypot-define90.8%
Simplified90.8%
*-commutative90.8%
hypot-define41.5%
+-commutative41.5%
*-commutative41.5%
add-sqr-sqrt41.3%
sqrt-unprod41.5%
*-commutative41.5%
*-commutative41.5%
swap-sqr41.5%
Applied egg-rr90.8%
*-commutative90.8%
associate-*r*91.2%
metadata-eval91.2%
hypot-undefine41.5%
unpow241.5%
unpow241.5%
+-commutative41.5%
unpow241.5%
unpow241.5%
hypot-undefine91.2%
Simplified91.2%
Final simplification89.1%
(FPCore (re im) :precision binary64 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0) (* 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 (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) {
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 (Math.sqrt((2.0 * (re + Math.sqrt(((re * re) + (im * im)))))) <= 0.0) {
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 math.sqrt((2.0 * (re + math.sqrt(((re * re) + (im * im)))))) <= 0.0: 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 (sqrt(Float64(2.0 * Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))))) <= 0.0) 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 (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) 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[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[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}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;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 (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 18.6%
sqr-neg18.6%
+-commutative18.6%
sqr-neg18.6%
+-commutative18.6%
distribute-rgt-in18.6%
cancel-sign-sub18.6%
distribute-rgt-out--18.6%
sub-neg18.6%
remove-double-neg18.6%
+-commutative18.6%
hypot-define18.6%
Simplified18.6%
Taylor expanded in re around -inf 64.6%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 41.5%
sqr-neg41.5%
+-commutative41.5%
sqr-neg41.5%
+-commutative41.5%
distribute-rgt-in41.5%
cancel-sign-sub41.5%
distribute-rgt-out--41.5%
sub-neg41.5%
remove-double-neg41.5%
+-commutative41.5%
hypot-define90.8%
Simplified90.8%
*-commutative90.8%
hypot-define41.5%
+-commutative41.5%
*-commutative41.5%
add-sqr-sqrt41.3%
sqrt-unprod41.5%
*-commutative41.5%
*-commutative41.5%
swap-sqr41.5%
Applied egg-rr90.8%
*-commutative90.8%
associate-*r*91.2%
metadata-eval91.2%
hypot-undefine41.5%
unpow241.5%
unpow241.5%
+-commutative41.5%
unpow241.5%
unpow241.5%
hypot-undefine91.2%
Simplified91.2%
Final simplification88.8%
(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.5%
sqr-neg39.5%
+-commutative39.5%
sqr-neg39.5%
+-commutative39.5%
distribute-rgt-in39.5%
cancel-sign-sub39.5%
distribute-rgt-out--39.5%
sub-neg39.5%
remove-double-neg39.5%
+-commutative39.5%
hypot-define84.3%
Simplified84.3%
*-commutative84.3%
hypot-define39.5%
+-commutative39.5%
*-commutative39.5%
add-sqr-sqrt39.2%
sqrt-unprod39.5%
*-commutative39.5%
*-commutative39.5%
swap-sqr39.5%
Applied egg-rr84.3%
*-commutative84.3%
associate-*r*84.7%
metadata-eval84.7%
hypot-undefine39.5%
unpow239.5%
unpow239.5%
+-commutative39.5%
unpow239.5%
unpow239.5%
hypot-undefine84.7%
Simplified84.7%
Final simplification84.7%
(FPCore (re im) :precision binary64 (if (<= re -2.4e+175) (* 0.5 (sqrt (* 2.0 (- re re)))) (if (<= re 7.4e+85) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -2.4e+175) {
tmp = 0.5 * sqrt((2.0 * (re - re)));
} else if (re <= 7.4e+85) {
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.4d+175)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re - re)))
else if (re <= 7.4d+85) 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.4e+175) {
tmp = 0.5 * Math.sqrt((2.0 * (re - re)));
} else if (re <= 7.4e+85) {
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.4e+175: tmp = 0.5 * math.sqrt((2.0 * (re - re))) elif re <= 7.4e+85: 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.4e+175) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - re)))); elseif (re <= 7.4e+85) 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.4e+175) tmp = 0.5 * sqrt((2.0 * (re - re))); elseif (re <= 7.4e+85) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.4e+175], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 7.4e+85], 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.4 \cdot 10^{+175}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - re\right)}\\
\mathbf{elif}\;re \leq 7.4 \cdot 10^{+85}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -2.4e175Initial program 2.7%
Taylor expanded in re around -inf 31.9%
mul-1-neg31.9%
Simplified31.9%
if -2.4e175 < re < 7.4000000000000004e85Initial program 46.3%
sqr-neg46.3%
+-commutative46.3%
sqr-neg46.3%
+-commutative46.3%
distribute-rgt-in46.3%
cancel-sign-sub46.3%
distribute-rgt-out--46.3%
sub-neg46.3%
remove-double-neg46.3%
+-commutative46.3%
hypot-define85.6%
Simplified85.6%
Taylor expanded in re around 0 39.8%
if 7.4000000000000004e85 < re Initial 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%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around inf 79.7%
*-commutative79.7%
unpow279.7%
rem-square-sqrt81.2%
associate-*r*81.2%
metadata-eval81.2%
*-lft-identity81.2%
Simplified81.2%
Final simplification46.7%
(FPCore (re im) :precision binary64 (if (<= re 8e+85) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= 8e+85) {
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 <= 8d+85) 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 <= 8e+85) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 8e+85: 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 <= 8e+85) 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 <= 8e+85) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 8e+85], 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 8 \cdot 10^{+85}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 8.0000000000000001e85Initial 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%
hypot-define80.7%
Simplified80.7%
Taylor expanded in re around 0 35.6%
if 8.0000000000000001e85 < re Initial 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%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around inf 79.7%
*-commutative79.7%
unpow279.7%
rem-square-sqrt81.2%
associate-*r*81.2%
metadata-eval81.2%
*-lft-identity81.2%
Simplified81.2%
Final simplification44.1%
(FPCore (re im) :precision binary64 (if (<= re 7e+85) (* 0.5 (sqrt (* 2.0 im))) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= 7e+85) {
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 <= 7d+85) 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 <= 7e+85) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 7e+85: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 7e+85) 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 <= 7e+85) tmp = 0.5 * sqrt((2.0 * im)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 7e+85], 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 7 \cdot 10^{+85}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 7.0000000000000001e85Initial 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%
hypot-define80.7%
Simplified80.7%
Taylor expanded in re around 0 34.4%
if 7.0000000000000001e85 < re Initial 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%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around inf 79.7%
*-commutative79.7%
unpow279.7%
rem-square-sqrt81.2%
associate-*r*81.2%
metadata-eval81.2%
*-lft-identity81.2%
Simplified81.2%
Final simplification43.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 39.5%
sqr-neg39.5%
+-commutative39.5%
sqr-neg39.5%
+-commutative39.5%
distribute-rgt-in39.5%
cancel-sign-sub39.5%
distribute-rgt-out--39.5%
sub-neg39.5%
remove-double-neg39.5%
+-commutative39.5%
hypot-define84.3%
Simplified84.3%
Taylor expanded in re around inf 24.6%
*-commutative24.6%
unpow224.6%
rem-square-sqrt25.0%
associate-*r*25.0%
metadata-eval25.0%
*-lft-identity25.0%
Simplified25.0%
Final simplification25.0%
(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 2024072
(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)))))