
(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 5 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}
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= re -1.45e+59) (* 0.5 (* (/ im_m (sqrt (* re -2.0))) (sqrt 2.0))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im_m)))))))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= -1.45e+59) {
tmp = 0.5 * ((im_m / sqrt((re * -2.0))) * sqrt(2.0));
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im_m))));
}
return tmp;
}
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= -1.45e+59) {
tmp = 0.5 * ((im_m / Math.sqrt((re * -2.0))) * Math.sqrt(2.0));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im_m))));
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= -1.45e+59: tmp = 0.5 * ((im_m / math.sqrt((re * -2.0))) * math.sqrt(2.0)) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im_m)))) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= -1.45e+59) tmp = Float64(0.5 * Float64(Float64(im_m / sqrt(Float64(re * -2.0))) * sqrt(2.0))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im_m))))); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= -1.45e+59) tmp = 0.5 * ((im_m / sqrt((re * -2.0))) * sqrt(2.0)); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im_m)))); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, -1.45e+59], N[(0.5 * N[(N[(im$95$m / N[Sqrt[N[(re * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im$95$m ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.45 \cdot 10^{+59}:\\
\;\;\;\;0.5 \cdot \left(\frac{im_m}{\sqrt{re \cdot -2}} \cdot \sqrt{2}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im_m\right)\right)}\\
\end{array}
\end{array}
if re < -1.44999999999999995e59Initial program 7.9%
sqr-neg7.9%
+-commutative7.9%
sqr-neg7.9%
+-commutative7.9%
distribute-rgt-in7.9%
cancel-sign-sub7.9%
distribute-rgt-out--7.9%
sub-neg7.9%
remove-double-neg7.9%
+-commutative7.9%
hypot-def25.5%
Simplified25.5%
Taylor expanded in re around -inf 48.0%
*-commutative48.0%
associate-*l/48.0%
Simplified48.0%
sqrt-prod47.7%
*-commutative47.7%
associate-/l*47.7%
sqrt-div62.0%
unpow262.0%
sqrt-prod50.6%
add-sqr-sqrt53.9%
div-inv53.9%
metadata-eval53.9%
Applied egg-rr53.9%
if -1.44999999999999995e59 < re Initial program 46.2%
sqr-neg46.2%
+-commutative46.2%
sqr-neg46.2%
+-commutative46.2%
distribute-rgt-in46.2%
cancel-sign-sub46.2%
distribute-rgt-out--46.2%
sub-neg46.2%
remove-double-neg46.2%
+-commutative46.2%
hypot-def92.5%
Simplified92.5%
Final simplification84.7%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= re -1.15e+71) (* 0.5 (sqrt (/ (- (pow im_m 2.0)) re))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im_m)))))))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= -1.15e+71) {
tmp = 0.5 * sqrt((-pow(im_m, 2.0) / re));
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im_m))));
}
return tmp;
}
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= -1.15e+71) {
tmp = 0.5 * Math.sqrt((-Math.pow(im_m, 2.0) / re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im_m))));
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= -1.15e+71: tmp = 0.5 * math.sqrt((-math.pow(im_m, 2.0) / re)) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im_m)))) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= -1.15e+71) tmp = Float64(0.5 * sqrt(Float64(Float64(-(im_m ^ 2.0)) / re))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im_m))))); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= -1.15e+71) tmp = 0.5 * sqrt((-(im_m ^ 2.0) / re)); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im_m)))); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, -1.15e+71], N[(0.5 * N[Sqrt[N[((-N[Power[im$95$m, 2.0], $MachinePrecision]) / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im$95$m ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.15 \cdot 10^{+71}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{-{im_m}^{2}}{re}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im_m\right)\right)}\\
\end{array}
\end{array}
if re < -1.1500000000000001e71Initial program 8.1%
sqr-neg8.1%
+-commutative8.1%
sqr-neg8.1%
+-commutative8.1%
distribute-rgt-in8.1%
cancel-sign-sub8.1%
distribute-rgt-out--8.1%
sub-neg8.1%
remove-double-neg8.1%
+-commutative8.1%
hypot-def24.4%
Simplified24.4%
Taylor expanded in re around -inf 49.7%
*-commutative49.7%
associate-*l/49.7%
Simplified49.7%
expm1-log1p-u49.4%
expm1-udef24.6%
associate-*r/24.6%
*-commutative24.6%
associate-*r*24.6%
metadata-eval24.6%
Applied egg-rr24.6%
expm1-def49.4%
expm1-log1p49.7%
mul-1-neg49.7%
Simplified49.7%
if -1.1500000000000001e71 < re Initial program 45.8%
sqr-neg45.8%
+-commutative45.8%
sqr-neg45.8%
+-commutative45.8%
distribute-rgt-in45.8%
cancel-sign-sub45.8%
distribute-rgt-out--45.8%
sub-neg45.8%
remove-double-neg45.8%
+-commutative45.8%
hypot-def92.2%
Simplified92.2%
Final simplification83.9%
im_m = (fabs.f64 im)
(FPCore (re im_m)
:precision binary64
(if (<= re -2.2e+69)
(* 0.5 (sqrt (/ (- (pow im_m 2.0)) re)))
(if (<= re 1.46e-198)
(* 0.5 (sqrt (* im_m 2.0)))
(* 0.5 (* 2.0 (sqrt re))))))im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= -2.2e+69) {
tmp = 0.5 * sqrt((-pow(im_m, 2.0) / re));
} else if (re <= 1.46e-198) {
tmp = 0.5 * sqrt((im_m * 2.0));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (re <= (-2.2d+69)) then
tmp = 0.5d0 * sqrt((-(im_m ** 2.0d0) / re))
else if (re <= 1.46d-198) then
tmp = 0.5d0 * sqrt((im_m * 2.0d0))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= -2.2e+69) {
tmp = 0.5 * Math.sqrt((-Math.pow(im_m, 2.0) / re));
} else if (re <= 1.46e-198) {
tmp = 0.5 * Math.sqrt((im_m * 2.0));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= -2.2e+69: tmp = 0.5 * math.sqrt((-math.pow(im_m, 2.0) / re)) elif re <= 1.46e-198: tmp = 0.5 * math.sqrt((im_m * 2.0)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= -2.2e+69) tmp = Float64(0.5 * sqrt(Float64(Float64(-(im_m ^ 2.0)) / re))); elseif (re <= 1.46e-198) tmp = Float64(0.5 * sqrt(Float64(im_m * 2.0))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= -2.2e+69) tmp = 0.5 * sqrt((-(im_m ^ 2.0) / re)); elseif (re <= 1.46e-198) tmp = 0.5 * sqrt((im_m * 2.0)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, -2.2e+69], N[(0.5 * N[Sqrt[N[((-N[Power[im$95$m, 2.0], $MachinePrecision]) / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.46e-198], N[(0.5 * N[Sqrt[N[(im$95$m * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.2 \cdot 10^{+69}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{-{im_m}^{2}}{re}}\\
\mathbf{elif}\;re \leq 1.46 \cdot 10^{-198}:\\
\;\;\;\;0.5 \cdot \sqrt{im_m \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -2.2000000000000002e69Initial program 8.1%
sqr-neg8.1%
+-commutative8.1%
sqr-neg8.1%
+-commutative8.1%
distribute-rgt-in8.1%
cancel-sign-sub8.1%
distribute-rgt-out--8.1%
sub-neg8.1%
remove-double-neg8.1%
+-commutative8.1%
hypot-def24.4%
Simplified24.4%
Taylor expanded in re around -inf 49.7%
*-commutative49.7%
associate-*l/49.7%
Simplified49.7%
expm1-log1p-u49.4%
expm1-udef24.6%
associate-*r/24.6%
*-commutative24.6%
associate-*r*24.6%
metadata-eval24.6%
Applied egg-rr24.6%
expm1-def49.4%
expm1-log1p49.7%
mul-1-neg49.7%
Simplified49.7%
if -2.2000000000000002e69 < re < 1.46e-198Initial program 43.5%
sqr-neg43.5%
+-commutative43.5%
sqr-neg43.5%
+-commutative43.5%
distribute-rgt-in43.5%
cancel-sign-sub43.5%
distribute-rgt-out--43.5%
sub-neg43.5%
remove-double-neg43.5%
+-commutative43.5%
hypot-def84.8%
Simplified84.8%
Taylor expanded in re around 0 44.2%
if 1.46e-198 < re Initial program 48.3%
sqr-neg48.3%
+-commutative48.3%
sqr-neg48.3%
+-commutative48.3%
distribute-rgt-in48.3%
cancel-sign-sub48.3%
distribute-rgt-out--48.3%
sub-neg48.3%
remove-double-neg48.3%
+-commutative48.3%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 69.8%
*-commutative69.8%
unpow269.8%
rem-square-sqrt71.1%
Simplified71.1%
Final simplification55.8%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= re 1.46e-198) (* 0.5 (sqrt (* im_m 2.0))) (* 0.5 (* 2.0 (sqrt re)))))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= 1.46e-198) {
tmp = 0.5 * sqrt((im_m * 2.0));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (re <= 1.46d-198) then
tmp = 0.5d0 * sqrt((im_m * 2.0d0))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= 1.46e-198) {
tmp = 0.5 * Math.sqrt((im_m * 2.0));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= 1.46e-198: tmp = 0.5 * math.sqrt((im_m * 2.0)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= 1.46e-198) tmp = Float64(0.5 * sqrt(Float64(im_m * 2.0))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= 1.46e-198) tmp = 0.5 * sqrt((im_m * 2.0)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, 1.46e-198], N[(0.5 * N[Sqrt[N[(im$95$m * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq 1.46 \cdot 10^{-198}:\\
\;\;\;\;0.5 \cdot \sqrt{im_m \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < 1.46e-198Initial program 32.1%
sqr-neg32.1%
+-commutative32.1%
sqr-neg32.1%
+-commutative32.1%
distribute-rgt-in32.1%
cancel-sign-sub32.1%
distribute-rgt-out--32.1%
sub-neg32.1%
remove-double-neg32.1%
+-commutative32.1%
hypot-def65.4%
Simplified65.4%
Taylor expanded in re around 0 33.9%
if 1.46e-198 < re Initial program 48.3%
sqr-neg48.3%
+-commutative48.3%
sqr-neg48.3%
+-commutative48.3%
distribute-rgt-in48.3%
cancel-sign-sub48.3%
distribute-rgt-out--48.3%
sub-neg48.3%
remove-double-neg48.3%
+-commutative48.3%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 69.8%
*-commutative69.8%
unpow269.8%
rem-square-sqrt71.1%
Simplified71.1%
Final simplification48.4%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (* 0.5 (sqrt (* im_m 2.0))))
im_m = fabs(im);
double code(double re, double im_m) {
return 0.5 * sqrt((im_m * 2.0));
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
code = 0.5d0 * sqrt((im_m * 2.0d0))
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
return 0.5 * Math.sqrt((im_m * 2.0));
}
im_m = math.fabs(im) def code(re, im_m): return 0.5 * math.sqrt((im_m * 2.0))
im_m = abs(im) function code(re, im_m) return Float64(0.5 * sqrt(Float64(im_m * 2.0))) end
im_m = abs(im); function tmp = code(re, im_m) tmp = 0.5 * sqrt((im_m * 2.0)); end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := N[(0.5 * N[Sqrt[N[(im$95$m * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|
\\
0.5 \cdot \sqrt{im_m \cdot 2}
\end{array}
Initial program 38.4%
sqr-neg38.4%
+-commutative38.4%
sqr-neg38.4%
+-commutative38.4%
distribute-rgt-in38.4%
cancel-sign-sub38.4%
distribute-rgt-out--38.4%
sub-neg38.4%
remove-double-neg38.4%
+-commutative38.4%
hypot-def78.9%
Simplified78.9%
Taylor expanded in re around 0 28.2%
Final simplification28.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 2023332
(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)))))