
(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 4 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 (<= re 9.5e-43) (sqrt (* 0.5 (- (hypot im re) re))) (/ (* 0.5 im) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= 9.5e-43) {
tmp = sqrt((0.5 * (hypot(im, re) - re)));
} else {
tmp = (0.5 * im) / sqrt(re);
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 9.5e-43) {
tmp = Math.sqrt((0.5 * (Math.hypot(im, re) - re)));
} else {
tmp = (0.5 * im) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 9.5e-43: tmp = math.sqrt((0.5 * (math.hypot(im, re) - re))) else: tmp = (0.5 * im) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 9.5e-43) tmp = sqrt(Float64(0.5 * Float64(hypot(im, re) - re))); else tmp = Float64(Float64(0.5 * im) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 9.5e-43) tmp = sqrt((0.5 * (hypot(im, re) - re))); else tmp = (0.5 * im) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 9.5e-43], N[Sqrt[N[(0.5 * N[(N[Sqrt[im ^ 2 + re ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 9.5 \cdot 10^{-43}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(im, re\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < 9.50000000000000044e-43Initial program 53.7%
add-sqr-sqrt53.3%
sqrt-unprod53.7%
*-commutative53.7%
*-commutative53.7%
swap-sqr53.7%
add-sqr-sqrt53.7%
*-commutative53.7%
hypot-define94.8%
metadata-eval94.8%
Applied egg-rr94.8%
associate-*l*94.8%
metadata-eval94.8%
*-commutative94.8%
hypot-undefine53.7%
unpow253.7%
unpow253.7%
+-commutative53.7%
unpow253.7%
unpow253.7%
hypot-undefine94.8%
Simplified94.8%
if 9.50000000000000044e-43 < re Initial program 12.6%
Taylor expanded in re around inf 84.8%
*-commutative84.8%
associate-*l*84.9%
*-commutative84.9%
associate-*r*85.0%
*-commutative85.0%
*-commutative85.0%
*-commutative85.0%
associate-*l*85.3%
Simplified85.3%
*-un-lft-identity85.3%
*-commutative85.3%
associate-*r*85.3%
associate-*l*85.4%
*-commutative85.4%
associate-*r*85.3%
*-commutative85.3%
*-commutative85.3%
associate-*r*85.3%
sqrt-div85.4%
metadata-eval85.4%
un-div-inv85.3%
Applied egg-rr86.0%
*-lft-identity86.0%
*-commutative86.0%
Simplified86.0%
Final simplification92.5%
(FPCore (re im)
:precision binary64
(if (<= re -1.45e+54)
(* 0.5 (sqrt (* 2.0 (* re -2.0))))
(if (<= re 3.3e-44)
(* 0.5 (sqrt (* 2.0 (- im re))))
(/ (* 0.5 im) (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= -1.45e+54) {
tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
} else if (re <= 3.3e-44) {
tmp = 0.5 * sqrt((2.0 * (im - re)));
} else {
tmp = (0.5 * im) / 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.45d+54)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
else if (re <= 3.3d-44) then
tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
else
tmp = (0.5d0 * im) / sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.45e+54) {
tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
} else if (re <= 3.3e-44) {
tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
} else {
tmp = (0.5 * im) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.45e+54: tmp = 0.5 * math.sqrt((2.0 * (re * -2.0))) elif re <= 3.3e-44: tmp = 0.5 * math.sqrt((2.0 * (im - re))) else: tmp = (0.5 * im) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.45e+54) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))); elseif (re <= 3.3e-44) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re)))); else tmp = Float64(Float64(0.5 * im) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.45e+54) tmp = 0.5 * sqrt((2.0 * (re * -2.0))); elseif (re <= 3.3e-44) tmp = 0.5 * sqrt((2.0 * (im - re))); else tmp = (0.5 * im) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.45e+54], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.3e-44], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.45 \cdot 10^{+54}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{elif}\;re \leq 3.3 \cdot 10^{-44}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -1.4499999999999999e54Initial program 36.1%
Taylor expanded in re around -inf 90.5%
*-commutative90.5%
Simplified90.5%
if -1.4499999999999999e54 < re < 3.30000000000000006e-44Initial program 59.6%
Taylor expanded in re around 0 81.7%
if 3.30000000000000006e-44 < re Initial program 12.6%
Taylor expanded in re around inf 84.8%
*-commutative84.8%
associate-*l*84.9%
*-commutative84.9%
associate-*r*85.0%
*-commutative85.0%
*-commutative85.0%
*-commutative85.0%
associate-*l*85.3%
Simplified85.3%
*-un-lft-identity85.3%
*-commutative85.3%
associate-*r*85.3%
associate-*l*85.4%
*-commutative85.4%
associate-*r*85.3%
*-commutative85.3%
*-commutative85.3%
associate-*r*85.3%
sqrt-div85.4%
metadata-eval85.4%
un-div-inv85.3%
Applied egg-rr86.0%
*-lft-identity86.0%
*-commutative86.0%
Simplified86.0%
Final simplification84.5%
(FPCore (re im) :precision binary64 (if (<= re -4e-311) (* 0.5 (sqrt (* 2.0 (* re -2.0)))) (/ (* 0.5 im) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -4e-311) {
tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
} else {
tmp = (0.5 * im) / 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 <= (-4d-311)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
else
tmp = (0.5d0 * im) / sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -4e-311) {
tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
} else {
tmp = (0.5 * im) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -4e-311: tmp = 0.5 * math.sqrt((2.0 * (re * -2.0))) else: tmp = (0.5 * im) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -4e-311) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))); else tmp = Float64(Float64(0.5 * im) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4e-311) tmp = 0.5 * sqrt((2.0 * (re * -2.0))); else tmp = (0.5 * im) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -4e-311], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4 \cdot 10^{-311}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -3.99999999999979e-311Initial program 54.6%
Taylor expanded in re around -inf 50.5%
*-commutative50.5%
Simplified50.5%
if -3.99999999999979e-311 < re Initial program 31.4%
Taylor expanded in re around inf 54.7%
*-commutative54.7%
associate-*l*54.7%
*-commutative54.7%
associate-*r*54.8%
*-commutative54.8%
*-commutative54.8%
*-commutative54.8%
associate-*l*55.0%
Simplified55.0%
*-un-lft-identity55.0%
*-commutative55.0%
associate-*r*55.0%
associate-*l*55.1%
*-commutative55.1%
associate-*r*55.0%
*-commutative55.0%
*-commutative55.0%
associate-*r*55.0%
sqrt-div55.0%
metadata-eval55.0%
un-div-inv55.0%
Applied egg-rr55.5%
*-lft-identity55.5%
*-commutative55.5%
Simplified55.5%
Final simplification53.0%
(FPCore (re im) :precision binary64 (/ (* 0.5 im) (sqrt re)))
double code(double re, double im) {
return (0.5 * im) / sqrt(re);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = (0.5d0 * im) / sqrt(re)
end function
public static double code(double re, double im) {
return (0.5 * im) / Math.sqrt(re);
}
def code(re, im): return (0.5 * im) / math.sqrt(re)
function code(re, im) return Float64(Float64(0.5 * im) / sqrt(re)) end
function tmp = code(re, im) tmp = (0.5 * im) / sqrt(re); end
code[re_, im_] := N[(N[(0.5 * im), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot im}{\sqrt{re}}
\end{array}
Initial program 42.9%
Taylor expanded in re around inf 27.6%
*-commutative27.6%
associate-*l*27.6%
*-commutative27.6%
associate-*r*27.6%
*-commutative27.6%
*-commutative27.6%
*-commutative27.6%
associate-*l*27.7%
Simplified27.7%
*-un-lft-identity27.7%
*-commutative27.7%
associate-*r*27.7%
associate-*l*27.7%
*-commutative27.7%
associate-*r*27.7%
*-commutative27.7%
*-commutative27.7%
associate-*r*27.7%
sqrt-div27.7%
metadata-eval27.7%
un-div-inv27.7%
Applied egg-rr28.0%
*-lft-identity28.0%
*-commutative28.0%
Simplified28.0%
Final simplification28.0%
herbie shell --seed 2024096
(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)))))