
(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 (<= re 3.05e-55) (sqrt (* 0.5 (- (hypot re im) re))) (* im (/ 0.5 (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= 3.05e-55) {
tmp = sqrt((0.5 * (hypot(re, im) - re)));
} else {
tmp = im * (0.5 / sqrt(re));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 3.05e-55) {
tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
} else {
tmp = im * (0.5 / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 3.05e-55: tmp = math.sqrt((0.5 * (math.hypot(re, im) - re))) else: tmp = im * (0.5 / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= 3.05e-55) tmp = sqrt(Float64(0.5 * Float64(hypot(re, 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 <= 3.05e-55) tmp = sqrt((0.5 * (hypot(re, im) - re))); else tmp = im * (0.5 / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 3.05e-55], N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - 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 3.05 \cdot 10^{-55}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < 3.0500000000000001e-55Initial program 51.6%
hypot-udef94.8%
add-sqr-sqrt94.0%
sqrt-unprod94.8%
*-commutative94.8%
*-commutative94.8%
swap-sqr94.8%
add-sqr-sqrt94.8%
metadata-eval94.8%
Applied egg-rr94.8%
*-commutative94.8%
associate-*r*95.3%
metadata-eval95.3%
Simplified95.3%
if 3.0500000000000001e-55 < re Initial program 10.2%
Taylor expanded in im around 0 79.6%
*-commutative79.6%
associate-*l*79.6%
*-commutative79.6%
Simplified79.6%
expm1-log1p-u79.0%
expm1-udef27.3%
sqrt-unprod27.3%
metadata-eval27.3%
metadata-eval27.3%
*-rgt-identity27.3%
*-commutative27.3%
sqrt-div27.3%
metadata-eval27.3%
un-div-inv27.3%
Applied egg-rr27.3%
expm1-def79.8%
expm1-log1p80.7%
associate-*r/80.6%
Simplified80.6%
associate-*r/80.7%
*-commutative80.7%
Applied egg-rr80.7%
Final simplification90.4%
(FPCore (re im) :precision binary64 (if (<= re -1.5e+54) (* 0.5 (sqrt (* re -4.0))) (if (<= re 2.6e-50) (sqrt (* 0.5 (- im re))) (* im (/ 0.5 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -1.5e+54) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 2.6e-50) {
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.5d+54)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 2.6d-50) 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.5e+54) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 2.6e-50) {
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.5e+54: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 2.6e-50: 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.5e+54) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 2.6e-50) 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.5e+54) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 2.6e-50) 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.5e+54], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.6e-50], 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.5 \cdot 10^{+54}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 2.6 \cdot 10^{-50}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -1.4999999999999999e54Initial program 32.0%
sub-neg32.0%
sqr-neg32.0%
sub-neg32.0%
sqr-neg32.0%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 82.2%
*-commutative82.2%
Simplified82.2%
if -1.4999999999999999e54 < re < 2.6000000000000001e-50Initial program 58.7%
hypot-udef92.9%
add-sqr-sqrt92.2%
sqrt-unprod92.9%
*-commutative92.9%
*-commutative92.9%
swap-sqr92.9%
add-sqr-sqrt92.9%
metadata-eval92.9%
Applied egg-rr92.9%
*-commutative92.9%
associate-*r*93.6%
metadata-eval93.6%
Simplified93.6%
Taylor expanded in re around 0 79.6%
neg-mul-179.6%
unsub-neg79.6%
Simplified79.6%
if 2.6000000000000001e-50 < re Initial program 10.2%
Taylor expanded in im around 0 79.6%
*-commutative79.6%
associate-*l*79.6%
*-commutative79.6%
Simplified79.6%
expm1-log1p-u79.0%
expm1-udef27.3%
sqrt-unprod27.3%
metadata-eval27.3%
metadata-eval27.3%
*-rgt-identity27.3%
*-commutative27.3%
sqrt-div27.3%
metadata-eval27.3%
un-div-inv27.3%
Applied egg-rr27.3%
expm1-def79.8%
expm1-log1p80.7%
associate-*r/80.6%
Simplified80.6%
associate-*r/80.7%
*-commutative80.7%
Applied egg-rr80.7%
Final simplification80.4%
(FPCore (re im) :precision binary64 (if (<= re 1.9e-162) (sqrt (* 0.5 (- im re))) (sqrt (* 0.5 im))))
double code(double re, double im) {
double tmp;
if (re <= 1.9e-162) {
tmp = sqrt((0.5 * (im - re)));
} else {
tmp = sqrt((0.5 * im));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= 1.9d-162) then
tmp = sqrt((0.5d0 * (im - re)))
else
tmp = sqrt((0.5d0 * im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 1.9e-162) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = Math.sqrt((0.5 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 1.9e-162: tmp = math.sqrt((0.5 * (im - re))) else: tmp = math.sqrt((0.5 * im)) return tmp
function code(re, im) tmp = 0.0 if (re <= 1.9e-162) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = sqrt(Float64(0.5 * im)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 1.9e-162) tmp = sqrt((0.5 * (im - re))); else tmp = sqrt((0.5 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 1.9e-162], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Sqrt[N[(0.5 * im), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 1.9 \cdot 10^{-162}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot im}\\
\end{array}
\end{array}
if re < 1.90000000000000002e-162Initial program 52.5%
hypot-udef97.3%
add-sqr-sqrt96.5%
sqrt-unprod97.3%
*-commutative97.3%
*-commutative97.3%
swap-sqr97.3%
add-sqr-sqrt97.3%
metadata-eval97.3%
Applied egg-rr97.3%
*-commutative97.3%
associate-*r*97.9%
metadata-eval97.9%
Simplified97.9%
Taylor expanded in re around 0 66.8%
neg-mul-166.8%
unsub-neg66.8%
Simplified66.8%
if 1.90000000000000002e-162 < re Initial program 16.4%
hypot-udef40.5%
add-sqr-sqrt40.3%
sqrt-unprod40.5%
*-commutative40.5%
*-commutative40.5%
swap-sqr40.5%
add-sqr-sqrt40.5%
metadata-eval40.5%
Applied egg-rr40.5%
*-commutative40.5%
associate-*r*40.5%
metadata-eval40.5%
Simplified40.5%
Taylor expanded in re around 0 33.5%
Final simplification53.0%
(FPCore (re im) :precision binary64 (if (<= im 3e-103) (* 0.5 (sqrt (* re -4.0))) (sqrt (* 0.5 (- im re)))))
double code(double re, double im) {
double tmp;
if (im <= 3e-103) {
tmp = 0.5 * sqrt((re * -4.0));
} else {
tmp = sqrt((0.5 * (im - re)));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 3d-103) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else
tmp = sqrt((0.5d0 * (im - re)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 3e-103) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else {
tmp = Math.sqrt((0.5 * (im - re)));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 3e-103: tmp = 0.5 * math.sqrt((re * -4.0)) else: tmp = math.sqrt((0.5 * (im - re))) return tmp
function code(re, im) tmp = 0.0 if (im <= 3e-103) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); else tmp = sqrt(Float64(0.5 * Float64(im - re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 3e-103) tmp = 0.5 * sqrt((re * -4.0)); else tmp = sqrt((0.5 * (im - re))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 3e-103], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 3 \cdot 10^{-103}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\end{array}
\end{array}
if im < 3e-103Initial program 31.1%
sub-neg31.1%
sqr-neg31.1%
sub-neg31.1%
sqr-neg31.1%
hypot-def64.2%
Simplified64.2%
Taylor expanded in re around -inf 44.1%
*-commutative44.1%
Simplified44.1%
if 3e-103 < im Initial program 40.5%
hypot-udef78.1%
add-sqr-sqrt77.5%
sqrt-unprod78.1%
*-commutative78.1%
*-commutative78.1%
swap-sqr78.1%
add-sqr-sqrt78.1%
metadata-eval78.1%
Applied egg-rr78.1%
*-commutative78.1%
associate-*r*78.6%
metadata-eval78.6%
Simplified78.6%
Taylor expanded in re around 0 67.7%
neg-mul-167.7%
unsub-neg67.7%
Simplified67.7%
Final simplification60.3%
(FPCore (re im) :precision binary64 (sqrt (* 0.5 im)))
double code(double re, double im) {
return sqrt((0.5 * im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt((0.5d0 * im))
end function
public static double code(double re, double im) {
return Math.sqrt((0.5 * im));
}
def code(re, im): return math.sqrt((0.5 * im))
function code(re, im) return sqrt(Float64(0.5 * im)) end
function tmp = code(re, im) tmp = sqrt((0.5 * im)); end
code[re_, im_] := N[Sqrt[N[(0.5 * im), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot im}
\end{array}
Initial program 37.5%
hypot-udef73.7%
add-sqr-sqrt73.2%
sqrt-unprod73.7%
*-commutative73.7%
*-commutative73.7%
swap-sqr73.7%
add-sqr-sqrt73.7%
metadata-eval73.7%
Applied egg-rr73.7%
*-commutative73.7%
associate-*r*74.1%
metadata-eval74.1%
Simplified74.1%
Taylor expanded in re around 0 49.6%
Final simplification49.6%
herbie shell --seed 2024024
(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)))))