(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im) :precision binary64 (if (<= re -3.1991825709021553e+118) (* 0.5 (* (sqrt (/ -1.0 re)) im)) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
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 <= -3.1991825709021553e+118) {
tmp = 0.5 * (sqrt((-1.0 / re)) * im);
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
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 <= -3.1991825709021553e+118) {
tmp = 0.5 * (Math.sqrt((-1.0 / re)) * im);
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
}
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 <= -3.1991825709021553e+118: tmp = 0.5 * (math.sqrt((-1.0 / re)) * im) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) 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 <= -3.1991825709021553e+118) tmp = Float64(0.5 * Float64(sqrt(Float64(-1.0 / re)) * im)); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); 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 <= -3.1991825709021553e+118) tmp = 0.5 * (sqrt((-1.0 / re)) * im); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); 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, -3.1991825709021553e+118], N[(0.5 * N[(N[Sqrt[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision] * im), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $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 -3.1991825709021553 \cdot 10^{+118}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{\frac{-1}{re}} \cdot im\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
Results
| Original | 38.6 |
|---|---|
| Target | 33.6 |
| Herbie | 12.0 |
if re < -3.1991825709021553e118Initial program 61.8
Simplified40.8
Taylor expanded in re around -inf 31.6
Simplified24.8
Taylor expanded in im around 0 33.1
if -3.1991825709021553e118 < re Initial program 34.5
Simplified8.3
Final simplification12.0
herbie shell --seed 2022210
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:herbie-target
(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)))))