(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im) :precision binary64 (if (<= re 7.962544944086704e-55) (* 0.5 (sqrt (* 2.0 (- (hypot re im) re)))) (* 0.5 (/ (* im (sqrt 0.5)) (* (pow 2.0 -0.5) (pow re 0.5))))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re)));
}
double code(double re, double im) {
double tmp;
if (re <= 7.962544944086704e-55) {
tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
} else {
tmp = 0.5 * ((im * sqrt(0.5)) / (pow(2.0, -0.5) * pow(re, 0.5)));
}
return tmp;
}
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) - re)));
}
public static double code(double re, double im) {
double tmp;
if (re <= 7.962544944086704e-55) {
tmp = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
} else {
tmp = 0.5 * ((im * Math.sqrt(0.5)) / (Math.pow(2.0, -0.5) * Math.pow(re, 0.5)));
}
return tmp;
}
def code(re, im): return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re)))
def code(re, im): tmp = 0 if re <= 7.962544944086704e-55: tmp = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re))) else: tmp = 0.5 * ((im * math.sqrt(0.5)) / (math.pow(2.0, -0.5) * math.pow(re, 0.5))) return tmp
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re)))) end
function code(re, im) tmp = 0.0 if (re <= 7.962544944086704e-55) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re)))); else tmp = Float64(0.5 * Float64(Float64(im * sqrt(0.5)) / Float64((2.0 ^ -0.5) * (re ^ 0.5)))); end return tmp end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))); end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 7.962544944086704e-55) tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re))); else tmp = 0.5 * ((im * sqrt(0.5)) / ((2.0 ^ -0.5) * (re ^ 0.5))); end tmp_2 = tmp; 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]
code[re_, im_] := If[LessEqual[re, 7.962544944086704e-55], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[(im * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision] / N[(N[Power[2.0, -0.5], $MachinePrecision] * N[Power[re, 0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\begin{array}{l}
\mathbf{if}\;re \leq 7.962544944086704 \cdot 10^{-55}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im \cdot \sqrt{0.5}}{{2}^{-0.5} \cdot {re}^{0.5}}\\
\end{array}
Results
if re < 7.962544944086704e-55Initial program 31.5
Simplified3.5
if 7.962544944086704e-55 < re Initial program 55.0
Simplified35.6
Taylor expanded in im around 0 18.8
Simplified18.7
Applied egg-rr18.7
Applied egg-rr18.6
Taylor expanded in re around 0 21.1
Simplified18.5
Final simplification8.0
herbie shell --seed 2022203
(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)))))