
(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 6 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 (+ (* re re) (* im im))) re) 0.0) (* im (* 0.5 (pow re -0.5))) (sqrt (* 0.5 (- (hypot re im) re)))))
double code(double re, double im) {
double tmp;
if ((sqrt(((re * re) + (im * im))) - re) <= 0.0) {
tmp = im * (0.5 * pow(re, -0.5));
} else {
tmp = sqrt((0.5 * (hypot(re, im) - re)));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if ((Math.sqrt(((re * re) + (im * im))) - re) <= 0.0) {
tmp = im * (0.5 * Math.pow(re, -0.5));
} else {
tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
}
return tmp;
}
def code(re, im): tmp = 0 if (math.sqrt(((re * re) + (im * im))) - re) <= 0.0: tmp = im * (0.5 * math.pow(re, -0.5)) else: tmp = math.sqrt((0.5 * (math.hypot(re, im) - re))) return tmp
function code(re, im) tmp = 0.0 if (Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re) <= 0.0) tmp = Float64(im * Float64(0.5 * (re ^ -0.5))); else tmp = sqrt(Float64(0.5 * Float64(hypot(re, im) - re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if ((sqrt(((re * re) + (im * im))) - re) <= 0.0) tmp = im * (0.5 * (re ^ -0.5)); else tmp = sqrt((0.5 * (hypot(re, im) - re))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision], 0.0], N[(im * N[(0.5 * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;im \cdot \left(0.5 \cdot {re}^{-0.5}\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\end{array}
\end{array}
if (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0Initial program 11.5%
add-sqr-sqrt11.5%
sqrt-unprod11.5%
*-commutative11.5%
*-commutative11.5%
swap-sqr11.5%
add-sqr-sqrt11.5%
*-commutative11.5%
hypot-define18.3%
metadata-eval18.3%
Applied egg-rr18.3%
associate-*l*18.3%
metadata-eval18.3%
Simplified18.3%
Taylor expanded in re around inf 92.8%
associate-*l*92.8%
unpow292.8%
rem-square-sqrt93.9%
rem-exp-log88.7%
exp-neg88.7%
unpow1/288.7%
exp-prod88.7%
distribute-lft-neg-out88.7%
distribute-rgt-neg-in88.7%
metadata-eval88.7%
exp-to-pow94.1%
Simplified94.1%
if 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 45.5%
add-sqr-sqrt45.2%
sqrt-unprod45.5%
*-commutative45.5%
*-commutative45.5%
swap-sqr45.5%
add-sqr-sqrt45.5%
*-commutative45.5%
hypot-define91.3%
metadata-eval91.3%
Applied egg-rr91.3%
associate-*l*91.3%
metadata-eval91.3%
Simplified91.3%
Final simplification91.6%
(FPCore (re im)
:precision binary64
(if (<= re -1.25e-22)
(sqrt (- re))
(if (<= re 4.1e-10)
(sqrt (* 0.5 (- im re)))
(if (<= re 4.9e+62)
(* 0.5 (/ im (sqrt re)))
(if (<= re 1.66e+106)
(sqrt (* im 0.5))
(* im (* 0.5 (pow re -0.5))))))))
double code(double re, double im) {
double tmp;
if (re <= -1.25e-22) {
tmp = sqrt(-re);
} else if (re <= 4.1e-10) {
tmp = sqrt((0.5 * (im - re)));
} else if (re <= 4.9e+62) {
tmp = 0.5 * (im / sqrt(re));
} else if (re <= 1.66e+106) {
tmp = sqrt((im * 0.5));
} else {
tmp = im * (0.5 * pow(re, -0.5));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-1.25d-22)) then
tmp = sqrt(-re)
else if (re <= 4.1d-10) then
tmp = sqrt((0.5d0 * (im - re)))
else if (re <= 4.9d+62) then
tmp = 0.5d0 * (im / sqrt(re))
else if (re <= 1.66d+106) then
tmp = sqrt((im * 0.5d0))
else
tmp = im * (0.5d0 * (re ** (-0.5d0)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.25e-22) {
tmp = Math.sqrt(-re);
} else if (re <= 4.1e-10) {
tmp = Math.sqrt((0.5 * (im - re)));
} else if (re <= 4.9e+62) {
tmp = 0.5 * (im / Math.sqrt(re));
} else if (re <= 1.66e+106) {
tmp = Math.sqrt((im * 0.5));
} else {
tmp = im * (0.5 * Math.pow(re, -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.25e-22: tmp = math.sqrt(-re) elif re <= 4.1e-10: tmp = math.sqrt((0.5 * (im - re))) elif re <= 4.9e+62: tmp = 0.5 * (im / math.sqrt(re)) elif re <= 1.66e+106: tmp = math.sqrt((im * 0.5)) else: tmp = im * (0.5 * math.pow(re, -0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.25e-22) tmp = sqrt(Float64(-re)); elseif (re <= 4.1e-10) tmp = sqrt(Float64(0.5 * Float64(im - re))); elseif (re <= 4.9e+62) tmp = Float64(0.5 * Float64(im / sqrt(re))); elseif (re <= 1.66e+106) tmp = sqrt(Float64(im * 0.5)); else tmp = Float64(im * Float64(0.5 * (re ^ -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.25e-22) tmp = sqrt(-re); elseif (re <= 4.1e-10) tmp = sqrt((0.5 * (im - re))); elseif (re <= 4.9e+62) tmp = 0.5 * (im / sqrt(re)); elseif (re <= 1.66e+106) tmp = sqrt((im * 0.5)); else tmp = im * (0.5 * (re ^ -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.25e-22], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 4.1e-10], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[re, 4.9e+62], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.66e+106], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision], N[(im * N[(0.5 * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.25 \cdot 10^{-22}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 4.1 \cdot 10^{-10}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{elif}\;re \leq 4.9 \cdot 10^{+62}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\mathbf{elif}\;re \leq 1.66 \cdot 10^{+106}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(0.5 \cdot {re}^{-0.5}\right)\\
\end{array}
\end{array}
if re < -1.24999999999999988e-22Initial program 43.7%
add-sqr-sqrt43.3%
sqrt-unprod43.7%
*-commutative43.7%
*-commutative43.7%
swap-sqr43.7%
add-sqr-sqrt43.7%
*-commutative43.7%
hypot-define98.6%
metadata-eval98.6%
Applied egg-rr98.6%
associate-*l*98.6%
metadata-eval98.6%
Simplified98.6%
Taylor expanded in re around -inf 86.5%
neg-mul-186.5%
Simplified86.5%
if -1.24999999999999988e-22 < re < 4.0999999999999998e-10Initial program 55.1%
add-sqr-sqrt54.7%
sqrt-unprod55.1%
*-commutative55.1%
*-commutative55.1%
swap-sqr55.1%
add-sqr-sqrt55.1%
*-commutative55.1%
hypot-define93.7%
metadata-eval93.7%
Applied egg-rr93.7%
associate-*l*93.7%
metadata-eval93.7%
Simplified93.7%
Taylor expanded in re around 0 86.1%
neg-mul-186.1%
unsub-neg86.1%
Simplified86.1%
if 4.0999999999999998e-10 < re < 4.8999999999999997e62Initial program 11.8%
Taylor expanded in re around inf 76.9%
*-commutative76.9%
associate-*l*77.2%
*-commutative77.2%
Simplified77.2%
pow177.2%
sqrt-unprod78.2%
metadata-eval78.2%
metadata-eval78.2%
*-un-lft-identity78.2%
sqrt-div78.1%
metadata-eval78.1%
un-div-inv78.5%
Applied egg-rr78.5%
unpow178.5%
Simplified78.5%
if 4.8999999999999997e62 < re < 1.66e106Initial program 33.1%
add-sqr-sqrt33.0%
sqrt-unprod33.1%
*-commutative33.1%
*-commutative33.1%
swap-sqr33.1%
add-sqr-sqrt33.1%
*-commutative33.1%
hypot-define81.0%
metadata-eval81.0%
Applied egg-rr81.0%
associate-*l*81.0%
metadata-eval81.0%
Simplified81.0%
Taylor expanded in re around 0 81.1%
if 1.66e106 < re Initial program 10.9%
add-sqr-sqrt10.9%
sqrt-unprod10.9%
*-commutative10.9%
*-commutative10.9%
swap-sqr10.9%
add-sqr-sqrt10.9%
*-commutative10.9%
hypot-define43.1%
metadata-eval43.1%
Applied egg-rr43.1%
associate-*l*43.1%
metadata-eval43.1%
Simplified43.1%
Taylor expanded in re around inf 81.0%
associate-*l*81.0%
unpow281.0%
rem-square-sqrt81.8%
rem-exp-log77.1%
exp-neg77.1%
unpow1/277.1%
exp-prod77.1%
distribute-lft-neg-out77.1%
distribute-rgt-neg-in77.1%
metadata-eval77.1%
exp-to-pow81.9%
Simplified81.9%
Final simplification84.9%
(FPCore (re im)
:precision binary64
(if (<= re -3.9e-24)
(sqrt (- re))
(if (<= re 2.1e-5)
(sqrt (* 0.5 (- im re)))
(if (or (<= re 1.05e+62) (not (<= re 1.2e+106)))
(* 0.5 (/ im (sqrt re)))
(sqrt (* im 0.5))))))
double code(double re, double im) {
double tmp;
if (re <= -3.9e-24) {
tmp = sqrt(-re);
} else if (re <= 2.1e-5) {
tmp = sqrt((0.5 * (im - re)));
} else if ((re <= 1.05e+62) || !(re <= 1.2e+106)) {
tmp = 0.5 * (im / sqrt(re));
} else {
tmp = sqrt((im * 0.5));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-3.9d-24)) then
tmp = sqrt(-re)
else if (re <= 2.1d-5) then
tmp = sqrt((0.5d0 * (im - re)))
else if ((re <= 1.05d+62) .or. (.not. (re <= 1.2d+106))) then
tmp = 0.5d0 * (im / sqrt(re))
else
tmp = sqrt((im * 0.5d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3.9e-24) {
tmp = Math.sqrt(-re);
} else if (re <= 2.1e-5) {
tmp = Math.sqrt((0.5 * (im - re)));
} else if ((re <= 1.05e+62) || !(re <= 1.2e+106)) {
tmp = 0.5 * (im / Math.sqrt(re));
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.9e-24: tmp = math.sqrt(-re) elif re <= 2.1e-5: tmp = math.sqrt((0.5 * (im - re))) elif (re <= 1.05e+62) or not (re <= 1.2e+106): tmp = 0.5 * (im / math.sqrt(re)) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.9e-24) tmp = sqrt(Float64(-re)); elseif (re <= 2.1e-5) tmp = sqrt(Float64(0.5 * Float64(im - re))); elseif ((re <= 1.05e+62) || !(re <= 1.2e+106)) tmp = Float64(0.5 * Float64(im / sqrt(re))); else tmp = sqrt(Float64(im * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.9e-24) tmp = sqrt(-re); elseif (re <= 2.1e-5) tmp = sqrt((0.5 * (im - re))); elseif ((re <= 1.05e+62) || ~((re <= 1.2e+106))) tmp = 0.5 * (im / sqrt(re)); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.9e-24], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 2.1e-5], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[Or[LessEqual[re, 1.05e+62], N[Not[LessEqual[re, 1.2e+106]], $MachinePrecision]], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.9 \cdot 10^{-24}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 2.1 \cdot 10^{-5}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{elif}\;re \leq 1.05 \cdot 10^{+62} \lor \neg \left(re \leq 1.2 \cdot 10^{+106}\right):\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < -3.9e-24Initial program 43.7%
add-sqr-sqrt43.3%
sqrt-unprod43.7%
*-commutative43.7%
*-commutative43.7%
swap-sqr43.7%
add-sqr-sqrt43.7%
*-commutative43.7%
hypot-define98.6%
metadata-eval98.6%
Applied egg-rr98.6%
associate-*l*98.6%
metadata-eval98.6%
Simplified98.6%
Taylor expanded in re around -inf 86.5%
neg-mul-186.5%
Simplified86.5%
if -3.9e-24 < re < 2.09999999999999988e-5Initial program 55.1%
add-sqr-sqrt54.7%
sqrt-unprod55.1%
*-commutative55.1%
*-commutative55.1%
swap-sqr55.1%
add-sqr-sqrt55.1%
*-commutative55.1%
hypot-define93.7%
metadata-eval93.7%
Applied egg-rr93.7%
associate-*l*93.7%
metadata-eval93.7%
Simplified93.7%
Taylor expanded in re around 0 86.1%
neg-mul-186.1%
unsub-neg86.1%
Simplified86.1%
if 2.09999999999999988e-5 < re < 1.05e62 or 1.2e106 < re Initial program 11.1%
Taylor expanded in re around inf 80.0%
*-commutative80.0%
associate-*l*80.1%
*-commutative80.1%
Simplified80.1%
pow180.1%
sqrt-unprod80.9%
metadata-eval80.9%
metadata-eval80.9%
*-un-lft-identity80.9%
sqrt-div80.9%
metadata-eval80.9%
un-div-inv81.0%
Applied egg-rr81.0%
unpow181.0%
Simplified81.0%
if 1.05e62 < re < 1.2e106Initial program 33.1%
add-sqr-sqrt33.0%
sqrt-unprod33.1%
*-commutative33.1%
*-commutative33.1%
swap-sqr33.1%
add-sqr-sqrt33.1%
*-commutative33.1%
hypot-define81.0%
metadata-eval81.0%
Applied egg-rr81.0%
associate-*l*81.0%
metadata-eval81.0%
Simplified81.0%
Taylor expanded in re around 0 81.1%
Final simplification84.8%
(FPCore (re im)
:precision binary64
(if (<= re -4e-23)
(sqrt (- re))
(if (<= re 5e-8)
(sqrt (* 0.5 (- im re)))
(if (<= re 3.2e+62)
(* 0.5 (/ im (sqrt re)))
(if (<= re 6.6e+104) (sqrt (* im 0.5)) (* im (sqrt (/ 0.25 re))))))))
double code(double re, double im) {
double tmp;
if (re <= -4e-23) {
tmp = sqrt(-re);
} else if (re <= 5e-8) {
tmp = sqrt((0.5 * (im - re)));
} else if (re <= 3.2e+62) {
tmp = 0.5 * (im / sqrt(re));
} else if (re <= 6.6e+104) {
tmp = sqrt((im * 0.5));
} else {
tmp = im * sqrt((0.25 / re));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-4d-23)) then
tmp = sqrt(-re)
else if (re <= 5d-8) then
tmp = sqrt((0.5d0 * (im - re)))
else if (re <= 3.2d+62) then
tmp = 0.5d0 * (im / sqrt(re))
else if (re <= 6.6d+104) then
tmp = sqrt((im * 0.5d0))
else
tmp = im * sqrt((0.25d0 / re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -4e-23) {
tmp = Math.sqrt(-re);
} else if (re <= 5e-8) {
tmp = Math.sqrt((0.5 * (im - re)));
} else if (re <= 3.2e+62) {
tmp = 0.5 * (im / Math.sqrt(re));
} else if (re <= 6.6e+104) {
tmp = Math.sqrt((im * 0.5));
} else {
tmp = im * Math.sqrt((0.25 / re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -4e-23: tmp = math.sqrt(-re) elif re <= 5e-8: tmp = math.sqrt((0.5 * (im - re))) elif re <= 3.2e+62: tmp = 0.5 * (im / math.sqrt(re)) elif re <= 6.6e+104: tmp = math.sqrt((im * 0.5)) else: tmp = im * math.sqrt((0.25 / re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -4e-23) tmp = sqrt(Float64(-re)); elseif (re <= 5e-8) tmp = sqrt(Float64(0.5 * Float64(im - re))); elseif (re <= 3.2e+62) tmp = Float64(0.5 * Float64(im / sqrt(re))); elseif (re <= 6.6e+104) tmp = sqrt(Float64(im * 0.5)); else tmp = Float64(im * sqrt(Float64(0.25 / re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4e-23) tmp = sqrt(-re); elseif (re <= 5e-8) tmp = sqrt((0.5 * (im - re))); elseif (re <= 3.2e+62) tmp = 0.5 * (im / sqrt(re)); elseif (re <= 6.6e+104) tmp = sqrt((im * 0.5)); else tmp = im * sqrt((0.25 / re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -4e-23], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 5e-8], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[re, 3.2e+62], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 6.6e+104], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision], N[(im * N[Sqrt[N[(0.25 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4 \cdot 10^{-23}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 5 \cdot 10^{-8}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{elif}\;re \leq 3.2 \cdot 10^{+62}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\mathbf{elif}\;re \leq 6.6 \cdot 10^{+104}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\
\end{array}
\end{array}
if re < -3.99999999999999984e-23Initial program 43.7%
add-sqr-sqrt43.3%
sqrt-unprod43.7%
*-commutative43.7%
*-commutative43.7%
swap-sqr43.7%
add-sqr-sqrt43.7%
*-commutative43.7%
hypot-define98.6%
metadata-eval98.6%
Applied egg-rr98.6%
associate-*l*98.6%
metadata-eval98.6%
Simplified98.6%
Taylor expanded in re around -inf 86.5%
neg-mul-186.5%
Simplified86.5%
if -3.99999999999999984e-23 < re < 4.9999999999999998e-8Initial program 55.1%
add-sqr-sqrt54.7%
sqrt-unprod55.1%
*-commutative55.1%
*-commutative55.1%
swap-sqr55.1%
add-sqr-sqrt55.1%
*-commutative55.1%
hypot-define93.7%
metadata-eval93.7%
Applied egg-rr93.7%
associate-*l*93.7%
metadata-eval93.7%
Simplified93.7%
Taylor expanded in re around 0 86.1%
neg-mul-186.1%
unsub-neg86.1%
Simplified86.1%
if 4.9999999999999998e-8 < re < 3.19999999999999984e62Initial program 11.8%
Taylor expanded in re around inf 76.9%
*-commutative76.9%
associate-*l*77.2%
*-commutative77.2%
Simplified77.2%
pow177.2%
sqrt-unprod78.2%
metadata-eval78.2%
metadata-eval78.2%
*-un-lft-identity78.2%
sqrt-div78.1%
metadata-eval78.1%
un-div-inv78.5%
Applied egg-rr78.5%
unpow178.5%
Simplified78.5%
if 3.19999999999999984e62 < re < 6.59999999999999969e104Initial program 33.1%
add-sqr-sqrt33.0%
sqrt-unprod33.1%
*-commutative33.1%
*-commutative33.1%
swap-sqr33.1%
add-sqr-sqrt33.1%
*-commutative33.1%
hypot-define81.0%
metadata-eval81.0%
Applied egg-rr81.0%
associate-*l*81.0%
metadata-eval81.0%
Simplified81.0%
Taylor expanded in re around 0 81.1%
if 6.59999999999999969e104 < re Initial program 10.9%
Taylor expanded in re around inf 80.9%
*-commutative80.9%
associate-*l*81.0%
*-commutative81.0%
Simplified81.0%
pow181.0%
sqrt-unprod81.7%
metadata-eval81.7%
metadata-eval81.7%
*-un-lft-identity81.7%
sqrt-div81.7%
metadata-eval81.7%
un-div-inv81.7%
Applied egg-rr81.7%
unpow181.7%
associate-*r/81.7%
*-commutative81.7%
associate-/l*81.8%
Simplified81.8%
add-sqr-sqrt81.5%
sqrt-unprod81.8%
frac-times81.7%
metadata-eval81.7%
add-sqr-sqrt81.8%
Applied egg-rr81.8%
Final simplification84.9%
(FPCore (re im) :precision binary64 (if (<= re -9.2e-23) (sqrt (- re)) (sqrt (* im 0.5))))
double code(double re, double im) {
double tmp;
if (re <= -9.2e-23) {
tmp = sqrt(-re);
} else {
tmp = sqrt((im * 0.5));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-9.2d-23)) then
tmp = sqrt(-re)
else
tmp = sqrt((im * 0.5d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -9.2e-23) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -9.2e-23: tmp = math.sqrt(-re) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -9.2e-23) tmp = sqrt(Float64(-re)); else tmp = sqrt(Float64(im * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -9.2e-23) tmp = sqrt(-re); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -9.2e-23], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -9.2 \cdot 10^{-23}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < -9.2000000000000004e-23Initial program 43.7%
add-sqr-sqrt43.3%
sqrt-unprod43.7%
*-commutative43.7%
*-commutative43.7%
swap-sqr43.7%
add-sqr-sqrt43.7%
*-commutative43.7%
hypot-define98.6%
metadata-eval98.6%
Applied egg-rr98.6%
associate-*l*98.6%
metadata-eval98.6%
Simplified98.6%
Taylor expanded in re around -inf 86.5%
neg-mul-186.5%
Simplified86.5%
if -9.2000000000000004e-23 < re Initial program 40.4%
add-sqr-sqrt40.1%
sqrt-unprod40.4%
*-commutative40.4%
*-commutative40.4%
swap-sqr40.4%
add-sqr-sqrt40.4%
*-commutative40.4%
hypot-define76.2%
metadata-eval76.2%
Applied egg-rr76.2%
associate-*l*76.2%
metadata-eval76.2%
Simplified76.2%
Taylor expanded in re around 0 66.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(Float64(-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.3%
add-sqr-sqrt41.0%
sqrt-unprod41.3%
*-commutative41.3%
*-commutative41.3%
swap-sqr41.3%
add-sqr-sqrt41.3%
*-commutative41.3%
hypot-define82.2%
metadata-eval82.2%
Applied egg-rr82.2%
associate-*l*82.2%
metadata-eval82.2%
Simplified82.2%
Taylor expanded in re around -inf 27.6%
neg-mul-127.6%
Simplified27.6%
herbie shell --seed 2024100
(FPCore (re im)
:name "math.sqrt on complex, imaginary part, im greater than 0 branch"
:precision binary64
:pre (> im 0.0)
(* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))