
(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 -5.6e+14) (* 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 (re <= -5.6e+14) {
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 (re <= -5.6e+14) {
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 re <= -5.6e+14: 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 (re <= -5.6e+14) 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 (re <= -5.6e+14) 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[re, -5.6e+14], 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}\;re \leq -5.6 \cdot 10^{+14}:\\
\;\;\;\;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 re < -5.6e14Initial program 16.9%
sqr-neg16.9%
+-commutative16.9%
sqr-neg16.9%
+-commutative16.9%
distribute-rgt-in16.9%
cancel-sign-sub16.9%
distribute-rgt-out--16.9%
sub-neg16.9%
remove-double-neg16.9%
+-commutative16.9%
hypot-define39.9%
Simplified39.9%
pow1/239.9%
hypot-define16.9%
+-commutative16.9%
pow-to-exp16.3%
+-commutative16.3%
hypot-define38.0%
Applied egg-rr38.0%
Taylor expanded in re around -inf 58.4%
if -5.6e14 < re Initial program 51.0%
sqr-neg51.0%
+-commutative51.0%
sqr-neg51.0%
+-commutative51.0%
distribute-rgt-in51.0%
cancel-sign-sub51.0%
distribute-rgt-out--51.0%
sub-neg51.0%
remove-double-neg51.0%
+-commutative51.0%
hypot-define92.6%
Simplified92.6%
*-commutative92.6%
hypot-define51.0%
+-commutative51.0%
*-commutative51.0%
add-sqr-sqrt50.6%
sqrt-unprod51.0%
*-commutative51.0%
*-commutative51.0%
swap-sqr51.0%
Applied egg-rr92.6%
*-commutative92.6%
associate-*r*92.6%
metadata-eval92.6%
hypot-undefine51.0%
unpow251.0%
unpow251.0%
+-commutative51.0%
unpow251.0%
unpow251.0%
hypot-undefine92.6%
Simplified92.6%
Final simplification83.5%
(FPCore (re im) :precision binary64 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0) (sqrt (* 0.5 (* -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 = sqrt((0.5 * (-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 = Math.sqrt((0.5 * (-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 = math.sqrt((0.5 * (-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 = sqrt(Float64(0.5 * 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 = sqrt((0.5 * (-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[Sqrt[N[(0.5 * N[(-0.5 * N[(N[Power[im, 2.0], $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}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;\sqrt{0.5 \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%
*-commutative18.6%
hypot-define18.6%
+-commutative18.6%
*-commutative18.6%
add-sqr-sqrt18.6%
sqrt-unprod18.6%
*-commutative18.6%
*-commutative18.6%
swap-sqr18.6%
Applied egg-rr18.6%
*-commutative18.6%
associate-*r*18.6%
metadata-eval18.6%
hypot-undefine18.6%
unpow218.6%
unpow218.6%
+-commutative18.6%
unpow218.6%
unpow218.6%
hypot-undefine18.6%
Simplified18.6%
Taylor expanded in re around -inf 55.8%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 45.1%
sqr-neg45.1%
+-commutative45.1%
sqr-neg45.1%
+-commutative45.1%
distribute-rgt-in45.1%
cancel-sign-sub45.1%
distribute-rgt-out--45.1%
sub-neg45.1%
remove-double-neg45.1%
+-commutative45.1%
hypot-define86.9%
Simplified86.9%
*-commutative86.9%
hypot-define45.1%
+-commutative45.1%
*-commutative45.1%
add-sqr-sqrt44.8%
sqrt-unprod45.1%
*-commutative45.1%
*-commutative45.1%
swap-sqr45.1%
Applied egg-rr86.9%
*-commutative86.9%
associate-*r*86.9%
metadata-eval86.9%
hypot-undefine45.1%
unpow245.1%
unpow245.1%
+-commutative45.1%
unpow245.1%
unpow245.1%
hypot-undefine86.9%
Simplified86.9%
Final simplification83.1%
(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 41.9%
sqr-neg41.9%
+-commutative41.9%
sqr-neg41.9%
+-commutative41.9%
distribute-rgt-in41.9%
cancel-sign-sub41.9%
distribute-rgt-out--41.9%
sub-neg41.9%
remove-double-neg41.9%
+-commutative41.9%
hypot-define78.6%
Simplified78.6%
*-commutative78.6%
hypot-define41.9%
+-commutative41.9%
*-commutative41.9%
add-sqr-sqrt41.6%
sqrt-unprod41.9%
*-commutative41.9%
*-commutative41.9%
swap-sqr41.9%
Applied egg-rr78.6%
*-commutative78.6%
associate-*r*78.6%
metadata-eval78.6%
hypot-undefine41.9%
unpow241.9%
unpow241.9%
+-commutative41.9%
unpow241.9%
unpow241.9%
hypot-undefine78.6%
Simplified78.6%
(FPCore (re im) :precision binary64 (if (or (<= re 8.3e-200) (and (not (<= re 1.3e-155)) (<= re 8.5e-65))) (sqrt (* 0.5 im)) (sqrt re)))
double code(double re, double im) {
double tmp;
if ((re <= 8.3e-200) || (!(re <= 1.3e-155) && (re <= 8.5e-65))) {
tmp = sqrt((0.5 * 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 <= 8.3d-200) .or. (.not. (re <= 1.3d-155)) .and. (re <= 8.5d-65)) then
tmp = sqrt((0.5d0 * im))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if ((re <= 8.3e-200) || (!(re <= 1.3e-155) && (re <= 8.5e-65))) {
tmp = Math.sqrt((0.5 * im));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if (re <= 8.3e-200) or (not (re <= 1.3e-155) and (re <= 8.5e-65)): tmp = math.sqrt((0.5 * im)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if ((re <= 8.3e-200) || (!(re <= 1.3e-155) && (re <= 8.5e-65))) tmp = sqrt(Float64(0.5 * im)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if ((re <= 8.3e-200) || (~((re <= 1.3e-155)) && (re <= 8.5e-65))) tmp = sqrt((0.5 * im)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[Or[LessEqual[re, 8.3e-200], And[N[Not[LessEqual[re, 1.3e-155]], $MachinePrecision], LessEqual[re, 8.5e-65]]], N[Sqrt[N[(0.5 * im), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 8.3 \cdot 10^{-200} \lor \neg \left(re \leq 1.3 \cdot 10^{-155}\right) \land re \leq 8.5 \cdot 10^{-65}:\\
\;\;\;\;\sqrt{0.5 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 8.3000000000000003e-200 or 1.30000000000000004e-155 < re < 8.5000000000000003e-65Initial program 39.8%
sqr-neg39.8%
+-commutative39.8%
sqr-neg39.8%
+-commutative39.8%
distribute-rgt-in39.8%
cancel-sign-sub39.8%
distribute-rgt-out--39.8%
sub-neg39.8%
remove-double-neg39.8%
+-commutative39.8%
hypot-define69.1%
Simplified69.1%
*-commutative69.1%
hypot-define39.8%
+-commutative39.8%
*-commutative39.8%
add-sqr-sqrt39.5%
sqrt-unprod39.8%
*-commutative39.8%
*-commutative39.8%
swap-sqr39.8%
Applied egg-rr69.1%
*-commutative69.1%
associate-*r*69.1%
metadata-eval69.1%
hypot-undefine39.8%
unpow239.8%
unpow239.8%
+-commutative39.8%
unpow239.8%
unpow239.8%
hypot-undefine69.1%
Simplified69.1%
Taylor expanded in re around 0 31.0%
if 8.3000000000000003e-200 < re < 1.30000000000000004e-155 or 8.5000000000000003e-65 < re Initial program 46.7%
sqr-neg46.7%
+-commutative46.7%
sqr-neg46.7%
+-commutative46.7%
distribute-rgt-in46.7%
cancel-sign-sub46.7%
distribute-rgt-out--46.7%
sub-neg46.7%
remove-double-neg46.7%
+-commutative46.7%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around inf 71.1%
*-commutative71.1%
unpow271.1%
rem-square-sqrt72.5%
associate-*r*72.5%
metadata-eval72.5%
*-lft-identity72.5%
Simplified72.5%
Final simplification43.8%
(FPCore (re im) :precision binary64 (if (<= im 2.7e-147) (sqrt re) (sqrt (* 0.5 (+ re im)))))
double code(double re, double im) {
double tmp;
if (im <= 2.7e-147) {
tmp = sqrt(re);
} else {
tmp = sqrt((0.5 * (re + im)));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 2.7d-147) then
tmp = sqrt(re)
else
tmp = sqrt((0.5d0 * (re + im)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 2.7e-147) {
tmp = Math.sqrt(re);
} else {
tmp = Math.sqrt((0.5 * (re + im)));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 2.7e-147: tmp = math.sqrt(re) else: tmp = math.sqrt((0.5 * (re + im))) return tmp
function code(re, im) tmp = 0.0 if (im <= 2.7e-147) tmp = sqrt(re); else tmp = sqrt(Float64(0.5 * Float64(re + im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 2.7e-147) tmp = sqrt(re); else tmp = sqrt((0.5 * (re + im))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 2.7e-147], N[Sqrt[re], $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 2.7 \cdot 10^{-147}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + im\right)}\\
\end{array}
\end{array}
if im < 2.6999999999999999e-147Initial program 40.3%
sqr-neg40.3%
+-commutative40.3%
sqr-neg40.3%
+-commutative40.3%
distribute-rgt-in40.3%
cancel-sign-sub40.3%
distribute-rgt-out--40.3%
sub-neg40.3%
remove-double-neg40.3%
+-commutative40.3%
hypot-define76.1%
Simplified76.1%
Taylor expanded in re around inf 29.1%
*-commutative29.1%
unpow229.1%
rem-square-sqrt29.7%
associate-*r*29.7%
metadata-eval29.7%
*-lft-identity29.7%
Simplified29.7%
if 2.6999999999999999e-147 < im 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-define82.9%
Simplified82.9%
*-commutative82.9%
hypot-define44.7%
+-commutative44.7%
*-commutative44.7%
add-sqr-sqrt44.4%
sqrt-unprod44.7%
*-commutative44.7%
*-commutative44.7%
swap-sqr44.7%
Applied egg-rr82.9%
*-commutative82.9%
associate-*r*82.9%
metadata-eval82.9%
hypot-undefine44.7%
unpow244.7%
unpow244.7%
+-commutative44.7%
unpow244.7%
unpow244.7%
hypot-undefine82.9%
Simplified82.9%
Taylor expanded in re around 0 66.5%
Final simplification43.2%
(FPCore (re im) :precision binary64 (if (<= re -5e-310) (sqrt 0.0) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= -5e-310) {
tmp = 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 <= (-5d-310)) then
tmp = 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 <= -5e-310) {
tmp = Math.sqrt(0.0);
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -5e-310: tmp = math.sqrt(0.0) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -5e-310) tmp = sqrt(0.0); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -5e-310) tmp = sqrt(0.0); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -5e-310], N[Sqrt[0.0], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\sqrt{0}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -4.999999999999985e-310Initial program 31.6%
sqr-neg31.6%
+-commutative31.6%
sqr-neg31.6%
+-commutative31.6%
distribute-rgt-in31.6%
cancel-sign-sub31.6%
distribute-rgt-out--31.6%
sub-neg31.6%
remove-double-neg31.6%
+-commutative31.6%
hypot-define59.1%
Simplified59.1%
*-commutative59.1%
hypot-define31.6%
+-commutative31.6%
*-commutative31.6%
add-sqr-sqrt31.4%
sqrt-unprod31.6%
*-commutative31.6%
*-commutative31.6%
swap-sqr31.6%
Applied egg-rr59.1%
*-commutative59.1%
associate-*r*59.1%
metadata-eval59.1%
hypot-undefine31.6%
unpow231.6%
unpow231.6%
+-commutative31.6%
unpow231.6%
unpow231.6%
hypot-undefine59.1%
Simplified59.1%
add-cube-cbrt53.5%
fma-define52.0%
pow252.0%
hypot-undefine28.0%
+-commutative28.0%
hypot-undefine52.0%
Applied egg-rr52.0%
Taylor expanded in re around -inf 10.2%
associate-*r*10.2%
rem-cube-cbrt10.2%
metadata-eval10.2%
mul0-rgt10.2%
Simplified10.2%
if -4.999999999999985e-310 < re Initial program 53.3%
sqr-neg53.3%
+-commutative53.3%
sqr-neg53.3%
+-commutative53.3%
distribute-rgt-in53.3%
cancel-sign-sub53.3%
distribute-rgt-out--53.3%
sub-neg53.3%
remove-double-neg53.3%
+-commutative53.3%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around inf 54.0%
*-commutative54.0%
unpow254.0%
rem-square-sqrt55.1%
associate-*r*55.1%
metadata-eval55.1%
*-lft-identity55.1%
Simplified55.1%
Final simplification31.6%
(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 41.9%
sqr-neg41.9%
+-commutative41.9%
sqr-neg41.9%
+-commutative41.9%
distribute-rgt-in41.9%
cancel-sign-sub41.9%
distribute-rgt-out--41.9%
sub-neg41.9%
remove-double-neg41.9%
+-commutative41.9%
hypot-define78.6%
Simplified78.6%
Taylor expanded in re around inf 25.7%
*-commutative25.7%
unpow225.7%
rem-square-sqrt26.2%
associate-*r*26.2%
metadata-eval26.2%
*-lft-identity26.2%
Simplified26.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 2024096
(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)))))