
(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 (exp 0.5) (+ (* 2.0 (log im)) (log (/ -1.0 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 * pow(exp(0.5), ((2.0 * log(im)) + log((-1.0 / 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.pow(Math.exp(0.5), ((2.0 * Math.log(im)) + Math.log((-1.0 / 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.pow(math.exp(0.5), ((2.0 * math.log(im)) + math.log((-1.0 / 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 * (exp(0.5) ^ Float64(Float64(2.0 * log(im)) + log(Float64(-1.0 / 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 * (exp(0.5) ^ ((2.0 * log(im)) + log((-1.0 / 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[Power[N[Exp[0.5], $MachinePrecision], N[(N[(2.0 * N[Log[im], $MachinePrecision]), $MachinePrecision] + N[Log[N[(-1.0 / 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]]
\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(e^{0.5}\right)}^{\left(2 \cdot \log im + \log \left(\frac{-1}{re}\right)\right)}\\
\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 18.9%
+-commutative18.9%
hypot-def18.9%
Simplified18.9%
*-commutative18.9%
sqrt-prod18.9%
Applied egg-rr18.9%
sqrt-unprod18.9%
pow1/218.9%
exp-to-pow18.9%
*-commutative18.9%
exp-prod18.9%
Applied egg-rr18.9%
Taylor expanded in re around -inf 43.3%
exp-prod43.0%
log-pow65.1%
Simplified65.1%
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 44.5%
+-commutative44.5%
hypot-def89.6%
Simplified89.6%
Final simplification86.6%
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 (exp (* 0.5 (+ (* 2.0 (log im)) (log (/ -1.0 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 * exp((0.5 * ((2.0 * log(im)) + log((-1.0 / 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.exp((0.5 * ((2.0 * Math.log(im)) + Math.log((-1.0 / 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.exp((0.5 * ((2.0 * math.log(im)) + math.log((-1.0 / 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 * exp(Float64(0.5 * Float64(Float64(2.0 * log(im)) + log(Float64(-1.0 / 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 * exp((0.5 * ((2.0 * log(im)) + log((-1.0 / 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[Exp[N[(0.5 * N[(N[(2.0 * N[Log[im], $MachinePrecision]), $MachinePrecision] + N[Log[N[(-1.0 / re), $MachinePrecision]], $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]]
\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 e^{0.5 \cdot \left(2 \cdot \log im + \log \left(\frac{-1}{re}\right)\right)}\\
\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 18.9%
+-commutative18.9%
hypot-def18.9%
Simplified18.9%
pow1/218.9%
pow-to-exp18.9%
*-commutative18.9%
Applied egg-rr18.9%
Taylor expanded in re around -inf 43.3%
log-pow64.4%
Simplified64.4%
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 44.5%
+-commutative44.5%
hypot-def89.6%
Simplified89.6%
Final simplification86.5%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -5.5e+159) (* 0.5 (sqrt (* 2.0 (* (/ im (/ re im)) -0.5)))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -5.5e+159) {
tmp = 0.5 * sqrt((2.0 * ((im / (re / im)) * -0.5)));
} 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 <= -5.5e+159) {
tmp = 0.5 * Math.sqrt((2.0 * ((im / (re / im)) * -0.5)));
} 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 <= -5.5e+159: tmp = 0.5 * math.sqrt((2.0 * ((im / (re / im)) * -0.5))) 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 <= -5.5e+159) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(Float64(im / Float64(re / im)) * -0.5)))); 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 <= -5.5e+159) tmp = 0.5 * sqrt((2.0 * ((im / (re / im)) * -0.5))); 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, -5.5e+159], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[(im / N[(re / im), $MachinePrecision]), $MachinePrecision] * -0.5), $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]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -5.5 \cdot 10^{+159}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -5.4999999999999998e159Initial program 2.8%
+-commutative2.8%
hypot-def32.3%
Simplified32.3%
Taylor expanded in re around -inf 54.0%
*-commutative54.0%
unpow254.0%
associate-/l*79.1%
Simplified79.1%
if -5.4999999999999998e159 < re Initial program 46.0%
+-commutative46.0%
hypot-def86.8%
Simplified86.8%
Final simplification86.0%
NOTE: im should be positive before calling this function
(FPCore (re im)
:precision binary64
(if (<= re -1.25e+62)
(* 0.5 (sqrt (* 2.0 (* im (/ im (/ re -0.5))))))
(if (<= re 8.2e+61)
(* 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.25e+62) {
tmp = 0.5 * sqrt((2.0 * (im * (im / (re / -0.5)))));
} else if (re <= 8.2e+61) {
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.25d+62)) then
tmp = 0.5d0 * sqrt((2.0d0 * (im * (im / (re / (-0.5d0))))))
else if (re <= 8.2d+61) 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.25e+62) {
tmp = 0.5 * Math.sqrt((2.0 * (im * (im / (re / -0.5)))));
} else if (re <= 8.2e+61) {
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.25e+62: tmp = 0.5 * math.sqrt((2.0 * (im * (im / (re / -0.5))))) elif re <= 8.2e+61: 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.25e+62) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im * Float64(im / Float64(re / -0.5)))))); elseif (re <= 8.2e+61) 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.25e+62) tmp = 0.5 * sqrt((2.0 * (im * (im / (re / -0.5))))); elseif (re <= 8.2e+61) 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.25e+62], N[(0.5 * N[Sqrt[N[(2.0 * N[(im * N[(im / N[(re / -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 8.2e+61], 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.25 \cdot 10^{+62}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im \cdot \frac{im}{\frac{re}{-0.5}}\right)}\\
\mathbf{elif}\;re \leq 8.2 \cdot 10^{+61}:\\
\;\;\;\;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.25000000000000007e62Initial program 13.0%
Taylor expanded in re around -inf 21.4%
+-commutative21.4%
mul-1-neg21.4%
unsub-neg21.4%
*-commutative21.4%
unpow221.4%
associate-/l*21.7%
Simplified21.7%
Taylor expanded in im around 0 50.7%
unpow250.7%
associate-*r/64.4%
*-commutative64.4%
associate-*l*64.4%
associate-/r/64.4%
Simplified64.4%
if -1.25000000000000007e62 < re < 8.19999999999999944e61Initial program 53.9%
+-commutative53.9%
hypot-def88.1%
Simplified88.1%
Taylor expanded in re around 0 43.5%
if 8.19999999999999944e61 < re Initial program 33.8%
+-commutative33.8%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 79.2%
unpow279.2%
rem-square-sqrt80.7%
Simplified80.7%
Final simplification55.7%
NOTE: im should be positive before calling this function
(FPCore (re im)
:precision binary64
(if (<= re -2.45e+63)
(* 0.5 (sqrt (* 2.0 (* (/ im (/ re im)) -0.5))))
(if (<= re 2.2e+61)
(* 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 <= -2.45e+63) {
tmp = 0.5 * sqrt((2.0 * ((im / (re / im)) * -0.5)));
} else if (re <= 2.2e+61) {
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 <= (-2.45d+63)) then
tmp = 0.5d0 * sqrt((2.0d0 * ((im / (re / im)) * (-0.5d0))))
else if (re <= 2.2d+61) 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 <= -2.45e+63) {
tmp = 0.5 * Math.sqrt((2.0 * ((im / (re / im)) * -0.5)));
} else if (re <= 2.2e+61) {
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 <= -2.45e+63: tmp = 0.5 * math.sqrt((2.0 * ((im / (re / im)) * -0.5))) elif re <= 2.2e+61: 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 <= -2.45e+63) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(Float64(im / Float64(re / im)) * -0.5)))); elseif (re <= 2.2e+61) 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 <= -2.45e+63) tmp = 0.5 * sqrt((2.0 * ((im / (re / im)) * -0.5))); elseif (re <= 2.2e+61) 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, -2.45e+63], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[(im / N[(re / im), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.2e+61], 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 -2.45 \cdot 10^{+63}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\right)}\\
\mathbf{elif}\;re \leq 2.2 \cdot 10^{+61}:\\
\;\;\;\;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 < -2.4499999999999998e63Initial program 13.0%
+-commutative13.0%
hypot-def39.9%
Simplified39.9%
Taylor expanded in re around -inf 50.7%
*-commutative50.7%
unpow250.7%
associate-/l*64.4%
Simplified64.4%
if -2.4499999999999998e63 < re < 2.2e61Initial program 53.9%
+-commutative53.9%
hypot-def88.1%
Simplified88.1%
Taylor expanded in re around 0 43.5%
if 2.2e61 < re Initial program 33.8%
+-commutative33.8%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 79.2%
unpow279.2%
rem-square-sqrt80.7%
Simplified80.7%
Final simplification55.7%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re 7.8e+60) (* 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 <= 7.8e+60) {
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 <= 7.8d+60) 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 <= 7.8e+60) {
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 <= 7.8e+60: 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 <= 7.8e+60) 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 <= 7.8e+60) 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, 7.8e+60], 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 7.8 \cdot 10^{+60}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < 7.8000000000000006e60Initial program 43.5%
+-commutative43.5%
hypot-def75.8%
Simplified75.8%
Taylor expanded in re around 0 34.2%
if 7.8000000000000006e60 < re Initial program 33.8%
+-commutative33.8%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 79.2%
unpow279.2%
rem-square-sqrt80.7%
Simplified80.7%
Final simplification44.2%
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 41.4%
+-commutative41.4%
hypot-def81.0%
Simplified81.0%
Taylor expanded in re around 0 29.4%
Final simplification29.4%
(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 2023201
(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)))))