
(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 5 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) (/ (* im 0.5) (sqrt re)) (* 0.5 (sqrt (* 2.0 (- (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 = (im * 0.5) / sqrt(re);
} else {
tmp = 0.5 * sqrt((2.0 * (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 = (im * 0.5) / Math.sqrt(re);
} else {
tmp = 0.5 * Math.sqrt((2.0 * (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 = (im * 0.5) / math.sqrt(re) else: tmp = 0.5 * math.sqrt((2.0 * (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(Float64(im * 0.5) / sqrt(re)); else tmp = Float64(0.5 * sqrt(Float64(2.0 * 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 = (im * 0.5) / sqrt(re); else tmp = 0.5 * sqrt((2.0 * (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[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $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:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\end{array}
\end{array}
if (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 6.9%
hypot-def6.9%
Simplified6.9%
Taylor expanded in re around inf 48.8%
unpow248.8%
associate-/l*56.2%
Simplified56.2%
Applied egg-rr7.7%
expm1-def99.7%
expm1-log1p99.7%
associate-*r/99.7%
*-commutative99.7%
Simplified99.7%
if 0.0 < (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 52.3%
hypot-def91.9%
Simplified91.9%
Final simplification92.9%
(FPCore (re im)
:precision binary64
(let* ((t_0 (* 0.5 (sqrt (* 2.0 (- im re))))))
(if (<= re -4.4e+81)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 5e-87)
t_0
(if (<= re 2.3e-83)
(* 0.5 (* im (pow re -0.5)))
(if (<= re 4.3e-30) t_0 (/ (* im 0.5) (sqrt re))))))))
double code(double re, double im) {
double t_0 = 0.5 * sqrt((2.0 * (im - re)));
double tmp;
if (re <= -4.4e+81) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 5e-87) {
tmp = t_0;
} else if (re <= 2.3e-83) {
tmp = 0.5 * (im * pow(re, -0.5));
} else if (re <= 4.3e-30) {
tmp = t_0;
} 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) :: t_0
real(8) :: tmp
t_0 = 0.5d0 * sqrt((2.0d0 * (im - re)))
if (re <= (-4.4d+81)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 5d-87) then
tmp = t_0
else if (re <= 2.3d-83) then
tmp = 0.5d0 * (im * (re ** (-0.5d0)))
else if (re <= 4.3d-30) then
tmp = t_0
else
tmp = (im * 0.5d0) / sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = 0.5 * Math.sqrt((2.0 * (im - re)));
double tmp;
if (re <= -4.4e+81) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 5e-87) {
tmp = t_0;
} else if (re <= 2.3e-83) {
tmp = 0.5 * (im * Math.pow(re, -0.5));
} else if (re <= 4.3e-30) {
tmp = t_0;
} else {
tmp = (im * 0.5) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): t_0 = 0.5 * math.sqrt((2.0 * (im - re))) tmp = 0 if re <= -4.4e+81: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 5e-87: tmp = t_0 elif re <= 2.3e-83: tmp = 0.5 * (im * math.pow(re, -0.5)) elif re <= 4.3e-30: tmp = t_0 else: tmp = (im * 0.5) / math.sqrt(re) return tmp
function code(re, im) t_0 = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re)))) tmp = 0.0 if (re <= -4.4e+81) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 5e-87) tmp = t_0; elseif (re <= 2.3e-83) tmp = Float64(0.5 * Float64(im * (re ^ -0.5))); elseif (re <= 4.3e-30) tmp = t_0; else tmp = Float64(Float64(im * 0.5) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) t_0 = 0.5 * sqrt((2.0 * (im - re))); tmp = 0.0; if (re <= -4.4e+81) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 5e-87) tmp = t_0; elseif (re <= 2.3e-83) tmp = 0.5 * (im * (re ^ -0.5)); elseif (re <= 4.3e-30) tmp = t_0; else tmp = (im * 0.5) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -4.4e+81], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5e-87], t$95$0, If[LessEqual[re, 2.3e-83], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 4.3e-30], t$95$0, N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{if}\;re \leq -4.4 \cdot 10^{+81}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 5 \cdot 10^{-87}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;re \leq 2.3 \cdot 10^{-83}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\
\mathbf{elif}\;re \leq 4.3 \cdot 10^{-30}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -4.39999999999999974e81Initial program 28.2%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 88.4%
*-commutative88.4%
Simplified88.4%
if -4.39999999999999974e81 < re < 5.00000000000000042e-87 or 2.2999999999999999e-83 < re < 4.29999999999999966e-30Initial program 65.8%
Taylor expanded in re around 0 82.2%
if 5.00000000000000042e-87 < re < 2.2999999999999999e-83Initial program 4.4%
hypot-def4.4%
Simplified4.4%
Taylor expanded in re around inf 42.9%
unpow242.9%
associate-/l*61.4%
Simplified61.4%
associate-/r/61.7%
*-commutative61.7%
*-un-lft-identity61.7%
metadata-eval61.7%
metadata-eval61.7%
sqrt-unprod61.1%
associate-*r*61.4%
*-un-lft-identity61.4%
add-sqr-sqrt61.1%
times-frac61.1%
metadata-eval61.1%
sqrt-div61.4%
*-un-lft-identity61.4%
metadata-eval61.4%
metadata-eval61.4%
sqrt-unprod60.9%
associate-*r*61.1%
un-div-inv61.4%
metadata-eval61.4%
sqrt-div61.1%
associate-*l*60.9%
*-commutative60.9%
associate-*r*60.7%
Applied egg-rr99.7%
if 4.29999999999999966e-30 < re Initial program 12.2%
hypot-def29.6%
Simplified29.6%
Taylor expanded in re around inf 46.3%
unpow246.3%
associate-/l*50.5%
Simplified50.5%
Applied egg-rr16.9%
expm1-def82.0%
expm1-log1p82.2%
associate-*r/82.2%
*-commutative82.2%
Simplified82.2%
Final simplification83.7%
(FPCore (re im) :precision binary64 (if (<= re -4.6e-58) (* 0.5 (sqrt (* re -4.0))) (if (<= re 5e-87) (* 0.5 (sqrt (* 2.0 im))) (/ (* im 0.5) (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= -4.6e-58) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 5e-87) {
tmp = 0.5 * sqrt((2.0 * im));
} 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 <= (-4.6d-58)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 5d-87) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
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 <= -4.6e-58) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 5e-87) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = (im * 0.5) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -4.6e-58: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 5e-87: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = (im * 0.5) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -4.6e-58) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 5e-87) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = Float64(Float64(im * 0.5) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4.6e-58) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 5e-87) tmp = 0.5 * sqrt((2.0 * im)); else tmp = (im * 0.5) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -4.6e-58], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5e-87], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.6 \cdot 10^{-58}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 5 \cdot 10^{-87}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -4.5999999999999998e-58Initial program 48.4%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 75.1%
*-commutative75.1%
Simplified75.1%
if -4.5999999999999998e-58 < re < 5.00000000000000042e-87Initial program 63.4%
hypot-def96.5%
Simplified96.5%
Taylor expanded in re around 0 88.6%
*-commutative88.6%
Simplified88.6%
if 5.00000000000000042e-87 < re Initial program 17.4%
hypot-def34.9%
Simplified34.9%
Taylor expanded in re around inf 40.7%
unpow240.7%
associate-/l*45.7%
Simplified45.7%
Applied egg-rr14.3%
expm1-def75.4%
expm1-log1p75.6%
associate-*r/75.6%
*-commutative75.6%
Simplified75.6%
Final simplification81.0%
(FPCore (re im) :precision binary64 (if (<= re -2.25e-54) (* 0.5 (sqrt (* re -4.0))) (* 0.5 (sqrt (* 2.0 im)))))
double code(double re, double im) {
double tmp;
if (re <= -2.25e-54) {
tmp = 0.5 * sqrt((re * -4.0));
} else {
tmp = 0.5 * sqrt((2.0 * im));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-2.25d-54)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else
tmp = 0.5d0 * sqrt((2.0d0 * im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.25e-54) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else {
tmp = 0.5 * Math.sqrt((2.0 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.25e-54: tmp = 0.5 * math.sqrt((re * -4.0)) else: tmp = 0.5 * math.sqrt((2.0 * im)) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.25e-54) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.25e-54) tmp = 0.5 * sqrt((re * -4.0)); else tmp = 0.5 * sqrt((2.0 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.25e-54], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.25 \cdot 10^{-54}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\end{array}
\end{array}
if re < -2.2499999999999999e-54Initial program 48.4%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 75.1%
*-commutative75.1%
Simplified75.1%
if -2.2499999999999999e-54 < re Initial program 45.6%
hypot-def72.6%
Simplified72.6%
Taylor expanded in re around 0 65.7%
*-commutative65.7%
Simplified65.7%
Final simplification68.6%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 im))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * im));
}
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
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * im));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * im))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * im))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * im)); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Initial program 46.5%
hypot-def81.0%
Simplified81.0%
Taylor expanded in re around 0 55.0%
*-commutative55.0%
Simplified55.0%
Final simplification55.0%
herbie shell --seed 2023193
(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)))))