
(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}
(FPCore (re im) :precision binary64 (if (<= (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))) 0.0) (* im (* (pow re -0.5) 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 = im * (pow(re, -0.5) * 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 = im * (Math.pow(re, -0.5) * 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 = im * (math.pow(re, -0.5) * 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(im * Float64((re ^ -0.5) * 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 = im * ((re ^ -0.5) * 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[(im * N[(N[Power[re, -0.5], $MachinePrecision] * 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:\\
\;\;\;\;im \cdot \left({re}^{-0.5} \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 8.5%
Taylor expanded in re around inf 45.0%
sqrt-div47.9%
sqrt-pow199.4%
metadata-eval99.4%
pow199.4%
associate-*r/99.4%
Applied egg-rr99.4%
*-commutative99.4%
associate-/l*99.4%
Simplified99.4%
clear-num99.4%
associate-/r/99.4%
pow1/299.4%
pow-flip99.6%
metadata-eval99.6%
Applied egg-rr99.6%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 42.0%
pow142.0%
Applied egg-rr91.5%
unpow191.5%
*-commutative91.5%
associate-*r*91.9%
metadata-eval91.9%
Simplified91.9%
(FPCore (re im)
:precision binary64
(if (<= re -2e-38)
(sqrt (- re))
(if (<= re 1.72e-71)
(sqrt (* 0.5 (- im re)))
(* im (* (pow re -0.5) 0.5)))))
double code(double re, double im) {
double tmp;
if (re <= -2e-38) {
tmp = sqrt(-re);
} else if (re <= 1.72e-71) {
tmp = sqrt((0.5 * (im - re)));
} else {
tmp = im * (pow(re, -0.5) * 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 <= (-2d-38)) then
tmp = sqrt(-re)
else if (re <= 1.72d-71) then
tmp = sqrt((0.5d0 * (im - re)))
else
tmp = im * ((re ** (-0.5d0)) * 0.5d0)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2e-38) {
tmp = Math.sqrt(-re);
} else if (re <= 1.72e-71) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = im * (Math.pow(re, -0.5) * 0.5);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2e-38: tmp = math.sqrt(-re) elif re <= 1.72e-71: tmp = math.sqrt((0.5 * (im - re))) else: tmp = im * (math.pow(re, -0.5) * 0.5) return tmp
function code(re, im) tmp = 0.0 if (re <= -2e-38) tmp = sqrt(Float64(-re)); elseif (re <= 1.72e-71) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64(im * Float64((re ^ -0.5) * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2e-38) tmp = sqrt(-re); elseif (re <= 1.72e-71) tmp = sqrt((0.5 * (im - re))); else tmp = im * ((re ^ -0.5) * 0.5); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2e-38], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 1.72e-71], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(im * N[(N[Power[re, -0.5], $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2 \cdot 10^{-38}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 1.72 \cdot 10^{-71}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left({re}^{-0.5} \cdot 0.5\right)\\
\end{array}
\end{array}
if re < -1.9999999999999999e-38Initial program 42.8%
pow142.8%
Applied egg-rr98.8%
unpow198.8%
*-commutative98.8%
associate-*r*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 79.5%
neg-mul-179.5%
Simplified79.5%
if -1.9999999999999999e-38 < re < 1.72e-71Initial program 53.2%
pow153.2%
Applied egg-rr94.7%
unpow194.7%
*-commutative94.7%
associate-*r*94.7%
metadata-eval94.7%
Simplified94.7%
Taylor expanded in re around 0 86.2%
neg-mul-186.2%
sub-neg86.2%
Simplified86.2%
if 1.72e-71 < re Initial program 8.1%
Taylor expanded in re around inf 51.3%
sqrt-div56.8%
sqrt-pow180.4%
metadata-eval80.4%
pow180.4%
associate-*r/80.4%
Applied egg-rr80.4%
*-commutative80.4%
associate-/l*80.3%
Simplified80.3%
clear-num80.3%
associate-/r/80.3%
pow1/280.3%
pow-flip80.4%
metadata-eval80.4%
Applied egg-rr80.4%
(FPCore (re im) :precision binary64 (if (<= re -2.3e-40) (sqrt (- re)) (if (<= re 2.4e-76) (sqrt (* 0.5 (- im re))) (* im (sqrt (/ 0.25 re))))))
double code(double re, double im) {
double tmp;
if (re <= -2.3e-40) {
tmp = sqrt(-re);
} else if (re <= 2.4e-76) {
tmp = sqrt((0.5 * (im - re)));
} else {
tmp = im * sqrt((0.25 / re));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-2.3d-40)) then
tmp = sqrt(-re)
else if (re <= 2.4d-76) then
tmp = sqrt((0.5d0 * (im - re)))
else
tmp = im * sqrt((0.25d0 / re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.3e-40) {
tmp = Math.sqrt(-re);
} else if (re <= 2.4e-76) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = im * Math.sqrt((0.25 / re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.3e-40: tmp = math.sqrt(-re) elif re <= 2.4e-76: tmp = math.sqrt((0.5 * (im - re))) else: tmp = im * math.sqrt((0.25 / re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.3e-40) tmp = sqrt(Float64(-re)); elseif (re <= 2.4e-76) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64(im * sqrt(Float64(0.25 / re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.3e-40) tmp = sqrt(-re); elseif (re <= 2.4e-76) tmp = sqrt((0.5 * (im - re))); else tmp = im * sqrt((0.25 / re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.3e-40], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 2.4e-76], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(im * N[Sqrt[N[(0.25 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.3 \cdot 10^{-40}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 2.4 \cdot 10^{-76}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\
\end{array}
\end{array}
if re < -2.3e-40Initial program 42.8%
pow142.8%
Applied egg-rr98.8%
unpow198.8%
*-commutative98.8%
associate-*r*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 79.5%
neg-mul-179.5%
Simplified79.5%
if -2.3e-40 < re < 2.40000000000000013e-76Initial program 53.2%
pow153.2%
Applied egg-rr94.7%
unpow194.7%
*-commutative94.7%
associate-*r*94.7%
metadata-eval94.7%
Simplified94.7%
Taylor expanded in re around 0 86.2%
neg-mul-186.2%
sub-neg86.2%
Simplified86.2%
if 2.40000000000000013e-76 < re Initial program 8.1%
Taylor expanded in re around inf 51.3%
sqrt-div56.8%
sqrt-pow180.4%
metadata-eval80.4%
pow180.4%
associate-*r/80.4%
Applied egg-rr80.4%
*-commutative80.4%
associate-/l*80.3%
Simplified80.3%
*-commutative80.3%
associate-/r/80.2%
div-inv80.2%
associate-/r*80.3%
add-sqr-sqrt80.0%
sqrt-unprod80.3%
frac-times80.3%
metadata-eval80.3%
add-sqr-sqrt80.3%
Applied egg-rr80.3%
associate-/r/80.4%
/-rgt-identity80.4%
*-commutative80.4%
Simplified80.4%
(FPCore (re im) :precision binary64 (if (<= re -9.8e-54) (sqrt (- re)) (sqrt (* im 0.5))))
double code(double re, double im) {
double tmp;
if (re <= -9.8e-54) {
tmp = sqrt(-re);
} 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 <= (-9.8d-54)) then
tmp = sqrt(-re)
else
tmp = sqrt((im * 0.5d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -9.8e-54) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -9.8e-54: tmp = math.sqrt(-re) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -9.8e-54) tmp = sqrt(Float64(-re)); else tmp = sqrt(Float64(im * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -9.8e-54) tmp = sqrt(-re); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -9.8e-54], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -9.8 \cdot 10^{-54}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < -9.80000000000000042e-54Initial program 44.2%
pow144.2%
Applied egg-rr98.9%
unpow198.9%
*-commutative98.9%
associate-*r*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 78.9%
neg-mul-178.9%
Simplified78.9%
if -9.80000000000000042e-54 < re Initial program 35.3%
pow135.3%
Applied egg-rr73.9%
unpow173.9%
*-commutative73.9%
associate-*r*73.9%
metadata-eval73.9%
Simplified73.9%
Taylor expanded in re around 0 62.3%
*-commutative62.3%
Simplified62.3%
(FPCore (re im) :precision binary64 (if (<= re -2e-310) (sqrt (- re)) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= -2e-310) {
tmp = sqrt(-re);
} else {
tmp = 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 <= (-2d-310)) then
tmp = sqrt(-re)
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2e-310) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2e-310: tmp = math.sqrt(-re) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -2e-310) tmp = sqrt(Float64(-re)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2e-310) tmp = sqrt(-re); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2e-310], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2 \cdot 10^{-310}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -1.999999999999994e-310Initial program 48.8%
pow148.8%
Applied egg-rr99.3%
unpow199.3%
*-commutative99.3%
associate-*r*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 59.0%
neg-mul-159.0%
Simplified59.0%
if -1.999999999999994e-310 < re Initial program 26.9%
pow126.9%
Applied egg-rr63.9%
unpow163.9%
*-commutative63.9%
associate-*r*63.9%
metadata-eval63.9%
Simplified63.9%
Taylor expanded in re around -inf 0.0%
neg-mul-10.0%
Simplified0.0%
neg-sub00.0%
sub-neg0.0%
add-sqr-sqrt0.0%
sqrt-unprod4.2%
sqr-neg4.2%
sqrt-unprod5.8%
add-sqr-sqrt5.8%
Applied egg-rr5.8%
+-lft-identity5.8%
Simplified5.8%
(FPCore (re im) :precision binary64 (sqrt re))
double code(double re, double im) {
return sqrt(re);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt(re)
end function
public static double code(double re, double im) {
return Math.sqrt(re);
}
def code(re, im): return math.sqrt(re)
function code(re, im) return sqrt(re) end
function tmp = code(re, im) tmp = sqrt(re); end
code[re_, im_] := N[Sqrt[re], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{re}
\end{array}
Initial program 38.2%
pow138.2%
Applied egg-rr82.1%
unpow182.1%
*-commutative82.1%
associate-*r*82.5%
metadata-eval82.5%
Simplified82.5%
Taylor expanded in re around -inf 30.4%
neg-mul-130.4%
Simplified30.4%
neg-sub030.4%
sub-neg30.4%
add-sqr-sqrt30.4%
sqrt-unprod17.5%
sqr-neg17.5%
sqrt-unprod2.8%
add-sqr-sqrt2.8%
Applied egg-rr2.8%
+-lft-identity2.8%
Simplified2.8%
herbie shell --seed 2024191
(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)))))