
(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 8 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 (<= re -3e-17) (* 0.5 (pow (pow (exp 0.25) (fma 2.0 (log im) (log (/ -1.0 re)))) 2.0)) (sqrt (* 0.5 (+ re (hypot re im))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -3e-17) {
tmp = 0.5 * pow(pow(exp(0.25), fma(2.0, log(im), log((-1.0 / re)))), 2.0);
} else {
tmp = sqrt((0.5 * (re + hypot(re, im))));
}
return tmp;
}
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -3e-17) tmp = Float64(0.5 * ((exp(0.25) ^ fma(2.0, log(im), log(Float64(-1.0 / re)))) ^ 2.0)); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im)))); end return tmp end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -3e-17], 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[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3 \cdot 10^{-17}:\\
\;\;\;\;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}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -3.00000000000000006e-17Initial program 15.5%
Simplified36.4%
add-sqr-sqrt36.3%
pow236.3%
pow1/236.3%
sqrt-pow136.3%
metadata-eval36.3%
Applied egg-rr36.3%
Taylor expanded in re around -inf 59.5%
exp-prod57.5%
+-commutative57.5%
log-pow36.4%
fma-def36.4%
Simplified36.4%
if -3.00000000000000006e-17 < re Initial program 57.8%
Simplified96.1%
add-sqr-sqrt95.2%
sqrt-unprod96.1%
*-commutative96.1%
*-commutative96.1%
swap-sqr96.1%
add-sqr-sqrt96.1%
metadata-eval96.1%
Applied egg-rr96.1%
*-commutative96.1%
associate-*r*96.1%
metadata-eval96.1%
Simplified96.1%
Final simplification80.0%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -1.5e+121) (* 0.5 (pow (exp (* 0.25 (+ (log (/ -1.0 re)) (log (pow im 2.0))))) 2.0)) (sqrt (* 0.5 (+ re (hypot re im))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -1.5e+121) {
tmp = 0.5 * pow(exp((0.25 * (log((-1.0 / re)) + log(pow(im, 2.0))))), 2.0);
} else {
tmp = sqrt((0.5 * (re + hypot(re, im))));
}
return tmp;
}
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -1.5e+121) {
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 = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -1.5e+121: 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 = math.sqrt((0.5 * (re + math.hypot(re, im)))) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -1.5e+121) tmp = Float64(0.5 * (exp(Float64(0.25 * Float64(log(Float64(-1.0 / re)) + log((im ^ 2.0))))) ^ 2.0)); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im)))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.5e+121) tmp = 0.5 * (exp((0.25 * (log((-1.0 / re)) + log((im ^ 2.0))))) ^ 2.0); else tmp = sqrt((0.5 * (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.5e+121], 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[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.5 \cdot 10^{+121}:\\
\;\;\;\;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}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -1.5000000000000001e121Initial program 9.5%
Simplified31.8%
add-sqr-sqrt31.8%
pow231.8%
pow1/231.8%
sqrt-pow131.8%
metadata-eval31.8%
Applied egg-rr31.8%
Taylor expanded in re around -inf 74.1%
if -1.5000000000000001e121 < re Initial program 53.8%
Simplified89.7%
add-sqr-sqrt88.9%
sqrt-unprod89.7%
*-commutative89.7%
*-commutative89.7%
swap-sqr89.7%
add-sqr-sqrt89.7%
metadata-eval89.7%
Applied egg-rr89.7%
*-commutative89.7%
associate-*r*89.7%
metadata-eval89.7%
Simplified89.7%
Final simplification87.1%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -2.2e+162) (* 0.5 (* (sqrt (* -0.5 (* im (* im (/ 1.0 re))))) (sqrt 2.0))) (sqrt (* 0.5 (+ re (hypot re im))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -2.2e+162) {
tmp = 0.5 * (sqrt((-0.5 * (im * (im * (1.0 / re))))) * sqrt(2.0));
} else {
tmp = sqrt((0.5 * (re + hypot(re, im))));
}
return tmp;
}
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -2.2e+162) {
tmp = 0.5 * (Math.sqrt((-0.5 * (im * (im * (1.0 / re))))) * Math.sqrt(2.0));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -2.2e+162: tmp = 0.5 * (math.sqrt((-0.5 * (im * (im * (1.0 / re))))) * math.sqrt(2.0)) else: tmp = math.sqrt((0.5 * (re + math.hypot(re, im)))) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -2.2e+162) tmp = Float64(0.5 * Float64(sqrt(Float64(-0.5 * Float64(im * Float64(im * Float64(1.0 / re))))) * sqrt(2.0))); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im)))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.2e+162) tmp = 0.5 * (sqrt((-0.5 * (im * (im * (1.0 / re))))) * sqrt(2.0)); else tmp = sqrt((0.5 * (re + hypot(re, im)))); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -2.2e+162], N[(0.5 * N[(N[Sqrt[N[(-0.5 * N[(im * N[(im * N[(1.0 / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.2 \cdot 10^{+162}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{-0.5 \cdot \left(im \cdot \left(im \cdot \frac{1}{re}\right)\right)} \cdot \sqrt{2}\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -2.2000000000000002e162Initial program 2.6%
Simplified28.4%
sqrt-prod28.3%
*-commutative28.3%
Applied egg-rr28.3%
Taylor expanded in re around -inf 48.1%
div-inv48.1%
unpow248.1%
associate-*l*53.9%
Applied egg-rr53.9%
if -2.2000000000000002e162 < re Initial program 53.8%
Simplified88.7%
add-sqr-sqrt87.9%
sqrt-unprod88.7%
*-commutative88.7%
*-commutative88.7%
swap-sqr88.7%
add-sqr-sqrt88.7%
metadata-eval88.7%
Applied egg-rr88.7%
*-commutative88.7%
associate-*r*88.7%
metadata-eval88.7%
Simplified88.7%
Final simplification83.7%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -4.5e+157) (* 0.5 (* (sqrt 2.0) (sqrt (* -0.5 (* im (/ im re)))))) (sqrt (* 0.5 (+ re (hypot re im))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -4.5e+157) {
tmp = 0.5 * (sqrt(2.0) * sqrt((-0.5 * (im * (im / re)))));
} else {
tmp = sqrt((0.5 * (re + hypot(re, im))));
}
return tmp;
}
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -4.5e+157) {
tmp = 0.5 * (Math.sqrt(2.0) * Math.sqrt((-0.5 * (im * (im / re)))));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -4.5e+157: tmp = 0.5 * (math.sqrt(2.0) * math.sqrt((-0.5 * (im * (im / re))))) else: tmp = math.sqrt((0.5 * (re + math.hypot(re, im)))) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -4.5e+157) tmp = Float64(0.5 * Float64(sqrt(2.0) * sqrt(Float64(-0.5 * Float64(im * Float64(im / re)))))); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im)))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4.5e+157) tmp = 0.5 * (sqrt(2.0) * sqrt((-0.5 * (im * (im / re))))); else tmp = sqrt((0.5 * (re + hypot(re, im)))); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -4.5e+157], N[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[N[(-0.5 * N[(im * N[(im / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.5 \cdot 10^{+157}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{-0.5 \cdot \left(im \cdot \frac{im}{re}\right)}\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -4.49999999999999985e157Initial program 2.6%
Simplified28.4%
sqrt-prod28.3%
*-commutative28.3%
Applied egg-rr28.3%
Taylor expanded in re around -inf 48.1%
unpow248.1%
*-un-lft-identity48.1%
times-frac53.9%
/-rgt-identity53.9%
Applied egg-rr53.9%
if -4.49999999999999985e157 < re Initial program 53.8%
Simplified88.7%
add-sqr-sqrt87.9%
sqrt-unprod88.7%
*-commutative88.7%
*-commutative88.7%
swap-sqr88.7%
add-sqr-sqrt88.7%
metadata-eval88.7%
Applied egg-rr88.7%
*-commutative88.7%
associate-*r*88.7%
metadata-eval88.7%
Simplified88.7%
Final simplification83.7%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -2.1e+181) (* 0.5 (sqrt (- (/ (pow im 2.0) re)))) (sqrt (* 0.5 (+ re (hypot re im))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -2.1e+181) {
tmp = 0.5 * sqrt(-(pow(im, 2.0) / re));
} else {
tmp = sqrt((0.5 * (re + hypot(re, im))));
}
return tmp;
}
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -2.1e+181) {
tmp = 0.5 * Math.sqrt(-(Math.pow(im, 2.0) / re));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -2.1e+181: tmp = 0.5 * math.sqrt(-(math.pow(im, 2.0) / re)) else: tmp = math.sqrt((0.5 * (re + math.hypot(re, im)))) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -2.1e+181) tmp = Float64(0.5 * sqrt(Float64(-Float64((im ^ 2.0) / re)))); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im)))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.1e+181) tmp = 0.5 * sqrt(-((im ^ 2.0) / re)); else tmp = sqrt((0.5 * (re + hypot(re, im)))); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -2.1e+181], N[(0.5 * N[Sqrt[(-N[(N[Power[im, 2.0], $MachinePrecision] / re), $MachinePrecision])], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.1 \cdot 10^{+181}:\\
\;\;\;\;0.5 \cdot \sqrt{-\frac{{im}^{2}}{re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -2.09999999999999997e181Initial program 2.5%
Simplified24.7%
Taylor expanded in re around -inf 48.2%
mul-1-neg48.2%
Simplified48.2%
if -2.09999999999999997e181 < re Initial program 52.4%
Simplified87.6%
add-sqr-sqrt86.9%
sqrt-unprod87.6%
*-commutative87.6%
*-commutative87.6%
swap-sqr87.6%
add-sqr-sqrt87.6%
metadata-eval87.6%
Applied egg-rr87.6%
*-commutative87.6%
associate-*r*87.6%
metadata-eval87.6%
Simplified87.6%
Final simplification82.8%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (sqrt (* 0.5 (+ re (hypot re im)))))
im = abs(im);
double code(double re, double im) {
return sqrt((0.5 * (re + hypot(re, im))));
}
im = Math.abs(im);
public static double code(double re, double im) {
return Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
im = abs(im) def code(re, im): return math.sqrt((0.5 * (re + math.hypot(re, im))))
im = abs(im) function code(re, im) return sqrt(Float64(0.5 * Float64(re + hypot(re, im)))) end
im = abs(im) function tmp = code(re, im) tmp = sqrt((0.5 * (re + hypot(re, im)))); end
NOTE: im should be positive before calling this function code[re_, im_] := N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}
\end{array}
Initial program 46.4%
Simplified80.0%
add-sqr-sqrt79.3%
sqrt-unprod80.0%
*-commutative80.0%
*-commutative80.0%
swap-sqr80.0%
add-sqr-sqrt80.0%
metadata-eval80.0%
Applied egg-rr80.0%
*-commutative80.0%
associate-*r*80.0%
metadata-eval80.0%
Simplified80.0%
Final simplification80.0%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re 4.4e-42) (* 0.5 (sqrt (* 2.0 im))) (sqrt re)))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= 4.4e-42) {
tmp = 0.5 * sqrt((2.0 * im));
} else {
tmp = 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 <= 4.4d-42) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else
tmp = sqrt(re)
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= 4.4e-42) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= 4.4e-42: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = math.sqrt(re) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= 4.4e-42) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = sqrt(re); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= 4.4e-42) tmp = 0.5 * sqrt((2.0 * im)); else tmp = sqrt(re); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, 4.4e-42], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq 4.4 \cdot 10^{-42}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 4.4000000000000001e-42Initial program 45.6%
Simplified70.7%
Taylor expanded in re around 0 29.6%
*-commutative29.6%
Simplified29.6%
if 4.4000000000000001e-42 < re Initial program 48.0%
Simplified100.0%
Taylor expanded in im around 0 83.6%
*-commutative83.6%
unpow283.6%
rem-square-sqrt85.1%
associate-*r*85.1%
metadata-eval85.1%
*-lft-identity85.1%
Simplified85.1%
Final simplification47.2%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (sqrt re))
im = abs(im);
double code(double re, double im) {
return sqrt(re);
}
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 = sqrt(re)
end function
im = Math.abs(im);
public static double code(double re, double im) {
return Math.sqrt(re);
}
im = abs(im) def code(re, im): return math.sqrt(re)
im = abs(im) function code(re, im) return sqrt(re) end
im = abs(im) function tmp = code(re, im) tmp = sqrt(re); end
NOTE: im should be positive before calling this function code[re_, im_] := N[Sqrt[re], $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
\sqrt{re}
\end{array}
Initial program 46.4%
Simplified80.0%
Taylor expanded in im around 0 30.9%
*-commutative30.9%
unpow230.9%
rem-square-sqrt31.5%
associate-*r*31.5%
metadata-eval31.5%
*-lft-identity31.5%
Simplified31.5%
Final simplification31.5%
(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 2023314
(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)))))