
(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 6 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 -3.7e+31) (* 0.5 (exp (* 0.5 (+ (* 2.0 (log im)) (log (/ -1.0 re)))))) (sqrt (* 0.5 (+ re (hypot re im))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -3.7e+31) {
tmp = 0.5 * exp((0.5 * ((2.0 * log(im)) + log((-1.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 <= -3.7e+31) {
tmp = 0.5 * Math.exp((0.5 * ((2.0 * Math.log(im)) + Math.log((-1.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 <= -3.7e+31: tmp = 0.5 * math.exp((0.5 * ((2.0 * math.log(im)) + math.log((-1.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 <= -3.7e+31) tmp = Float64(0.5 * exp(Float64(0.5 * Float64(Float64(2.0 * log(im)) + log(Float64(-1.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 <= -3.7e+31) tmp = 0.5 * exp((0.5 * ((2.0 * log(im)) + log((-1.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, -3.7e+31], 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[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.7 \cdot 10^{+31}:\\
\;\;\;\;0.5 \cdot e^{0.5 \cdot \left(2 \cdot \log im + \log \left(\frac{-1}{re}\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -3.6999999999999998e31Initial program 5.8%
sqr-neg5.8%
+-commutative5.8%
sqr-neg5.8%
distribute-rgt-in5.8%
cancel-sign-sub5.8%
distribute-rgt-out--5.8%
sub-neg5.8%
remove-double-neg5.8%
hypot-def35.6%
Simplified35.6%
Taylor expanded in re around -inf 29.1%
+-commutative29.1%
mul-1-neg29.1%
unsub-neg29.1%
associate-*r/29.1%
unpow229.1%
associate-/l*28.8%
Simplified28.8%
pow1/228.9%
pow-to-exp27.8%
Applied egg-rr28.1%
Taylor expanded in im around 0 34.6%
+-commutative34.6%
distribute-neg-frac34.6%
metadata-eval34.6%
Simplified34.6%
if -3.6999999999999998e31 < re Initial program 51.1%
sqr-neg51.1%
+-commutative51.1%
sqr-neg51.1%
distribute-rgt-in51.1%
cancel-sign-sub51.1%
distribute-rgt-out--51.1%
sub-neg51.1%
remove-double-neg51.1%
hypot-def94.3%
Simplified94.3%
add-sqr-sqrt93.6%
sqrt-unprod94.3%
*-commutative94.3%
*-commutative94.3%
swap-sqr94.3%
add-sqr-sqrt94.3%
metadata-eval94.3%
Applied egg-rr94.3%
*-commutative94.3%
associate-*r*94.3%
metadata-eval94.3%
Simplified94.3%
Final simplification81.0%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= (+ re (sqrt (+ (* re re) (* im im)))) 0.0) (* 0.5 (sqrt (/ (- im) (/ re im)))) (sqrt (* 0.5 (+ re (hypot re im))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if ((re + sqrt(((re * re) + (im * im)))) <= 0.0) {
tmp = 0.5 * sqrt((-im / (re / im)));
} 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 + Math.sqrt(((re * re) + (im * im)))) <= 0.0) {
tmp = 0.5 * Math.sqrt((-im / (re / im)));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if (re + math.sqrt(((re * re) + (im * im)))) <= 0.0: tmp = 0.5 * math.sqrt((-im / (re / im))) else: tmp = math.sqrt((0.5 * (re + math.hypot(re, im)))) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))) <= 0.0) tmp = Float64(0.5 * sqrt(Float64(Float64(-im) / Float64(re / im)))); 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 + sqrt(((re * re) + (im * im)))) <= 0.0) tmp = 0.5 * sqrt((-im / (re / im))); 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[N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 0.0], N[(0.5 * N[Sqrt[N[((-im) / N[(re / im), $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 + \sqrt{re \cdot re + im \cdot im} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{-im}{\frac{re}{im}}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if (+.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-def15.3%
Simplified15.3%
Taylor expanded in re around -inf 28.1%
+-commutative28.1%
mul-1-neg28.1%
unsub-neg28.1%
associate-*r/28.1%
unpow228.1%
associate-/l*28.1%
Simplified28.1%
Taylor expanded in im around 0 32.9%
mul-1-neg32.9%
unpow232.9%
associate-/l*47.0%
Simplified47.0%
if 0.0 < (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 46.0%
sqr-neg46.0%
+-commutative46.0%
sqr-neg46.0%
distribute-rgt-in46.0%
cancel-sign-sub46.0%
distribute-rgt-out--46.0%
sub-neg46.0%
remove-double-neg46.0%
hypot-def90.3%
Simplified90.3%
add-sqr-sqrt89.7%
sqrt-unprod90.3%
*-commutative90.3%
*-commutative90.3%
swap-sqr90.3%
add-sqr-sqrt90.3%
metadata-eval90.3%
Applied egg-rr90.3%
*-commutative90.3%
associate-*r*90.3%
metadata-eval90.3%
Simplified90.3%
Final simplification85.0%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -1.26e+92) (* 0.5 (sqrt (/ (- im) (/ re im)))) (if (<= re 1.6e-33) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -1.26e+92) {
tmp = 0.5 * sqrt((-im / (re / im)));
} else if (re <= 1.6e-33) {
tmp = 0.5 * sqrt((2.0 * (re + 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 <= (-1.26d+92)) then
tmp = 0.5d0 * sqrt((-im / (re / im)))
else if (re <= 1.6d-33) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + 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 <= -1.26e+92) {
tmp = 0.5 * Math.sqrt((-im / (re / im)));
} else if (re <= 1.6e-33) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -1.26e+92: tmp = 0.5 * math.sqrt((-im / (re / im))) elif re <= 1.6e-33: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = math.sqrt(re) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -1.26e+92) tmp = Float64(0.5 * sqrt(Float64(Float64(-im) / Float64(re / im)))); elseif (re <= 1.6e-33) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = sqrt(re); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.26e+92) tmp = 0.5 * sqrt((-im / (re / im))); elseif (re <= 1.6e-33) tmp = 0.5 * sqrt((2.0 * (re + 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, -1.26e+92], N[(0.5 * N[Sqrt[N[((-im) / N[(re / im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.6e-33], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.26 \cdot 10^{+92}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{-im}{\frac{re}{im}}}\\
\mathbf{elif}\;re \leq 1.6 \cdot 10^{-33}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -1.26e92Initial program 3.8%
sqr-neg3.8%
+-commutative3.8%
sqr-neg3.8%
distribute-rgt-in3.8%
cancel-sign-sub3.8%
distribute-rgt-out--3.8%
sub-neg3.8%
remove-double-neg3.8%
hypot-def33.0%
Simplified33.0%
Taylor expanded in re around -inf 32.4%
+-commutative32.4%
mul-1-neg32.4%
unsub-neg32.4%
associate-*r/32.4%
unpow232.4%
associate-/l*31.9%
Simplified31.9%
Taylor expanded in im around 0 39.9%
mul-1-neg39.9%
unpow239.9%
associate-/l*44.5%
Simplified44.5%
if -1.26e92 < re < 1.59999999999999988e-33Initial program 51.5%
sqr-neg51.5%
+-commutative51.5%
sqr-neg51.5%
distribute-rgt-in51.5%
cancel-sign-sub51.5%
distribute-rgt-out--51.5%
sub-neg51.5%
remove-double-neg51.5%
hypot-def87.9%
Simplified87.9%
Taylor expanded in re around 0 36.5%
if 1.59999999999999988e-33 < re Initial program 43.9%
sqr-neg43.9%
+-commutative43.9%
sqr-neg43.9%
distribute-rgt-in43.9%
cancel-sign-sub43.9%
distribute-rgt-out--43.9%
sub-neg43.9%
remove-double-neg43.9%
hypot-def98.6%
Simplified98.6%
Taylor expanded in im around 0 76.4%
*-commutative76.4%
unpow276.4%
rem-square-sqrt77.9%
associate-*r*77.9%
metadata-eval77.9%
*-lft-identity77.9%
Simplified77.9%
Final simplification49.2%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re 8.2e-33) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re)))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= 8.2e-33) {
tmp = 0.5 * sqrt((2.0 * (re + 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 <= 8.2d-33) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + 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 <= 8.2e-33) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= 8.2e-33: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = math.sqrt(re) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= 8.2e-33) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = sqrt(re); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= 8.2e-33) tmp = 0.5 * sqrt((2.0 * (re + 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, 8.2e-33], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq 8.2 \cdot 10^{-33}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 8.2e-33Initial program 39.9%
sqr-neg39.9%
+-commutative39.9%
sqr-neg39.9%
distribute-rgt-in39.9%
cancel-sign-sub39.9%
distribute-rgt-out--39.9%
sub-neg39.9%
remove-double-neg39.9%
hypot-def74.6%
Simplified74.6%
Taylor expanded in re around 0 30.4%
if 8.2e-33 < re Initial program 43.9%
sqr-neg43.9%
+-commutative43.9%
sqr-neg43.9%
distribute-rgt-in43.9%
cancel-sign-sub43.9%
distribute-rgt-out--43.9%
sub-neg43.9%
remove-double-neg43.9%
hypot-def98.6%
Simplified98.6%
Taylor expanded in im around 0 76.4%
*-commutative76.4%
unpow276.4%
rem-square-sqrt77.9%
associate-*r*77.9%
metadata-eval77.9%
*-lft-identity77.9%
Simplified77.9%
Final simplification43.4%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re 5.5e-35) (* 0.5 (sqrt (* 2.0 im))) (sqrt re)))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= 5.5e-35) {
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 <= 5.5d-35) 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 <= 5.5e-35) {
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 <= 5.5e-35: 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 <= 5.5e-35) 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 <= 5.5e-35) 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, 5.5e-35], 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 5.5 \cdot 10^{-35}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 5.4999999999999997e-35Initial program 39.9%
sqr-neg39.9%
+-commutative39.9%
sqr-neg39.9%
distribute-rgt-in39.9%
cancel-sign-sub39.9%
distribute-rgt-out--39.9%
sub-neg39.9%
remove-double-neg39.9%
hypot-def74.6%
Simplified74.6%
Taylor expanded in re around 0 29.5%
*-commutative29.5%
Simplified29.5%
if 5.4999999999999997e-35 < re Initial program 43.9%
sqr-neg43.9%
+-commutative43.9%
sqr-neg43.9%
distribute-rgt-in43.9%
cancel-sign-sub43.9%
distribute-rgt-out--43.9%
sub-neg43.9%
remove-double-neg43.9%
hypot-def98.6%
Simplified98.6%
Taylor expanded in im around 0 76.4%
*-commutative76.4%
unpow276.4%
rem-square-sqrt77.9%
associate-*r*77.9%
metadata-eval77.9%
*-lft-identity77.9%
Simplified77.9%
Final simplification42.8%
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 41.0%
sqr-neg41.0%
+-commutative41.0%
sqr-neg41.0%
distribute-rgt-in41.0%
cancel-sign-sub41.0%
distribute-rgt-out--41.0%
sub-neg41.0%
remove-double-neg41.0%
hypot-def81.2%
Simplified81.2%
Taylor expanded in im around 0 27.6%
*-commutative27.6%
unpow227.6%
rem-square-sqrt28.1%
associate-*r*28.1%
metadata-eval28.1%
*-lft-identity28.1%
Simplified28.1%
Final simplification28.1%
(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 2023277
(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)))))