(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im) :precision binary64 (if (<= re -9.46019038849945e+108) (* 0.5 (sqrt (* 2.0 (* -0.5 (/ (pow im 2.0) re))))) (* 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 <= -9.46019038849945e+108) {
tmp = 0.5 * sqrt((2.0 * (-0.5 * (pow(im, 2.0) / re))));
} 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 <= -9.46019038849945e+108) {
tmp = 0.5 * Math.sqrt((2.0 * (-0.5 * (Math.pow(im, 2.0) / re))));
} 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 <= -9.46019038849945e+108: tmp = 0.5 * math.sqrt((2.0 * (-0.5 * (math.pow(im, 2.0) / re)))) 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 <= -9.46019038849945e+108) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(-0.5 * Float64((im ^ 2.0) / re))))); 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 <= -9.46019038849945e+108) tmp = 0.5 * sqrt((2.0 * (-0.5 * ((im ^ 2.0) / re)))); 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, -9.46019038849945e+108], 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[(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 -9.46019038849945 \cdot 10^{+108}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
Results
| Original | 38.5 |
|---|---|
| Target | 33.3 |
| Herbie | 11.6 |
if re < -9.4601903884994502e108Initial program 61.6
Simplified39.6
Taylor expanded in re around -inf 31.3
if -9.4601903884994502e108 < re Initial program 34.0
Simplified7.8
Final simplification11.6
herbie shell --seed 2022192
(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)))))