
(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 (+ (* re re) (* im im))) re) 0.0) (/ (* im 0.5) (sqrt re)) (sqrt (* 0.5 (- (hypot re im) re)))))
double code(double re, double im) {
double tmp;
if ((sqrt(((re * re) + (im * im))) - re) <= 0.0) {
tmp = (im * 0.5) / sqrt(re);
} else {
tmp = sqrt((0.5 * (hypot(re, im) - re)));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if ((Math.sqrt(((re * re) + (im * im))) - re) <= 0.0) {
tmp = (im * 0.5) / Math.sqrt(re);
} else {
tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
}
return tmp;
}
def code(re, im): tmp = 0 if (math.sqrt(((re * re) + (im * im))) - re) <= 0.0: tmp = (im * 0.5) / math.sqrt(re) else: tmp = math.sqrt((0.5 * (math.hypot(re, im) - re))) return tmp
function code(re, im) tmp = 0.0 if (Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re) <= 0.0) tmp = Float64(Float64(im * 0.5) / sqrt(re)); 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(((re * re) + (im * im))) - re) <= 0.0) tmp = (im * 0.5) / sqrt(re); else tmp = sqrt((0.5 * (hypot(re, im) - re))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision], 0.0], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $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{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\end{array}
\end{array}
if (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0Initial program 5.9%
Taylor expanded in re around inf 47.8%
*-commutative47.8%
sqrt-div50.1%
sqrt-pow188.3%
metadata-eval88.3%
pow188.3%
associate-*l/88.3%
Applied egg-rr88.3%
if 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 44.1%
sub-neg44.1%
sqr-neg44.1%
sub-neg44.1%
sqr-neg44.1%
hypot-define88.7%
Simplified88.7%
*-commutative88.7%
hypot-define44.1%
*-commutative44.1%
add-sqr-sqrt43.8%
sqrt-unprod44.1%
*-commutative44.1%
*-commutative44.1%
swap-sqr44.1%
Applied egg-rr88.7%
associate-*l*88.7%
metadata-eval88.7%
Simplified88.7%
Final simplification88.6%
(FPCore (re im) :precision binary64 (if (<= re -1.3e-32) (sqrt (- re)) (if (<= re 5.8e+49) (sqrt (* im 0.5)) (/ (* im 0.5) (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= -1.3e-32) {
tmp = sqrt(-re);
} else if (re <= 5.8e+49) {
tmp = sqrt((im * 0.5));
} 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.3d-32)) then
tmp = sqrt(-re)
else if (re <= 5.8d+49) then
tmp = sqrt((im * 0.5d0))
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.3e-32) {
tmp = Math.sqrt(-re);
} else if (re <= 5.8e+49) {
tmp = Math.sqrt((im * 0.5));
} else {
tmp = (im * 0.5) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.3e-32: tmp = math.sqrt(-re) elif re <= 5.8e+49: tmp = math.sqrt((im * 0.5)) else: tmp = (im * 0.5) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.3e-32) tmp = sqrt(Float64(-re)); elseif (re <= 5.8e+49) tmp = sqrt(Float64(im * 0.5)); 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.3e-32) tmp = sqrt(-re); elseif (re <= 5.8e+49) tmp = sqrt((im * 0.5)); else tmp = (im * 0.5) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.3e-32], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 5.8e+49], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.3 \cdot 10^{-32}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 5.8 \cdot 10^{+49}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -1.2999999999999999e-32Initial program 37.1%
sub-neg37.1%
sqr-neg37.1%
sub-neg37.1%
sqr-neg37.1%
hypot-define100.0%
Simplified100.0%
*-commutative100.0%
hypot-define37.1%
*-commutative37.1%
add-sqr-sqrt36.8%
sqrt-unprod37.1%
*-commutative37.1%
*-commutative37.1%
swap-sqr37.1%
Applied egg-rr100.0%
associate-*l*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 72.9%
neg-mul-172.9%
Simplified72.9%
if -1.2999999999999999e-32 < re < 5.8e49Initial program 54.2%
sub-neg54.2%
sqr-neg54.2%
sub-neg54.2%
sqr-neg54.2%
hypot-define84.3%
Simplified84.3%
*-commutative84.3%
hypot-define54.2%
*-commutative54.2%
add-sqr-sqrt53.8%
sqrt-unprod54.2%
*-commutative54.2%
*-commutative54.2%
swap-sqr54.2%
Applied egg-rr84.3%
associate-*l*84.3%
metadata-eval84.3%
Simplified84.3%
Taylor expanded in re around 0 77.2%
*-commutative77.2%
Simplified77.2%
if 5.8e49 < re Initial program 4.8%
Taylor expanded in re around inf 54.9%
*-commutative54.9%
sqrt-div66.1%
sqrt-pow183.8%
metadata-eval83.8%
pow183.8%
associate-*l/83.8%
Applied egg-rr83.8%
(FPCore (re im) :precision binary64 (if (<= re -2e-32) (sqrt (- re)) (if (<= re 1.8e+49) (sqrt (* im 0.5)) (* im (/ 0.5 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -2e-32) {
tmp = sqrt(-re);
} else if (re <= 1.8e+49) {
tmp = sqrt((im * 0.5));
} 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 <= (-2d-32)) then
tmp = sqrt(-re)
else if (re <= 1.8d+49) then
tmp = sqrt((im * 0.5d0))
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 <= -2e-32) {
tmp = Math.sqrt(-re);
} else if (re <= 1.8e+49) {
tmp = Math.sqrt((im * 0.5));
} else {
tmp = im * (0.5 / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2e-32: tmp = math.sqrt(-re) elif re <= 1.8e+49: tmp = math.sqrt((im * 0.5)) else: tmp = im * (0.5 / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -2e-32) tmp = sqrt(Float64(-re)); elseif (re <= 1.8e+49) tmp = sqrt(Float64(im * 0.5)); else tmp = Float64(im * Float64(0.5 / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2e-32) tmp = sqrt(-re); elseif (re <= 1.8e+49) tmp = sqrt((im * 0.5)); else tmp = im * (0.5 / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2e-32], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 1.8e+49], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision], N[(im * N[(0.5 / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2 \cdot 10^{-32}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 1.8 \cdot 10^{+49}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -2.00000000000000011e-32Initial program 37.1%
sub-neg37.1%
sqr-neg37.1%
sub-neg37.1%
sqr-neg37.1%
hypot-define100.0%
Simplified100.0%
*-commutative100.0%
hypot-define37.1%
*-commutative37.1%
add-sqr-sqrt36.8%
sqrt-unprod37.1%
*-commutative37.1%
*-commutative37.1%
swap-sqr37.1%
Applied egg-rr100.0%
associate-*l*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 72.9%
neg-mul-172.9%
Simplified72.9%
if -2.00000000000000011e-32 < re < 1.79999999999999998e49Initial program 54.2%
sub-neg54.2%
sqr-neg54.2%
sub-neg54.2%
sqr-neg54.2%
hypot-define84.3%
Simplified84.3%
*-commutative84.3%
hypot-define54.2%
*-commutative54.2%
add-sqr-sqrt53.8%
sqrt-unprod54.2%
*-commutative54.2%
*-commutative54.2%
swap-sqr54.2%
Applied egg-rr84.3%
associate-*l*84.3%
metadata-eval84.3%
Simplified84.3%
Taylor expanded in re around 0 77.2%
*-commutative77.2%
Simplified77.2%
if 1.79999999999999998e49 < re Initial program 4.8%
Taylor expanded in re around inf 54.9%
sqrt-div66.1%
sqrt-pow183.8%
metadata-eval83.8%
pow183.8%
associate-*r/83.8%
Applied egg-rr83.8%
*-commutative83.8%
associate-/l*83.7%
Simplified83.7%
(FPCore (re im) :precision binary64 (if (<= re -6.8e-33) (sqrt (- re)) (sqrt (* im 0.5))))
double code(double re, double im) {
double tmp;
if (re <= -6.8e-33) {
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 <= (-6.8d-33)) 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 <= -6.8e-33) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -6.8e-33: tmp = math.sqrt(-re) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -6.8e-33) 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 <= -6.8e-33) tmp = sqrt(-re); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -6.8e-33], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -6.8 \cdot 10^{-33}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < -6.8000000000000001e-33Initial program 37.1%
sub-neg37.1%
sqr-neg37.1%
sub-neg37.1%
sqr-neg37.1%
hypot-define100.0%
Simplified100.0%
*-commutative100.0%
hypot-define37.1%
*-commutative37.1%
add-sqr-sqrt36.8%
sqrt-unprod37.1%
*-commutative37.1%
*-commutative37.1%
swap-sqr37.1%
Applied egg-rr100.0%
associate-*l*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 72.9%
neg-mul-172.9%
Simplified72.9%
if -6.8000000000000001e-33 < re Initial program 38.5%
sub-neg38.5%
sqr-neg38.5%
sub-neg38.5%
sqr-neg38.5%
hypot-define69.4%
Simplified69.4%
*-commutative69.4%
hypot-define38.5%
*-commutative38.5%
add-sqr-sqrt38.2%
sqrt-unprod38.5%
*-commutative38.5%
*-commutative38.5%
swap-sqr38.5%
Applied egg-rr69.4%
associate-*l*69.4%
metadata-eval69.4%
Simplified69.4%
Taylor expanded in re around 0 59.8%
*-commutative59.8%
Simplified59.8%
(FPCore (re im) :precision binary64 (if (<= re -4e-310) (sqrt (- re)) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= -4e-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 <= (-4d-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 <= -4e-310) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -4e-310: tmp = math.sqrt(-re) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -4e-310) tmp = sqrt(Float64(-re)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4e-310) tmp = sqrt(-re); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -4e-310], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4 \cdot 10^{-310}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -3.999999999999988e-310Initial program 50.6%
sub-neg50.6%
sqr-neg50.6%
sub-neg50.6%
sqr-neg50.6%
hypot-define100.0%
Simplified100.0%
*-commutative100.0%
hypot-define50.6%
*-commutative50.6%
add-sqr-sqrt50.3%
sqrt-unprod50.6%
*-commutative50.6%
*-commutative50.6%
swap-sqr50.6%
Applied egg-rr100.0%
associate-*l*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 51.6%
neg-mul-151.6%
Simplified51.6%
if -3.999999999999988e-310 < re Initial program 26.6%
sub-neg26.6%
sqr-neg26.6%
sub-neg26.6%
sqr-neg26.6%
hypot-define57.3%
Simplified57.3%
*-commutative57.3%
hypot-define26.6%
*-commutative26.6%
add-sqr-sqrt26.4%
sqrt-unprod26.6%
*-commutative26.6%
*-commutative26.6%
swap-sqr26.6%
Applied egg-rr57.3%
associate-*l*57.3%
metadata-eval57.3%
Simplified57.3%
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.5%
sqr-neg4.5%
sqrt-unprod5.7%
add-sqr-sqrt5.7%
Applied egg-rr5.7%
+-lft-identity5.7%
Simplified5.7%
(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.1%
sub-neg38.1%
sqr-neg38.1%
sub-neg38.1%
sqr-neg38.1%
hypot-define77.8%
Simplified77.8%
*-commutative77.8%
hypot-define38.1%
*-commutative38.1%
add-sqr-sqrt37.9%
sqrt-unprod38.1%
*-commutative38.1%
*-commutative38.1%
swap-sqr38.1%
Applied egg-rr77.8%
associate-*l*77.8%
metadata-eval77.8%
Simplified77.8%
Taylor expanded in re around -inf 24.8%
neg-mul-124.8%
Simplified24.8%
neg-sub024.8%
sub-neg24.8%
add-sqr-sqrt24.8%
sqrt-unprod15.7%
sqr-neg15.7%
sqrt-unprod2.9%
add-sqr-sqrt2.9%
Applied egg-rr2.9%
+-lft-identity2.9%
Simplified2.9%
herbie shell --seed 2024116
(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)))))