
(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 7 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}
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0) (* 0.5 (pow (pow (exp 0.25) (fma 2.0 (log im) (log (/ -1.0 re)))) 2.0)) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) {
tmp = 0.5 * pow(pow(exp(0.25), fma(2.0, log(im), log((-1.0 / re)))), 2.0);
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
return tmp;
}
im = abs(im) function code(re, im) tmp = 0.0 if (sqrt(Float64(2.0 * Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))))) <= 0.0) tmp = Float64(0.5 * ((exp(0.25) ^ fma(2.0, log(im), log(Float64(-1.0 / re)))) ^ 2.0)); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); end return tmp end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[N[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(0.5 * N[Power[N[Power[N[Exp[0.25], $MachinePrecision], N[(2.0 * N[Log[im], $MachinePrecision] + N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot {\left({\left(e^{0.25}\right)}^{\left(\mathsf{fma}\left(2, \log im, \log \left(\frac{-1}{re}\right)\right)\right)}\right)}^{2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 4.7%
sqr-neg4.7%
+-commutative4.7%
sqr-neg4.7%
distribute-rgt-in4.7%
cancel-sign-sub4.7%
distribute-rgt-out--4.7%
sub-neg4.7%
remove-double-neg4.7%
hypot-def4.7%
Simplified4.7%
add-sqr-sqrt4.7%
pow24.7%
pow1/24.7%
sqrt-pow14.7%
*-commutative4.7%
metadata-eval4.7%
Applied egg-rr4.7%
Taylor expanded in re around -inf 56.7%
exp-prod54.6%
+-commutative54.6%
log-pow47.3%
fma-def47.3%
Simplified47.3%
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 46.4%
sqr-neg46.4%
+-commutative46.4%
sqr-neg46.4%
distribute-rgt-in46.4%
cancel-sign-sub46.4%
distribute-rgt-out--46.4%
sub-neg46.4%
remove-double-neg46.4%
hypot-def89.5%
Simplified89.5%
Final simplification83.7%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -1.36e-24) (* 0.5 (pow (exp (* 0.25 (+ (log (/ -1.0 re)) (log (pow im 2.0))))) 2.0)) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -1.36e-24) {
tmp = 0.5 * pow(exp((0.25 * (log((-1.0 / re)) + log(pow(im, 2.0))))), 2.0);
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
return tmp;
}
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -1.36e-24) {
tmp = 0.5 * Math.pow(Math.exp((0.25 * (Math.log((-1.0 / re)) + Math.log(Math.pow(im, 2.0))))), 2.0);
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -1.36e-24: tmp = 0.5 * math.pow(math.exp((0.25 * (math.log((-1.0 / re)) + math.log(math.pow(im, 2.0))))), 2.0) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -1.36e-24) tmp = Float64(0.5 * (exp(Float64(0.25 * Float64(log(Float64(-1.0 / re)) + log((im ^ 2.0))))) ^ 2.0)); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.36e-24) tmp = 0.5 * (exp((0.25 * (log((-1.0 / re)) + log((im ^ 2.0))))) ^ 2.0); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -1.36e-24], N[(0.5 * N[Power[N[Exp[N[(0.25 * N[(N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision] + N[Log[N[Power[im, 2.0], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.36 \cdot 10^{-24}:\\
\;\;\;\;0.5 \cdot {\left(e^{0.25 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left({im}^{2}\right)\right)}\right)}^{2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -1.36000000000000001e-24Initial program 5.4%
sqr-neg5.4%
+-commutative5.4%
sqr-neg5.4%
distribute-rgt-in5.4%
cancel-sign-sub5.4%
distribute-rgt-out--5.4%
sub-neg5.4%
remove-double-neg5.4%
hypot-def25.5%
Simplified25.5%
add-sqr-sqrt25.4%
pow225.4%
pow1/225.4%
sqrt-pow125.4%
*-commutative25.4%
metadata-eval25.4%
Applied egg-rr25.4%
Taylor expanded in re around -inf 59.1%
if -1.36000000000000001e-24 < re Initial program 50.4%
sqr-neg50.4%
+-commutative50.4%
sqr-neg50.4%
distribute-rgt-in50.4%
cancel-sign-sub50.4%
distribute-rgt-out--50.4%
sub-neg50.4%
remove-double-neg50.4%
hypot-def92.2%
Simplified92.2%
Final simplification85.1%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0) (* 0.5 (sqrt (/ (* im (- im)) re))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) {
tmp = 0.5 * sqrt(((im * -im) / re));
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
return tmp;
}
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (Math.sqrt((2.0 * (re + Math.sqrt(((re * re) + (im * im)))))) <= 0.0) {
tmp = 0.5 * Math.sqrt(((im * -im) / re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if math.sqrt((2.0 * (re + math.sqrt(((re * re) + (im * im)))))) <= 0.0: tmp = 0.5 * math.sqrt(((im * -im) / re)) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (sqrt(Float64(2.0 * Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))))) <= 0.0) tmp = Float64(0.5 * sqrt(Float64(Float64(im * Float64(-im)) / re))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) tmp = 0.5 * sqrt(((im * -im) / re)); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[N[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(0.5 * N[Sqrt[N[(N[(im * (-im)), $MachinePrecision] / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot \left(-im\right)}{re}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 4.7%
sqr-neg4.7%
+-commutative4.7%
sqr-neg4.7%
distribute-rgt-in4.7%
cancel-sign-sub4.7%
distribute-rgt-out--4.7%
sub-neg4.7%
remove-double-neg4.7%
hypot-def4.7%
Simplified4.7%
Taylor expanded in re around -inf 55.8%
associate-*r/55.8%
neg-mul-155.8%
unpow255.8%
distribute-rgt-neg-in55.8%
Simplified55.8%
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 46.4%
sqr-neg46.4%
+-commutative46.4%
sqr-neg46.4%
distribute-rgt-in46.4%
cancel-sign-sub46.4%
distribute-rgt-out--46.4%
sub-neg46.4%
remove-double-neg46.4%
hypot-def89.5%
Simplified89.5%
Final simplification84.9%
NOTE: im should be positive before calling this function
(FPCore (re im)
:precision binary64
(if (<= re -1.36e-24)
(* 0.5 (sqrt (/ (* im (- im)) re)))
(if (<= re 5.1e-36)
(* 0.5 (sqrt (* 2.0 (+ re (+ im (/ (* re 0.5) (/ im re)))))))
(* 0.5 (* 2.0 (sqrt re))))))im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -1.36e-24) {
tmp = 0.5 * sqrt(((im * -im) / re));
} else if (re <= 5.1e-36) {
tmp = 0.5 * sqrt((2.0 * (re + (im + ((re * 0.5) / (im / re))))));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-1.36d-24)) then
tmp = 0.5d0 * sqrt(((im * -im) / re))
else if (re <= 5.1d-36) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + (im + ((re * 0.5d0) / (im / re))))))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -1.36e-24) {
tmp = 0.5 * Math.sqrt(((im * -im) / re));
} else if (re <= 5.1e-36) {
tmp = 0.5 * Math.sqrt((2.0 * (re + (im + ((re * 0.5) / (im / re))))));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -1.36e-24: tmp = 0.5 * math.sqrt(((im * -im) / re)) elif re <= 5.1e-36: tmp = 0.5 * math.sqrt((2.0 * (re + (im + ((re * 0.5) / (im / re)))))) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -1.36e-24) tmp = Float64(0.5 * sqrt(Float64(Float64(im * Float64(-im)) / re))); elseif (re <= 5.1e-36) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + Float64(im + Float64(Float64(re * 0.5) / Float64(im / re))))))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.36e-24) tmp = 0.5 * sqrt(((im * -im) / re)); elseif (re <= 5.1e-36) tmp = 0.5 * sqrt((2.0 * (re + (im + ((re * 0.5) / (im / re)))))); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -1.36e-24], N[(0.5 * N[Sqrt[N[(N[(im * (-im)), $MachinePrecision] / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5.1e-36], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[(im + N[(N[(re * 0.5), $MachinePrecision] / N[(im / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.36 \cdot 10^{-24}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot \left(-im\right)}{re}}\\
\mathbf{elif}\;re \leq 5.1 \cdot 10^{-36}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \left(im + \frac{re \cdot 0.5}{\frac{im}{re}}\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -1.36000000000000001e-24Initial program 5.4%
sqr-neg5.4%
+-commutative5.4%
sqr-neg5.4%
distribute-rgt-in5.4%
cancel-sign-sub5.4%
distribute-rgt-out--5.4%
sub-neg5.4%
remove-double-neg5.4%
hypot-def25.5%
Simplified25.5%
Taylor expanded in re around -inf 52.6%
associate-*r/52.6%
neg-mul-152.6%
unpow252.6%
distribute-rgt-neg-in52.6%
Simplified52.6%
if -1.36000000000000001e-24 < re < 5.09999999999999973e-36Initial program 52.8%
Taylor expanded in re around 0 37.6%
unpow237.6%
Simplified37.6%
*-commutative37.6%
associate-/l*37.6%
associate-*l/37.6%
Applied egg-rr37.6%
if 5.09999999999999973e-36 < re Initial program 46.9%
sqr-neg46.9%
+-commutative46.9%
sqr-neg46.9%
distribute-rgt-in46.9%
cancel-sign-sub46.9%
distribute-rgt-out--46.9%
sub-neg46.9%
remove-double-neg46.9%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 79.9%
*-commutative79.9%
unpow279.9%
rem-square-sqrt81.4%
Simplified81.4%
Final simplification55.0%
NOTE: im should be positive before calling this function
(FPCore (re im)
:precision binary64
(if (<= re -1.36e-24)
(* 0.5 (sqrt (/ (* im (- im)) re)))
(if (<= re 2.7e+21)
(* 0.5 (sqrt (* 2.0 (+ re im))))
(* 0.5 (* 2.0 (sqrt re))))))im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -1.36e-24) {
tmp = 0.5 * sqrt(((im * -im) / re));
} else if (re <= 2.7e+21) {
tmp = 0.5 * sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-1.36d-24)) then
tmp = 0.5d0 * sqrt(((im * -im) / re))
else if (re <= 2.7d+21) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -1.36e-24) {
tmp = 0.5 * Math.sqrt(((im * -im) / re));
} else if (re <= 2.7e+21) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -1.36e-24: tmp = 0.5 * math.sqrt(((im * -im) / re)) elif re <= 2.7e+21: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -1.36e-24) tmp = Float64(0.5 * sqrt(Float64(Float64(im * Float64(-im)) / re))); elseif (re <= 2.7e+21) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.36e-24) tmp = 0.5 * sqrt(((im * -im) / re)); elseif (re <= 2.7e+21) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -1.36e-24], N[(0.5 * N[Sqrt[N[(N[(im * (-im)), $MachinePrecision] / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.7e+21], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.36 \cdot 10^{-24}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot \left(-im\right)}{re}}\\
\mathbf{elif}\;re \leq 2.7 \cdot 10^{+21}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -1.36000000000000001e-24Initial program 5.4%
sqr-neg5.4%
+-commutative5.4%
sqr-neg5.4%
distribute-rgt-in5.4%
cancel-sign-sub5.4%
distribute-rgt-out--5.4%
sub-neg5.4%
remove-double-neg5.4%
hypot-def25.5%
Simplified25.5%
Taylor expanded in re around -inf 52.6%
associate-*r/52.6%
neg-mul-152.6%
unpow252.6%
distribute-rgt-neg-in52.6%
Simplified52.6%
if -1.36000000000000001e-24 < re < 2.7e21Initial program 57.2%
sqr-neg57.2%
+-commutative57.2%
sqr-neg57.2%
distribute-rgt-in57.2%
cancel-sign-sub57.2%
distribute-rgt-out--57.2%
sub-neg57.2%
remove-double-neg57.2%
hypot-def88.6%
Simplified88.6%
Taylor expanded in re around 0 38.5%
distribute-lft-out38.5%
*-commutative38.5%
Simplified38.5%
if 2.7e21 < re Initial program 35.7%
sqr-neg35.7%
+-commutative35.7%
sqr-neg35.7%
distribute-rgt-in35.7%
cancel-sign-sub35.7%
distribute-rgt-out--35.7%
sub-neg35.7%
remove-double-neg35.7%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 87.2%
*-commutative87.2%
unpow287.2%
rem-square-sqrt88.8%
Simplified88.8%
Final simplification54.1%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re 5.1e-36) (* 0.5 (sqrt (* 2.0 im))) (* 0.5 (* 2.0 (sqrt re)))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= 5.1e-36) {
tmp = 0.5 * sqrt((2.0 * im));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= 5.1d-36) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= 5.1e-36) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= 5.1e-36: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= 5.1e-36) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= 5.1e-36) tmp = 0.5 * sqrt((2.0 * im)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, 5.1e-36], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq 5.1 \cdot 10^{-36}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < 5.09999999999999973e-36Initial program 37.7%
sqr-neg37.7%
+-commutative37.7%
sqr-neg37.7%
distribute-rgt-in37.7%
cancel-sign-sub37.7%
distribute-rgt-out--37.7%
sub-neg37.7%
remove-double-neg37.7%
hypot-def67.3%
Simplified67.3%
Taylor expanded in re around 0 29.0%
*-commutative29.0%
Simplified29.0%
if 5.09999999999999973e-36 < re Initial program 46.9%
sqr-neg46.9%
+-commutative46.9%
sqr-neg46.9%
distribute-rgt-in46.9%
cancel-sign-sub46.9%
distribute-rgt-out--46.9%
sub-neg46.9%
remove-double-neg46.9%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 79.9%
*-commutative79.9%
unpow279.9%
rem-square-sqrt81.4%
Simplified81.4%
Final simplification46.0%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 im))))
im = abs(im);
double code(double re, double im) {
return 0.5 * sqrt((2.0 * im));
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * im))
end function
im = Math.abs(im);
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * im));
}
im = abs(im) def code(re, im): return 0.5 * math.sqrt((2.0 * im))
im = abs(im) function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * im))) end
im = abs(im) function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * im)); end
NOTE: im should be positive before calling this function code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Initial program 40.7%
sqr-neg40.7%
+-commutative40.7%
sqr-neg40.7%
distribute-rgt-in40.7%
cancel-sign-sub40.7%
distribute-rgt-out--40.7%
sub-neg40.7%
remove-double-neg40.7%
hypot-def77.9%
Simplified77.9%
Taylor expanded in re around 0 23.7%
*-commutative23.7%
Simplified23.7%
Final simplification23.7%
(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 2023279
(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)))))