
(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 2.8e+73) (sqrt (* 0.5 (- (hypot im re) re))) (* (* 0.5 im) (pow re -0.5))))
double code(double re, double im) {
double tmp;
if (re <= 2.8e+73) {
tmp = sqrt((0.5 * (hypot(im, re) - re)));
} else {
tmp = (0.5 * im) * pow(re, -0.5);
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 2.8e+73) {
tmp = Math.sqrt((0.5 * (Math.hypot(im, re) - re)));
} else {
tmp = (0.5 * im) * Math.pow(re, -0.5);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 2.8e+73: tmp = math.sqrt((0.5 * (math.hypot(im, re) - re))) else: tmp = (0.5 * im) * math.pow(re, -0.5) return tmp
function code(re, im) tmp = 0.0 if (re <= 2.8e+73) tmp = sqrt(Float64(0.5 * Float64(hypot(im, re) - re))); else tmp = Float64(Float64(0.5 * im) * (re ^ -0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 2.8e+73) tmp = sqrt((0.5 * (hypot(im, re) - re))); else tmp = (0.5 * im) * (re ^ -0.5); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 2.8e+73], N[Sqrt[N[(0.5 * N[(N[Sqrt[im ^ 2 + re ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 2.8 \cdot 10^{+73}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(im, re\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\left(0.5 \cdot im\right) \cdot {re}^{-0.5}\\
\end{array}
\end{array}
if re < 2.80000000000000008e73Initial program 48.2%
pow148.2%
Applied egg-rr90.3%
unpow190.3%
*-commutative90.3%
associate-*r*90.3%
metadata-eval90.3%
hypot-undefine48.2%
unpow248.2%
unpow248.2%
+-commutative48.2%
unpow248.2%
unpow248.2%
hypot-undefine90.3%
Simplified90.3%
if 2.80000000000000008e73 < re Initial program 8.8%
Taylor expanded in re around inf 81.7%
associate-*l*81.7%
*-commutative81.7%
associate-*r*82.1%
Simplified82.1%
pow182.1%
add-log-exp13.6%
associate-*l*13.6%
exp-prod13.6%
sqrt-unprod13.6%
metadata-eval13.6%
metadata-eval13.6%
pow113.6%
add-log-exp82.6%
inv-pow82.6%
sqrt-pow182.8%
metadata-eval82.8%
Applied egg-rr82.8%
unpow182.8%
associate-*r*82.8%
*-commutative82.8%
Simplified82.8%
Final simplification88.7%
(FPCore (re im) :precision binary64 (if (<= re -3.9e+90) (sqrt (- re)) (if (<= re 3.9e+70) (sqrt (* 0.5 (- im re))) (* (* 0.5 im) (pow re -0.5)))))
double code(double re, double im) {
double tmp;
if (re <= -3.9e+90) {
tmp = sqrt(-re);
} else if (re <= 3.9e+70) {
tmp = sqrt((0.5 * (im - re)));
} else {
tmp = (0.5 * im) * 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 <= (-3.9d+90)) then
tmp = sqrt(-re)
else if (re <= 3.9d+70) then
tmp = sqrt((0.5d0 * (im - re)))
else
tmp = (0.5d0 * im) * (re ** (-0.5d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3.9e+90) {
tmp = Math.sqrt(-re);
} else if (re <= 3.9e+70) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = (0.5 * im) * Math.pow(re, -0.5);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.9e+90: tmp = math.sqrt(-re) elif re <= 3.9e+70: tmp = math.sqrt((0.5 * (im - re))) else: tmp = (0.5 * im) * math.pow(re, -0.5) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.9e+90) tmp = sqrt(Float64(-re)); elseif (re <= 3.9e+70) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64(Float64(0.5 * im) * (re ^ -0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.9e+90) tmp = sqrt(-re); elseif (re <= 3.9e+70) tmp = sqrt((0.5 * (im - re))); else tmp = (0.5 * im) * (re ^ -0.5); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.9e+90], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 3.9e+70], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.9 \cdot 10^{+90}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 3.9 \cdot 10^{+70}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\left(0.5 \cdot im\right) \cdot {re}^{-0.5}\\
\end{array}
\end{array}
if re < -3.9000000000000002e90Initial program 19.4%
pow119.4%
Applied egg-rr100.0%
unpow1100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine19.4%
unpow219.4%
unpow219.4%
+-commutative19.4%
unpow219.4%
unpow219.4%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around -inf 83.1%
neg-mul-183.1%
Simplified83.1%
if -3.9000000000000002e90 < re < 3.89999999999999975e70Initial program 54.2%
pow154.2%
Applied egg-rr87.9%
unpow187.9%
*-commutative87.9%
associate-*r*87.9%
metadata-eval87.9%
hypot-undefine54.2%
unpow254.2%
unpow254.2%
+-commutative54.2%
unpow254.2%
unpow254.2%
hypot-undefine87.9%
Simplified87.9%
Taylor expanded in re around 0 77.7%
neg-mul-177.7%
sub-neg77.7%
Simplified77.7%
if 3.89999999999999975e70 < re Initial program 12.1%
Taylor expanded in re around inf 80.7%
associate-*l*80.7%
*-commutative80.7%
associate-*r*81.1%
Simplified81.1%
pow181.1%
add-log-exp15.0%
associate-*l*15.0%
exp-prod15.0%
sqrt-unprod15.0%
metadata-eval15.0%
metadata-eval15.0%
pow115.0%
add-log-exp81.6%
inv-pow81.6%
sqrt-pow181.7%
metadata-eval81.7%
Applied egg-rr81.7%
unpow181.7%
associate-*r*81.7%
*-commutative81.7%
Simplified81.7%
Final simplification79.4%
(FPCore (re im) :precision binary64 (if (<= re -1.42e+89) (sqrt (- re)) (if (<= re 3.2e+70) (sqrt (* 0.5 (- im re))) (* im (/ 0.5 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -1.42e+89) {
tmp = sqrt(-re);
} else if (re <= 3.2e+70) {
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.42d+89)) then
tmp = sqrt(-re)
else if (re <= 3.2d+70) 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.42e+89) {
tmp = Math.sqrt(-re);
} else if (re <= 3.2e+70) {
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.42e+89: tmp = math.sqrt(-re) elif re <= 3.2e+70: 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.42e+89) tmp = sqrt(Float64(-re)); elseif (re <= 3.2e+70) 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.42e+89) tmp = sqrt(-re); elseif (re <= 3.2e+70) 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.42e+89], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 3.2e+70], 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.42 \cdot 10^{+89}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 3.2 \cdot 10^{+70}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -1.42e89Initial program 19.4%
pow119.4%
Applied egg-rr100.0%
unpow1100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine19.4%
unpow219.4%
unpow219.4%
+-commutative19.4%
unpow219.4%
unpow219.4%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around -inf 83.1%
neg-mul-183.1%
Simplified83.1%
if -1.42e89 < re < 3.2000000000000002e70Initial program 54.2%
pow154.2%
Applied egg-rr87.9%
unpow187.9%
*-commutative87.9%
associate-*r*87.9%
metadata-eval87.9%
hypot-undefine54.2%
unpow254.2%
unpow254.2%
+-commutative54.2%
unpow254.2%
unpow254.2%
hypot-undefine87.9%
Simplified87.9%
Taylor expanded in re around 0 77.7%
neg-mul-177.7%
sub-neg77.7%
Simplified77.7%
if 3.2000000000000002e70 < re Initial program 12.1%
Taylor expanded in re around inf 80.7%
associate-*l*80.7%
*-commutative80.7%
associate-*r*81.1%
Simplified81.1%
pow181.1%
add-log-exp15.0%
associate-*l*15.0%
exp-prod15.0%
sqrt-unprod15.0%
metadata-eval15.0%
metadata-eval15.0%
pow115.0%
add-log-exp81.6%
inv-pow81.6%
sqrt-pow181.7%
metadata-eval81.7%
Applied egg-rr81.7%
unpow181.7%
associate-*r*81.7%
*-commutative81.7%
Simplified81.7%
*-commutative81.7%
associate-*r*81.7%
rem-cube-cbrt80.6%
rem-cube-cbrt81.7%
add-sqr-sqrt81.4%
sqrt-unprod58.1%
swap-sqr43.6%
unpow243.6%
pow-prod-up43.6%
metadata-eval43.6%
inv-pow43.6%
div-inv43.6%
sqrt-div59.7%
sqrt-pow181.6%
metadata-eval81.6%
pow181.6%
associate-*r/81.7%
Applied egg-rr81.7%
*-commutative81.7%
associate-/l*81.7%
Applied egg-rr81.7%
(FPCore (re im) :precision binary64 (if (<= re -1.02e+96) (sqrt (- re)) (sqrt (* 0.5 im))))
double code(double re, double im) {
double tmp;
if (re <= -1.02e+96) {
tmp = sqrt(-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.02d+96)) then
tmp = sqrt(-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.02e+96) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt((0.5 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.02e+96: tmp = math.sqrt(-re) else: tmp = math.sqrt((0.5 * im)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.02e+96) tmp = sqrt(Float64(-re)); else tmp = sqrt(Float64(0.5 * im)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.02e+96) tmp = sqrt(-re); else tmp = sqrt((0.5 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.02e+96], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[N[(0.5 * im), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.02 \cdot 10^{+96}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot im}\\
\end{array}
\end{array}
if re < -1.02000000000000001e96Initial program 19.4%
pow119.4%
Applied egg-rr100.0%
unpow1100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine19.4%
unpow219.4%
unpow219.4%
+-commutative19.4%
unpow219.4%
unpow219.4%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around -inf 83.1%
neg-mul-183.1%
Simplified83.1%
if -1.02000000000000001e96 < re Initial program 43.6%
pow143.6%
Applied egg-rr74.4%
unpow174.4%
*-commutative74.4%
associate-*r*74.4%
metadata-eval74.4%
hypot-undefine43.6%
unpow243.6%
unpow243.6%
+-commutative43.6%
unpow243.6%
unpow243.6%
hypot-undefine74.4%
Simplified74.4%
Taylor expanded in im around inf 63.3%
*-commutative63.3%
Simplified63.3%
Final simplification66.2%
(FPCore (re im) :precision binary64 (if (<= re -2e-311) (sqrt (- re)) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= -2e-311) {
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-311)) 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-311) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2e-311: tmp = math.sqrt(-re) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -2e-311) tmp = sqrt(Float64(-re)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2e-311) tmp = sqrt(-re); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2e-311], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2 \cdot 10^{-311}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -1.9999999999999e-311Initial program 49.8%
pow149.8%
Applied egg-rr100.0%
unpow1100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine49.8%
unpow249.8%
unpow249.8%
+-commutative49.8%
unpow249.8%
unpow249.8%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around -inf 45.8%
neg-mul-145.8%
Simplified45.8%
if -1.9999999999999e-311 < re Initial program 31.9%
pow131.9%
Applied egg-rr60.1%
unpow160.1%
*-commutative60.1%
associate-*r*60.1%
metadata-eval60.1%
hypot-undefine31.9%
unpow231.9%
unpow231.9%
+-commutative31.9%
unpow231.9%
unpow231.9%
hypot-undefine60.1%
Simplified60.1%
Taylor expanded in re around -inf 0.0%
neg-mul-10.0%
Simplified0.0%
expm1-log1p-u0.0%
expm1-undefine0.0%
add-sqr-sqrt0.0%
sqrt-unprod4.0%
sqr-neg4.0%
sqrt-unprod4.6%
add-sqr-sqrt4.6%
Applied egg-rr4.6%
sub-neg4.6%
metadata-eval4.6%
+-commutative4.6%
log1p-undefine4.6%
rem-exp-log4.6%
associate-+r+5.8%
metadata-eval5.8%
metadata-eval5.8%
remove-double-neg5.8%
distribute-neg-in5.8%
+-lft-identity5.8%
remove-double-neg5.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 40.0%
pow140.0%
Applied egg-rr78.2%
unpow178.2%
*-commutative78.2%
associate-*r*78.2%
metadata-eval78.2%
hypot-undefine40.0%
unpow240.0%
unpow240.0%
+-commutative40.0%
unpow240.0%
unpow240.0%
hypot-undefine78.2%
Simplified78.2%
Taylor expanded in re around -inf 20.8%
neg-mul-120.8%
Simplified20.8%
expm1-log1p-u19.6%
expm1-undefine14.2%
add-sqr-sqrt14.2%
sqrt-unprod7.7%
sqr-neg7.7%
sqrt-unprod2.5%
add-sqr-sqrt2.5%
Applied egg-rr2.5%
sub-neg2.5%
metadata-eval2.5%
+-commutative2.5%
log1p-undefine2.5%
rem-exp-log2.5%
associate-+r+3.2%
metadata-eval3.2%
metadata-eval3.2%
remove-double-neg3.2%
distribute-neg-in3.2%
+-lft-identity3.2%
remove-double-neg3.2%
Simplified3.2%
herbie shell --seed 2024163
(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)))))