
(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 (<= re 4100000.0) (sqrt (* (- (hypot re im) re) 0.5)) (* im (* 0.5 (pow re -0.5)))))
double code(double re, double im) {
double tmp;
if (re <= 4100000.0) {
tmp = sqrt(((hypot(re, im) - re) * 0.5));
} else {
tmp = im * (0.5 * pow(re, -0.5));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 4100000.0) {
tmp = Math.sqrt(((Math.hypot(re, im) - re) * 0.5));
} else {
tmp = im * (0.5 * Math.pow(re, -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 4100000.0: tmp = math.sqrt(((math.hypot(re, im) - re) * 0.5)) else: tmp = im * (0.5 * math.pow(re, -0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= 4100000.0) tmp = sqrt(Float64(Float64(hypot(re, im) - re) * 0.5)); else tmp = Float64(im * Float64(0.5 * (re ^ -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 4100000.0) tmp = sqrt(((hypot(re, im) - re) * 0.5)); else tmp = im * (0.5 * (re ^ -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 4100000.0], N[Sqrt[N[(N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision] * 0.5), $MachinePrecision]], $MachinePrecision], N[(im * N[(0.5 * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 4100000:\\
\;\;\;\;\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(0.5 \cdot {re}^{-0.5}\right)\\
\end{array}
\end{array}
if re < 4.1e6Initial program 50.5%
sub-neg50.5%
sqr-neg50.5%
sub-neg50.5%
sqr-neg50.5%
hypot-define94.1%
Simplified94.1%
add-sqr-sqrt93.3%
sqrt-unprod94.1%
*-commutative94.1%
*-commutative94.1%
swap-sqr94.1%
add-sqr-sqrt94.1%
*-commutative94.1%
metadata-eval94.1%
Applied egg-rr94.1%
associate-*l*94.1%
metadata-eval94.1%
Simplified94.1%
if 4.1e6 < re Initial program 12.9%
sub-neg12.9%
sqr-neg12.9%
sub-neg12.9%
sqr-neg12.9%
hypot-define33.9%
Simplified33.9%
add-sqr-sqrt33.8%
sqrt-unprod33.9%
*-commutative33.9%
*-commutative33.9%
swap-sqr33.9%
add-sqr-sqrt33.9%
*-commutative33.9%
metadata-eval33.9%
Applied egg-rr33.9%
associate-*l*33.9%
metadata-eval33.9%
Simplified33.9%
Taylor expanded in re around inf 81.6%
associate-*l*81.7%
unpow281.7%
rem-square-sqrt82.6%
rem-exp-log77.7%
exp-neg77.7%
unpow1/277.7%
exp-prod77.7%
distribute-lft-neg-out77.7%
distribute-rgt-neg-in77.7%
metadata-eval77.7%
exp-to-pow82.7%
Simplified82.7%
Final simplification90.8%
(FPCore (re im)
:precision binary64
(if (<= re -3e-39)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 1350000.0)
(sqrt (* 0.5 (- im re)))
(* im (* 0.5 (pow re -0.5))))))
double code(double re, double im) {
double tmp;
if (re <= -3e-39) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 1350000.0) {
tmp = sqrt((0.5 * (im - re)));
} else {
tmp = im * (0.5 * pow(re, -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 <= (-3d-39)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 1350000.0d0) then
tmp = sqrt((0.5d0 * (im - re)))
else
tmp = im * (0.5d0 * (re ** (-0.5d0)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3e-39) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 1350000.0) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = im * (0.5 * Math.pow(re, -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3e-39: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 1350000.0: tmp = math.sqrt((0.5 * (im - re))) else: tmp = im * (0.5 * math.pow(re, -0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3e-39) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 1350000.0) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64(im * Float64(0.5 * (re ^ -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3e-39) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 1350000.0) tmp = sqrt((0.5 * (im - re))); else tmp = im * (0.5 * (re ^ -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3e-39], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1350000.0], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(im * N[(0.5 * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3 \cdot 10^{-39}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 1350000:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \left(0.5 \cdot {re}^{-0.5}\right)\\
\end{array}
\end{array}
if re < -3.00000000000000028e-39Initial program 47.5%
Taylor expanded in re around -inf 80.0%
*-commutative80.0%
Simplified80.0%
if -3.00000000000000028e-39 < re < 1.35e6Initial program 52.5%
sub-neg52.5%
sqr-neg52.5%
sub-neg52.5%
sqr-neg52.5%
hypot-define90.1%
Simplified90.1%
add-sqr-sqrt89.3%
sqrt-unprod90.1%
*-commutative90.1%
*-commutative90.1%
swap-sqr90.1%
add-sqr-sqrt90.1%
*-commutative90.1%
metadata-eval90.1%
Applied egg-rr90.1%
associate-*l*90.1%
metadata-eval90.1%
Simplified90.1%
Taylor expanded in re around 0 80.0%
neg-mul-180.0%
unsub-neg80.0%
Simplified80.0%
if 1.35e6 < re Initial program 12.9%
sub-neg12.9%
sqr-neg12.9%
sub-neg12.9%
sqr-neg12.9%
hypot-define33.9%
Simplified33.9%
add-sqr-sqrt33.8%
sqrt-unprod33.9%
*-commutative33.9%
*-commutative33.9%
swap-sqr33.9%
add-sqr-sqrt33.9%
*-commutative33.9%
metadata-eval33.9%
Applied egg-rr33.9%
associate-*l*33.9%
metadata-eval33.9%
Simplified33.9%
Taylor expanded in re around inf 81.6%
associate-*l*81.7%
unpow281.7%
rem-square-sqrt82.6%
rem-exp-log77.7%
exp-neg77.7%
unpow1/277.7%
exp-prod77.7%
distribute-lft-neg-out77.7%
distribute-rgt-neg-in77.7%
metadata-eval77.7%
exp-to-pow82.7%
Simplified82.7%
Final simplification80.8%
(FPCore (re im) :precision binary64 (if (<= re -3.3e-39) (* 0.5 (sqrt (* re -4.0))) (if (<= re 800.0) (sqrt (* 0.5 (- im re))) (* 0.5 (/ im (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -3.3e-39) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 800.0) {
tmp = sqrt((0.5 * (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 <= (-3.3d-39)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 800.0d0) then
tmp = sqrt((0.5d0 * (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 <= -3.3e-39) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 800.0) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = 0.5 * (im / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.3e-39: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 800.0: tmp = math.sqrt((0.5 * (im - re))) else: tmp = 0.5 * (im / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.3e-39) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 800.0) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64(0.5 * Float64(im / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.3e-39) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 800.0) tmp = sqrt((0.5 * (im - re))); else tmp = 0.5 * (im / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.3e-39], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 800.0], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.3 \cdot 10^{-39}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 800:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -3.29999999999999985e-39Initial program 47.5%
Taylor expanded in re around -inf 80.0%
*-commutative80.0%
Simplified80.0%
if -3.29999999999999985e-39 < re < 800Initial program 52.5%
sub-neg52.5%
sqr-neg52.5%
sub-neg52.5%
sqr-neg52.5%
hypot-define90.1%
Simplified90.1%
add-sqr-sqrt89.3%
sqrt-unprod90.1%
*-commutative90.1%
*-commutative90.1%
swap-sqr90.1%
add-sqr-sqrt90.1%
*-commutative90.1%
metadata-eval90.1%
Applied egg-rr90.1%
associate-*l*90.1%
metadata-eval90.1%
Simplified90.1%
Taylor expanded in re around 0 80.0%
neg-mul-180.0%
unsub-neg80.0%
Simplified80.0%
if 800 < re Initial program 12.9%
Taylor expanded in re around inf 81.6%
associate-*r*82.0%
Simplified82.0%
sqrt-div81.9%
metadata-eval81.9%
un-div-inv81.9%
associate-*l*81.5%
sqrt-unprod82.6%
metadata-eval82.6%
metadata-eval82.6%
*-commutative82.6%
*-un-lft-identity82.6%
Applied egg-rr82.6%
Final simplification80.8%
(FPCore (re im) :precision binary64 (if (<= re -2.25e-266) (sqrt (* 0.5 (- im re))) (sqrt (* im 0.5))))
double code(double re, double im) {
double tmp;
if (re <= -2.25e-266) {
tmp = sqrt((0.5 * (im - 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 <= (-2.25d-266)) then
tmp = sqrt((0.5d0 * (im - 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 <= -2.25e-266) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.25e-266: tmp = math.sqrt((0.5 * (im - re))) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.25e-266) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = sqrt(Float64(im * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.25e-266) tmp = sqrt((0.5 * (im - re))); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.25e-266], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.25 \cdot 10^{-266}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < -2.2500000000000001e-266Initial program 52.8%
sub-neg52.8%
sqr-neg52.8%
sub-neg52.8%
sqr-neg52.8%
hypot-define100.0%
Simplified100.0%
add-sqr-sqrt99.2%
sqrt-unprod100.0%
*-commutative100.0%
*-commutative100.0%
swap-sqr100.0%
add-sqr-sqrt100.0%
*-commutative100.0%
metadata-eval100.0%
Applied egg-rr100.0%
associate-*l*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around 0 51.3%
neg-mul-151.3%
unsub-neg51.3%
Simplified51.3%
if -2.2500000000000001e-266 < re Initial program 29.3%
sub-neg29.3%
sqr-neg29.3%
sub-neg29.3%
sqr-neg29.3%
hypot-define58.3%
Simplified58.3%
add-sqr-sqrt57.8%
sqrt-unprod58.3%
*-commutative58.3%
*-commutative58.3%
swap-sqr58.3%
add-sqr-sqrt58.3%
*-commutative58.3%
metadata-eval58.3%
Applied egg-rr58.3%
associate-*l*58.3%
metadata-eval58.3%
Simplified58.3%
Taylor expanded in re around 0 51.2%
Final simplification51.3%
(FPCore (re im) :precision binary64 (if (<= re -2.7e-39) (* 0.5 (sqrt (* re -4.0))) (sqrt (* im 0.5))))
double code(double re, double im) {
double tmp;
if (re <= -2.7e-39) {
tmp = 0.5 * sqrt((re * -4.0));
} 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 <= (-2.7d-39)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else
tmp = sqrt((im * 0.5d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.7e-39) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.7e-39: tmp = 0.5 * math.sqrt((re * -4.0)) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.7e-39) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); else tmp = sqrt(Float64(im * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.7e-39) tmp = 0.5 * sqrt((re * -4.0)); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.7e-39], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.7 \cdot 10^{-39}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < -2.7000000000000001e-39Initial program 47.5%
Taylor expanded in re around -inf 80.0%
*-commutative80.0%
Simplified80.0%
if -2.7000000000000001e-39 < re Initial program 36.5%
sub-neg36.5%
sqr-neg36.5%
sub-neg36.5%
sqr-neg36.5%
hypot-define67.4%
Simplified67.4%
add-sqr-sqrt66.8%
sqrt-unprod67.4%
*-commutative67.4%
*-commutative67.4%
swap-sqr67.4%
add-sqr-sqrt67.4%
*-commutative67.4%
metadata-eval67.4%
Applied egg-rr67.4%
associate-*l*67.4%
metadata-eval67.4%
Simplified67.4%
Taylor expanded in re around 0 56.1%
Final simplification62.9%
(FPCore (re im) :precision binary64 (sqrt (* im 0.5)))
double code(double re, double im) {
return sqrt((im * 0.5));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt((im * 0.5d0))
end function
public static double code(double re, double im) {
return Math.sqrt((im * 0.5));
}
def code(re, im): return math.sqrt((im * 0.5))
function code(re, im) return sqrt(Float64(im * 0.5)) end
function tmp = code(re, im) tmp = sqrt((im * 0.5)); end
code[re_, im_] := N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{im \cdot 0.5}
\end{array}
Initial program 39.6%
sub-neg39.6%
sqr-neg39.6%
sub-neg39.6%
sqr-neg39.6%
hypot-define76.7%
Simplified76.7%
add-sqr-sqrt76.1%
sqrt-unprod76.7%
*-commutative76.7%
*-commutative76.7%
swap-sqr76.7%
add-sqr-sqrt76.7%
*-commutative76.7%
metadata-eval76.7%
Applied egg-rr76.7%
associate-*l*76.7%
metadata-eval76.7%
Simplified76.7%
Taylor expanded in re around 0 47.5%
Final simplification47.5%
herbie shell --seed 2024053
(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)))))