
(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}
(FPCore (re im) :precision binary64 (if (<= (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))) 0.0) (* (pow re -0.5) (* im 0.5)) (sqrt (* 0.5 (- (hypot re im) re)))))
double code(double re, double im) {
double tmp;
if (sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0) {
tmp = pow(re, -0.5) * (im * 0.5);
} else {
tmp = sqrt((0.5 * (hypot(re, im) - re)));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) - re))) <= 0.0) {
tmp = Math.pow(re, -0.5) * (im * 0.5);
} else {
tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
}
return tmp;
}
def code(re, im): tmp = 0 if math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re))) <= 0.0: tmp = math.pow(re, -0.5) * (im * 0.5) else: tmp = math.sqrt((0.5 * (math.hypot(re, im) - re))) return tmp
function code(re, im) tmp = 0.0 if (sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re))) <= 0.0) tmp = Float64((re ^ -0.5) * Float64(im * 0.5)); else tmp = sqrt(Float64(0.5 * Float64(hypot(re, im) - re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0) tmp = (re ^ -0.5) * (im * 0.5); else tmp = sqrt((0.5 * (hypot(re, im) - re))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(N[Power[re, -0.5], $MachinePrecision] * N[(im * 0.5), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \leq 0:\\
\;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\end{array}
\end{array}
if (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 6.0%
add-sqr-sqrt6.0%
sqrt-unprod6.0%
*-commutative6.0%
*-commutative6.0%
swap-sqr6.0%
add-sqr-sqrt6.0%
*-commutative6.0%
hypot-define6.0%
metadata-eval6.0%
Applied egg-rr6.0%
associate-*l*6.0%
metadata-eval6.0%
Simplified6.0%
Taylor expanded in re around inf 98.2%
*-commutative98.2%
unpow298.2%
rem-square-sqrt99.8%
Simplified99.8%
*-un-lft-identity99.8%
inv-pow99.8%
sqrt-pow199.9%
metadata-eval99.9%
Applied egg-rr99.9%
*-lft-identity99.9%
Simplified99.9%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 48.7%
add-sqr-sqrt48.3%
sqrt-unprod48.7%
*-commutative48.7%
*-commutative48.7%
swap-sqr48.7%
add-sqr-sqrt48.7%
*-commutative48.7%
hypot-define92.2%
metadata-eval92.2%
Applied egg-rr92.2%
associate-*l*92.2%
metadata-eval92.2%
Simplified92.2%
Final simplification93.1%
(FPCore (re im)
:precision binary64
(if (<= re -7.5e-10)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 1.45e-106)
(sqrt (* 0.5 (* im (- 1.0 (/ re im)))))
(* (pow re -0.5) (* im 0.5)))))
double code(double re, double im) {
double tmp;
if (re <= -7.5e-10) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 1.45e-106) {
tmp = sqrt((0.5 * (im * (1.0 - (re / im)))));
} else {
tmp = pow(re, -0.5) * (im * 0.5);
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-7.5d-10)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 1.45d-106) then
tmp = sqrt((0.5d0 * (im * (1.0d0 - (re / im)))))
else
tmp = (re ** (-0.5d0)) * (im * 0.5d0)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -7.5e-10) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 1.45e-106) {
tmp = Math.sqrt((0.5 * (im * (1.0 - (re / im)))));
} else {
tmp = Math.pow(re, -0.5) * (im * 0.5);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -7.5e-10: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 1.45e-106: tmp = math.sqrt((0.5 * (im * (1.0 - (re / im))))) else: tmp = math.pow(re, -0.5) * (im * 0.5) return tmp
function code(re, im) tmp = 0.0 if (re <= -7.5e-10) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 1.45e-106) tmp = sqrt(Float64(0.5 * Float64(im * Float64(1.0 - Float64(re / im))))); else tmp = Float64((re ^ -0.5) * Float64(im * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -7.5e-10) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 1.45e-106) tmp = sqrt((0.5 * (im * (1.0 - (re / im))))); else tmp = (re ^ -0.5) * (im * 0.5); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -7.5e-10], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.45e-106], N[Sqrt[N[(0.5 * N[(im * N[(1.0 - N[(re / im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[Power[re, -0.5], $MachinePrecision] * N[(im * 0.5), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -7.5 \cdot 10^{-10}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 1.45 \cdot 10^{-106}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im \cdot \left(1 - \frac{re}{im}\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\
\end{array}
\end{array}
if re < -7.49999999999999995e-10Initial program 40.8%
sub-neg40.8%
sqr-neg40.8%
sub-neg40.8%
sqr-neg40.8%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around -inf 76.4%
*-commutative76.4%
Simplified76.4%
if -7.49999999999999995e-10 < re < 1.45e-106Initial program 61.5%
add-sqr-sqrt61.1%
sqrt-unprod61.5%
*-commutative61.5%
*-commutative61.5%
swap-sqr61.5%
add-sqr-sqrt61.5%
*-commutative61.5%
hypot-define97.3%
metadata-eval97.3%
Applied egg-rr97.3%
associate-*l*97.3%
metadata-eval97.3%
Simplified97.3%
Taylor expanded in im around inf 86.8%
mul-1-neg86.8%
unsub-neg86.8%
Simplified86.8%
if 1.45e-106 < re Initial program 20.0%
add-sqr-sqrt19.9%
sqrt-unprod20.0%
*-commutative20.0%
*-commutative20.0%
swap-sqr20.0%
add-sqr-sqrt20.0%
*-commutative20.0%
hypot-define39.9%
metadata-eval39.9%
Applied egg-rr39.9%
associate-*l*39.9%
metadata-eval39.9%
Simplified39.9%
Taylor expanded in re around inf 73.7%
*-commutative73.7%
unpow273.7%
rem-square-sqrt74.7%
Simplified74.7%
*-un-lft-identity74.7%
inv-pow74.7%
sqrt-pow174.8%
metadata-eval74.8%
Applied egg-rr74.8%
*-lft-identity74.8%
Simplified74.8%
Final simplification80.3%
(FPCore (re im) :precision binary64 (if (<= re -1.25e-9) (* 0.5 (sqrt (* re -4.0))) (if (<= re 8e-107) (sqrt (* 0.5 (- im re))) (* (pow re -0.5) (* im 0.5)))))
double code(double re, double im) {
double tmp;
if (re <= -1.25e-9) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 8e-107) {
tmp = sqrt((0.5 * (im - re)));
} else {
tmp = pow(re, -0.5) * (im * 0.5);
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-1.25d-9)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 8d-107) then
tmp = sqrt((0.5d0 * (im - re)))
else
tmp = (re ** (-0.5d0)) * (im * 0.5d0)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.25e-9) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 8e-107) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = Math.pow(re, -0.5) * (im * 0.5);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.25e-9: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 8e-107: tmp = math.sqrt((0.5 * (im - re))) else: tmp = math.pow(re, -0.5) * (im * 0.5) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.25e-9) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 8e-107) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64((re ^ -0.5) * Float64(im * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.25e-9) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 8e-107) tmp = sqrt((0.5 * (im - re))); else tmp = (re ^ -0.5) * (im * 0.5); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.25e-9], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 8e-107], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[Power[re, -0.5], $MachinePrecision] * N[(im * 0.5), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.25 \cdot 10^{-9}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 8 \cdot 10^{-107}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\
\end{array}
\end{array}
if re < -1.25e-9Initial program 40.8%
sub-neg40.8%
sqr-neg40.8%
sub-neg40.8%
sqr-neg40.8%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around -inf 76.4%
*-commutative76.4%
Simplified76.4%
if -1.25e-9 < re < 8e-107Initial program 61.5%
add-sqr-sqrt61.1%
sqrt-unprod61.5%
*-commutative61.5%
*-commutative61.5%
swap-sqr61.5%
add-sqr-sqrt61.5%
*-commutative61.5%
hypot-define97.3%
metadata-eval97.3%
Applied egg-rr97.3%
associate-*l*97.3%
metadata-eval97.3%
Simplified97.3%
Taylor expanded in re around 0 86.8%
neg-mul-186.8%
unsub-neg86.8%
Simplified86.8%
if 8e-107 < re Initial program 20.0%
add-sqr-sqrt19.9%
sqrt-unprod20.0%
*-commutative20.0%
*-commutative20.0%
swap-sqr20.0%
add-sqr-sqrt20.0%
*-commutative20.0%
hypot-define39.9%
metadata-eval39.9%
Applied egg-rr39.9%
associate-*l*39.9%
metadata-eval39.9%
Simplified39.9%
Taylor expanded in re around inf 73.7%
*-commutative73.7%
unpow273.7%
rem-square-sqrt74.7%
Simplified74.7%
*-un-lft-identity74.7%
inv-pow74.7%
sqrt-pow174.8%
metadata-eval74.8%
Applied egg-rr74.8%
*-lft-identity74.8%
Simplified74.8%
Final simplification80.3%
(FPCore (re im) :precision binary64 (if (<= re -1.8e-12) (* 0.5 (sqrt (* re -4.0))) (if (<= re 3.9e-107) (sqrt (* 0.5 (- im re))) (* im (/ 0.5 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -1.8e-12) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 3.9e-107) {
tmp = sqrt((0.5 * (im - re)));
} else {
tmp = im * (0.5 / sqrt(re));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-1.8d-12)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 3.9d-107) then
tmp = sqrt((0.5d0 * (im - re)))
else
tmp = im * (0.5d0 / sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.8e-12) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 3.9e-107) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = im * (0.5 / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.8e-12: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 3.9e-107: tmp = math.sqrt((0.5 * (im - re))) else: tmp = im * (0.5 / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.8e-12) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 3.9e-107) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64(im * Float64(0.5 / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.8e-12) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 3.9e-107) tmp = sqrt((0.5 * (im - re))); else tmp = im * (0.5 / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.8e-12], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.9e-107], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(im * N[(0.5 / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.8 \cdot 10^{-12}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 3.9 \cdot 10^{-107}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -1.8e-12Initial program 40.8%
sub-neg40.8%
sqr-neg40.8%
sub-neg40.8%
sqr-neg40.8%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around -inf 76.4%
*-commutative76.4%
Simplified76.4%
if -1.8e-12 < re < 3.9000000000000001e-107Initial program 61.5%
add-sqr-sqrt61.1%
sqrt-unprod61.5%
*-commutative61.5%
*-commutative61.5%
swap-sqr61.5%
add-sqr-sqrt61.5%
*-commutative61.5%
hypot-define97.3%
metadata-eval97.3%
Applied egg-rr97.3%
associate-*l*97.3%
metadata-eval97.3%
Simplified97.3%
Taylor expanded in re around 0 86.8%
neg-mul-186.8%
unsub-neg86.8%
Simplified86.8%
if 3.9000000000000001e-107 < re Initial program 20.0%
add-sqr-sqrt19.9%
sqrt-unprod20.0%
*-commutative20.0%
*-commutative20.0%
swap-sqr20.0%
add-sqr-sqrt20.0%
*-commutative20.0%
hypot-define39.9%
metadata-eval39.9%
Applied egg-rr39.9%
associate-*l*39.9%
metadata-eval39.9%
Simplified39.9%
Taylor expanded in re around inf 73.7%
*-commutative73.7%
unpow273.7%
rem-square-sqrt74.7%
Simplified74.7%
*-commutative74.7%
sqrt-div74.6%
metadata-eval74.6%
un-div-inv74.7%
Applied egg-rr74.7%
associate-/l*74.6%
Simplified74.6%
Final simplification80.2%
(FPCore (re im) :precision binary64 (if (<= re -1.15e-6) (* 0.5 (sqrt (* re -4.0))) (if (<= re 1.45e-106) (sqrt (* 0.5 (- im re))) (/ (* im 0.5) (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= -1.15e-6) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 1.45e-106) {
tmp = sqrt((0.5 * (im - re)));
} else {
tmp = (im * 0.5) / sqrt(re);
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-1.15d-6)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 1.45d-106) then
tmp = sqrt((0.5d0 * (im - re)))
else
tmp = (im * 0.5d0) / sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.15e-6) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 1.45e-106) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = (im * 0.5) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.15e-6: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 1.45e-106: tmp = math.sqrt((0.5 * (im - re))) else: tmp = (im * 0.5) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.15e-6) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 1.45e-106) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64(Float64(im * 0.5) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.15e-6) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 1.45e-106) tmp = sqrt((0.5 * (im - re))); else tmp = (im * 0.5) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.15e-6], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.45e-106], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.15 \cdot 10^{-6}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 1.45 \cdot 10^{-106}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -1.15e-6Initial program 40.8%
sub-neg40.8%
sqr-neg40.8%
sub-neg40.8%
sqr-neg40.8%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around -inf 76.4%
*-commutative76.4%
Simplified76.4%
if -1.15e-6 < re < 1.45e-106Initial program 61.5%
add-sqr-sqrt61.1%
sqrt-unprod61.5%
*-commutative61.5%
*-commutative61.5%
swap-sqr61.5%
add-sqr-sqrt61.5%
*-commutative61.5%
hypot-define97.3%
metadata-eval97.3%
Applied egg-rr97.3%
associate-*l*97.3%
metadata-eval97.3%
Simplified97.3%
Taylor expanded in re around 0 86.8%
neg-mul-186.8%
unsub-neg86.8%
Simplified86.8%
if 1.45e-106 < re Initial program 20.0%
add-sqr-sqrt19.9%
sqrt-unprod20.0%
*-commutative20.0%
*-commutative20.0%
swap-sqr20.0%
add-sqr-sqrt20.0%
*-commutative20.0%
hypot-define39.9%
metadata-eval39.9%
Applied egg-rr39.9%
associate-*l*39.9%
metadata-eval39.9%
Simplified39.9%
Taylor expanded in re around inf 73.7%
*-commutative73.7%
unpow273.7%
rem-square-sqrt74.7%
Simplified74.7%
*-commutative74.7%
sqrt-div74.6%
metadata-eval74.6%
un-div-inv74.7%
Applied egg-rr74.7%
Final simplification80.3%
(FPCore (re im) :precision binary64 (if (<= re -6.8e-16) (* 0.5 (sqrt (* re -4.0))) (sqrt (* im 0.5))))
double code(double re, double im) {
double tmp;
if (re <= -6.8e-16) {
tmp = 0.5 * sqrt((re * -4.0));
} else {
tmp = sqrt((im * 0.5));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-6.8d-16)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else
tmp = sqrt((im * 0.5d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -6.8e-16) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -6.8e-16: tmp = 0.5 * math.sqrt((re * -4.0)) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -6.8e-16) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); else tmp = sqrt(Float64(im * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -6.8e-16) tmp = 0.5 * sqrt((re * -4.0)); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -6.8e-16], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -6.8 \cdot 10^{-16}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < -6.8e-16Initial program 40.8%
sub-neg40.8%
sqr-neg40.8%
sub-neg40.8%
sqr-neg40.8%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around -inf 76.4%
*-commutative76.4%
Simplified76.4%
if -6.8e-16 < re Initial program 44.7%
add-sqr-sqrt44.4%
sqrt-unprod44.7%
*-commutative44.7%
*-commutative44.7%
swap-sqr44.7%
add-sqr-sqrt44.7%
*-commutative44.7%
hypot-define74.0%
metadata-eval74.0%
Applied egg-rr74.0%
associate-*l*74.0%
metadata-eval74.0%
Simplified74.0%
Taylor expanded in re around 0 62.4%
Final simplification66.6%
(FPCore (re im) :precision binary64 (sqrt (* 0.5 (- im re))))
double code(double re, double im) {
return sqrt((0.5 * (im - re)));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt((0.5d0 * (im - re)))
end function
public static double code(double re, double im) {
return Math.sqrt((0.5 * (im - re)));
}
def code(re, im): return math.sqrt((0.5 * (im - re)))
function code(re, im) return sqrt(Float64(0.5 * Float64(im - re))) end
function tmp = code(re, im) tmp = sqrt((0.5 * (im - re))); end
code[re_, im_] := N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot \left(im - re\right)}
\end{array}
Initial program 43.5%
add-sqr-sqrt43.2%
sqrt-unprod43.5%
*-commutative43.5%
*-commutative43.5%
swap-sqr43.5%
add-sqr-sqrt43.5%
*-commutative43.5%
hypot-define81.7%
metadata-eval81.7%
Applied egg-rr81.7%
associate-*l*81.7%
metadata-eval81.7%
Simplified81.7%
Taylor expanded in re around 0 55.0%
neg-mul-155.0%
unsub-neg55.0%
Simplified55.0%
Final simplification55.0%
(FPCore (re im) :precision binary64 (sqrt (* im 0.5)))
double code(double re, double im) {
return sqrt((im * 0.5));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt((im * 0.5d0))
end function
public static double code(double re, double im) {
return Math.sqrt((im * 0.5));
}
def code(re, im): return math.sqrt((im * 0.5))
function code(re, im) return sqrt(Float64(im * 0.5)) end
function tmp = code(re, im) tmp = sqrt((im * 0.5)); end
code[re_, im_] := N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{im \cdot 0.5}
\end{array}
Initial program 43.5%
add-sqr-sqrt43.2%
sqrt-unprod43.5%
*-commutative43.5%
*-commutative43.5%
swap-sqr43.5%
add-sqr-sqrt43.5%
*-commutative43.5%
hypot-define81.7%
metadata-eval81.7%
Applied egg-rr81.7%
associate-*l*81.7%
metadata-eval81.7%
Simplified81.7%
Taylor expanded in re around 0 51.5%
Final simplification51.5%
herbie shell --seed 2024076
(FPCore (re im)
:name "math.sqrt on complex, imaginary part, im greater than 0 branch"
:precision binary64
:pre (> im 0.0)
(* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))