
(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 (<= (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))) 0.0) (* (pow re -0.5) (* im 0.5)) (sqrt (* 0.5 (- (hypot im re) re)))))
double code(double re, double im) {
double tmp;
if (sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0) {
tmp = pow(re, -0.5) * (im * 0.5);
} else {
tmp = sqrt((0.5 * (hypot(im, re) - 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 = Math.pow(re, -0.5) * (im * 0.5);
} else {
tmp = Math.sqrt((0.5 * (Math.hypot(im, re) - re)));
}
return tmp;
}
def code(re, im): tmp = 0 if math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re))) <= 0.0: tmp = math.pow(re, -0.5) * (im * 0.5) else: tmp = math.sqrt((0.5 * (math.hypot(im, re) - 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((re ^ -0.5) * Float64(im * 0.5)); else tmp = sqrt(Float64(0.5 * Float64(hypot(im, re) - 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 = (re ^ -0.5) * (im * 0.5); else tmp = sqrt((0.5 * (hypot(im, re) - 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[(N[Power[re, -0.5], $MachinePrecision] * N[(im * 0.5), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(N[Sqrt[im ^ 2 + re ^ 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:\\
\;\;\;\;{re}^{-0.5} \cdot \left(im \cdot 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(im, re\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 5.8%
sub-neg5.8%
sqr-neg5.8%
sub-neg5.8%
sqr-neg5.8%
hypot-define5.8%
Simplified5.8%
*-commutative5.8%
hypot-define5.8%
*-commutative5.8%
add-sqr-sqrt5.8%
sqrt-unprod5.8%
*-commutative5.8%
*-commutative5.8%
swap-sqr5.8%
Applied egg-rr5.8%
*-commutative5.8%
associate-*r*5.8%
metadata-eval5.8%
hypot-undefine5.8%
unpow25.8%
unpow25.8%
+-commutative5.8%
unpow25.8%
unpow25.8%
hypot-undefine5.8%
Simplified5.8%
Taylor expanded in im around 0 98.1%
*-commutative98.1%
*-commutative98.1%
unpow298.1%
rem-square-sqrt99.6%
Simplified99.6%
*-un-lft-identity99.6%
inv-pow99.6%
sqrt-pow199.6%
metadata-eval99.6%
Applied egg-rr99.6%
*-lft-identity99.6%
Simplified99.6%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 44.5%
sub-neg44.5%
sqr-neg44.5%
sub-neg44.5%
sqr-neg44.5%
hypot-define90.0%
Simplified90.0%
*-commutative90.0%
hypot-define44.5%
*-commutative44.5%
add-sqr-sqrt44.2%
sqrt-unprod44.5%
*-commutative44.5%
*-commutative44.5%
swap-sqr44.5%
Applied egg-rr90.0%
*-commutative90.0%
associate-*r*90.0%
metadata-eval90.0%
hypot-undefine44.5%
unpow244.5%
unpow244.5%
+-commutative44.5%
unpow244.5%
unpow244.5%
hypot-undefine90.0%
Simplified90.0%
Final simplification91.2%
(FPCore (re im) :precision binary64 (if (<= re -1.066e+45) (sqrt (- re)) (if (<= re 1.45e-52) (sqrt (* 0.5 (- im re))) (* im (sqrt (/ 0.25 re))))))
double code(double re, double im) {
double tmp;
if (re <= -1.066e+45) {
tmp = sqrt(-re);
} else if (re <= 1.45e-52) {
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 <= (-1.066d+45)) then
tmp = sqrt(-re)
else if (re <= 1.45d-52) 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 <= -1.066e+45) {
tmp = Math.sqrt(-re);
} else if (re <= 1.45e-52) {
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 <= -1.066e+45: tmp = math.sqrt(-re) elif re <= 1.45e-52: 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 <= -1.066e+45) tmp = sqrt(Float64(-re)); elseif (re <= 1.45e-52) 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 <= -1.066e+45) tmp = sqrt(-re); elseif (re <= 1.45e-52) tmp = sqrt((0.5 * (im - re))); else tmp = im * sqrt((0.25 / re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.066e+45], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 1.45e-52], 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 -1.066 \cdot 10^{+45}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 1.45 \cdot 10^{-52}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\
\end{array}
\end{array}
if re < -1.06600000000000005e45Initial program 37.4%
sub-neg37.4%
sqr-neg37.4%
sub-neg37.4%
sqr-neg37.4%
hypot-define100.0%
Simplified100.0%
*-commutative100.0%
hypot-define37.4%
*-commutative37.4%
add-sqr-sqrt37.1%
sqrt-unprod37.4%
*-commutative37.4%
*-commutative37.4%
swap-sqr37.4%
Applied egg-rr100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine37.4%
unpow237.4%
unpow237.4%
+-commutative37.4%
unpow237.4%
unpow237.4%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around -inf 87.8%
neg-mul-187.8%
Simplified87.8%
if -1.06600000000000005e45 < re < 1.4500000000000001e-52Initial program 55.6%
sub-neg55.6%
sqr-neg55.6%
sub-neg55.6%
sqr-neg55.6%
hypot-define91.2%
Simplified91.2%
*-commutative91.2%
hypot-define55.6%
*-commutative55.6%
add-sqr-sqrt55.2%
sqrt-unprod55.6%
*-commutative55.6%
*-commutative55.6%
swap-sqr55.6%
Applied egg-rr91.2%
*-commutative91.2%
associate-*r*91.2%
metadata-eval91.2%
hypot-undefine55.6%
unpow255.6%
unpow255.6%
+-commutative55.6%
unpow255.6%
unpow255.6%
hypot-undefine91.2%
Simplified91.2%
Taylor expanded in re around 0 81.3%
neg-mul-181.3%
sub-neg81.3%
Simplified81.3%
if 1.4500000000000001e-52 < re Initial program 12.1%
sub-neg12.1%
sqr-neg12.1%
sub-neg12.1%
sqr-neg12.1%
hypot-define43.8%
Simplified43.8%
*-commutative43.8%
hypot-define12.1%
*-commutative12.1%
add-sqr-sqrt12.1%
sqrt-unprod12.1%
*-commutative12.1%
*-commutative12.1%
swap-sqr12.1%
Applied egg-rr43.8%
*-commutative43.8%
associate-*r*43.8%
metadata-eval43.8%
hypot-undefine12.1%
unpow212.1%
unpow212.1%
+-commutative12.1%
unpow212.1%
unpow212.1%
hypot-undefine43.8%
Simplified43.8%
Taylor expanded in im around 0 70.6%
*-commutative70.6%
*-commutative70.6%
unpow270.6%
rem-square-sqrt71.5%
Simplified71.5%
*-commutative71.5%
sqrt-div71.4%
metadata-eval71.4%
un-div-inv71.5%
*-commutative71.5%
Applied egg-rr71.5%
associate-*r/71.4%
Simplified71.4%
*-un-lft-identity71.4%
*-commutative71.4%
add-sqr-sqrt71.1%
sqrt-unprod71.4%
frac-times71.3%
metadata-eval71.3%
add-sqr-sqrt71.5%
Applied egg-rr71.5%
*-rgt-identity71.5%
Simplified71.5%
(FPCore (re im) :precision binary64 (if (<= re -1.066e+45) (sqrt (- re)) (sqrt (* im 0.5))))
double code(double re, double im) {
double tmp;
if (re <= -1.066e+45) {
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 <= (-1.066d+45)) 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 <= -1.066e+45) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.066e+45: tmp = math.sqrt(-re) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.066e+45) 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 <= -1.066e+45) tmp = sqrt(-re); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.066e+45], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.066 \cdot 10^{+45}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < -1.06600000000000005e45Initial program 37.4%
sub-neg37.4%
sqr-neg37.4%
sub-neg37.4%
sqr-neg37.4%
hypot-define100.0%
Simplified100.0%
*-commutative100.0%
hypot-define37.4%
*-commutative37.4%
add-sqr-sqrt37.1%
sqrt-unprod37.4%
*-commutative37.4%
*-commutative37.4%
swap-sqr37.4%
Applied egg-rr100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine37.4%
unpow237.4%
unpow237.4%
+-commutative37.4%
unpow237.4%
unpow237.4%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around -inf 87.8%
neg-mul-187.8%
Simplified87.8%
if -1.06600000000000005e45 < re Initial program 40.5%
sub-neg40.5%
sqr-neg40.5%
sub-neg40.5%
sqr-neg40.5%
hypot-define74.7%
Simplified74.7%
*-commutative74.7%
hypot-define40.5%
*-commutative40.5%
add-sqr-sqrt40.2%
sqrt-unprod40.5%
*-commutative40.5%
*-commutative40.5%
swap-sqr40.5%
Applied egg-rr74.7%
*-commutative74.7%
associate-*r*74.7%
metadata-eval74.7%
hypot-undefine40.5%
unpow240.5%
unpow240.5%
+-commutative40.5%
unpow240.5%
unpow240.5%
hypot-undefine74.7%
Simplified74.7%
Taylor expanded in im around inf 63.4%
Final simplification68.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 54.3%
sub-neg54.3%
sqr-neg54.3%
sub-neg54.3%
sqr-neg54.3%
hypot-define100.0%
Simplified100.0%
*-commutative100.0%
hypot-define54.3%
*-commutative54.3%
add-sqr-sqrt53.9%
sqrt-unprod54.3%
*-commutative54.3%
*-commutative54.3%
swap-sqr54.3%
Applied egg-rr100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine54.3%
unpow254.3%
unpow254.3%
+-commutative54.3%
unpow254.3%
unpow254.3%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around -inf 48.2%
neg-mul-148.2%
Simplified48.2%
if -1.999999999999994e-310 < re Initial program 24.2%
sub-neg24.2%
sqr-neg24.2%
sub-neg24.2%
sqr-neg24.2%
hypot-define58.0%
Simplified58.0%
*-commutative58.0%
hypot-define24.2%
*-commutative24.2%
add-sqr-sqrt24.0%
sqrt-unprod24.2%
*-commutative24.2%
*-commutative24.2%
swap-sqr24.2%
Applied egg-rr58.0%
*-commutative58.0%
associate-*r*58.0%
metadata-eval58.0%
hypot-undefine24.2%
unpow224.2%
unpow224.2%
+-commutative24.2%
unpow224.2%
unpow224.2%
hypot-undefine58.0%
Simplified58.0%
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.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 39.8%
sub-neg39.8%
sqr-neg39.8%
sub-neg39.8%
sqr-neg39.8%
hypot-define79.8%
Simplified79.8%
*-commutative79.8%
hypot-define39.8%
*-commutative39.8%
add-sqr-sqrt39.6%
sqrt-unprod39.8%
*-commutative39.8%
*-commutative39.8%
swap-sqr39.8%
Applied egg-rr79.8%
*-commutative79.8%
associate-*r*79.8%
metadata-eval79.8%
hypot-undefine39.8%
unpow239.8%
unpow239.8%
+-commutative39.8%
unpow239.8%
unpow239.8%
hypot-undefine79.8%
Simplified79.8%
Taylor expanded in re around -inf 25.0%
neg-mul-125.0%
Simplified25.0%
neg-sub025.0%
sub-neg25.0%
add-sqr-sqrt25.0%
sqrt-unprod16.2%
sqr-neg16.2%
sqrt-unprod2.8%
add-sqr-sqrt2.8%
Applied egg-rr2.8%
+-lft-identity2.8%
Simplified2.8%
herbie shell --seed 2024137
(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)))))