
(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 4 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 4.9e-20) (sqrt (* 0.5 (- (hypot re im) re))) (* (pow re -0.5) (* 0.5 im))))
double code(double re, double im) {
double tmp;
if (re <= 4.9e-20) {
tmp = sqrt((0.5 * (hypot(re, im) - re)));
} else {
tmp = pow(re, -0.5) * (0.5 * im);
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 4.9e-20) {
tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
} else {
tmp = Math.pow(re, -0.5) * (0.5 * im);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 4.9e-20: tmp = math.sqrt((0.5 * (math.hypot(re, im) - re))) else: tmp = math.pow(re, -0.5) * (0.5 * im) return tmp
function code(re, im) tmp = 0.0 if (re <= 4.9e-20) tmp = sqrt(Float64(0.5 * Float64(hypot(re, im) - re))); else tmp = Float64((re ^ -0.5) * Float64(0.5 * im)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 4.9e-20) tmp = sqrt((0.5 * (hypot(re, im) - re))); else tmp = (re ^ -0.5) * (0.5 * im); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 4.9e-20], N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[Power[re, -0.5], $MachinePrecision] * N[(0.5 * im), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 4.9 \cdot 10^{-20}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;{re}^{-0.5} \cdot \left(0.5 \cdot im\right)\\
\end{array}
\end{array}
if re < 4.9000000000000002e-20Initial program 48.6%
*-commutative48.6%
hypot-udef91.9%
*-commutative91.9%
add-sqr-sqrt91.1%
sqrt-unprod91.9%
*-commutative91.9%
*-commutative91.9%
swap-sqr91.9%
add-sqr-sqrt91.9%
metadata-eval91.9%
Applied egg-rr91.9%
*-commutative91.9%
associate-*r*91.9%
metadata-eval91.9%
Simplified91.9%
if 4.9000000000000002e-20 < re Initial program 16.7%
Taylor expanded in im around 0 89.4%
associate-*r*89.4%
*-commutative89.4%
Simplified89.4%
add-log-exp33.0%
*-un-lft-identity33.0%
log-prod33.0%
metadata-eval33.0%
add-log-exp89.4%
associate-*l*89.4%
*-commutative89.4%
associate-*r*89.4%
associate-*l*89.4%
associate-*r*89.4%
sqrt-unprod89.9%
sqrt-unprod89.9%
un-div-inv89.9%
Applied egg-rr89.9%
+-lft-identity89.9%
*-commutative89.9%
associate-*r/89.9%
metadata-eval89.9%
unpow1/289.9%
unpow-189.9%
exp-to-pow85.1%
exp-prod85.1%
associate-*l*85.1%
metadata-eval85.1%
exp-to-pow90.0%
Simplified90.0%
Final simplification91.5%
(FPCore (re im)
:precision binary64
(if (<= re -1.5e+20)
(* 0.5 (sqrt (* 2.0 (* re -2.0))))
(if (<= re 0.00075)
(* 0.5 (sqrt (* 2.0 (- im re))))
(* (pow re -0.5) (* 0.5 im)))))
double code(double re, double im) {
double tmp;
if (re <= -1.5e+20) {
tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
} else if (re <= 0.00075) {
tmp = 0.5 * sqrt((2.0 * (im - re)));
} else {
tmp = pow(re, -0.5) * (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.5d+20)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
else if (re <= 0.00075d0) then
tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
else
tmp = (re ** (-0.5d0)) * (0.5d0 * im)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.5e+20) {
tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
} else if (re <= 0.00075) {
tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
} else {
tmp = Math.pow(re, -0.5) * (0.5 * im);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.5e+20: tmp = 0.5 * math.sqrt((2.0 * (re * -2.0))) elif re <= 0.00075: tmp = 0.5 * math.sqrt((2.0 * (im - re))) else: tmp = math.pow(re, -0.5) * (0.5 * im) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.5e+20) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))); elseif (re <= 0.00075) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re)))); else tmp = Float64((re ^ -0.5) * Float64(0.5 * im)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.5e+20) tmp = 0.5 * sqrt((2.0 * (re * -2.0))); elseif (re <= 0.00075) tmp = 0.5 * sqrt((2.0 * (im - re))); else tmp = (re ^ -0.5) * (0.5 * im); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.5e+20], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 0.00075], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Power[re, -0.5], $MachinePrecision] * N[(0.5 * im), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.5 \cdot 10^{+20}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{elif}\;re \leq 0.00075:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;{re}^{-0.5} \cdot \left(0.5 \cdot im\right)\\
\end{array}
\end{array}
if re < -1.5e20Initial program 35.6%
Taylor expanded in re around -inf 79.3%
*-commutative79.3%
Simplified79.3%
if -1.5e20 < re < 7.5000000000000002e-4Initial program 53.6%
Taylor expanded in re around 0 77.7%
if 7.5000000000000002e-4 < re Initial program 16.7%
Taylor expanded in im around 0 89.4%
associate-*r*89.4%
*-commutative89.4%
Simplified89.4%
add-log-exp33.0%
*-un-lft-identity33.0%
log-prod33.0%
metadata-eval33.0%
add-log-exp89.4%
associate-*l*89.4%
*-commutative89.4%
associate-*r*89.4%
associate-*l*89.4%
associate-*r*89.4%
sqrt-unprod89.9%
sqrt-unprod89.9%
un-div-inv89.9%
Applied egg-rr89.9%
+-lft-identity89.9%
*-commutative89.9%
associate-*r/89.9%
metadata-eval89.9%
unpow1/289.9%
unpow-189.9%
exp-to-pow85.1%
exp-prod85.1%
associate-*l*85.1%
metadata-eval85.1%
exp-to-pow90.0%
Simplified90.0%
Final simplification80.8%
(FPCore (re im) :precision binary64 (if (<= re -5e-310) (* 0.5 (sqrt (* 2.0 (* re -2.0)))) (* (pow re -0.5) (* 0.5 im))))
double code(double re, double im) {
double tmp;
if (re <= -5e-310) {
tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
} else {
tmp = pow(re, -0.5) * (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 <= (-5d-310)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
else
tmp = (re ** (-0.5d0)) * (0.5d0 * im)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -5e-310) {
tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
} else {
tmp = Math.pow(re, -0.5) * (0.5 * im);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -5e-310: tmp = 0.5 * math.sqrt((2.0 * (re * -2.0))) else: tmp = math.pow(re, -0.5) * (0.5 * im) return tmp
function code(re, im) tmp = 0.0 if (re <= -5e-310) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))); else tmp = Float64((re ^ -0.5) * Float64(0.5 * im)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -5e-310) tmp = 0.5 * sqrt((2.0 * (re * -2.0))); else tmp = (re ^ -0.5) * (0.5 * im); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -5e-310], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Power[re, -0.5], $MachinePrecision] * N[(0.5 * im), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -5 \cdot 10^{-310}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{else}:\\
\;\;\;\;{re}^{-0.5} \cdot \left(0.5 \cdot im\right)\\
\end{array}
\end{array}
if re < -4.999999999999985e-310Initial program 53.4%
Taylor expanded in re around -inf 47.1%
*-commutative47.1%
Simplified47.1%
if -4.999999999999985e-310 < re Initial program 27.3%
Taylor expanded in im around 0 60.3%
associate-*r*60.3%
*-commutative60.3%
Simplified60.3%
add-log-exp18.3%
*-un-lft-identity18.3%
log-prod18.3%
metadata-eval18.3%
add-log-exp60.3%
associate-*l*60.3%
*-commutative60.3%
associate-*r*60.4%
associate-*l*60.3%
associate-*r*60.4%
sqrt-unprod60.6%
sqrt-unprod60.7%
un-div-inv60.7%
Applied egg-rr60.7%
+-lft-identity60.7%
*-commutative60.7%
associate-*r/60.7%
metadata-eval60.7%
unpow1/260.7%
unpow-160.7%
exp-to-pow57.5%
exp-prod57.5%
associate-*l*57.5%
metadata-eval57.5%
exp-to-pow60.8%
Simplified60.8%
Final simplification53.4%
(FPCore (re im) :precision binary64 (* (pow re -0.5) (* 0.5 im)))
double code(double re, double im) {
return pow(re, -0.5) * (0.5 * im);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = (re ** (-0.5d0)) * (0.5d0 * im)
end function
public static double code(double re, double im) {
return Math.pow(re, -0.5) * (0.5 * im);
}
def code(re, im): return math.pow(re, -0.5) * (0.5 * im)
function code(re, im) return Float64((re ^ -0.5) * Float64(0.5 * im)) end
function tmp = code(re, im) tmp = (re ^ -0.5) * (0.5 * im); end
code[re_, im_] := N[(N[Power[re, -0.5], $MachinePrecision] * N[(0.5 * im), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{re}^{-0.5} \cdot \left(0.5 \cdot im\right)
\end{array}
Initial program 41.5%
Taylor expanded in im around 0 27.6%
associate-*r*27.6%
*-commutative27.6%
Simplified27.6%
add-log-exp8.4%
*-un-lft-identity8.4%
log-prod8.4%
metadata-eval8.4%
add-log-exp27.6%
associate-*l*27.6%
*-commutative27.6%
associate-*r*27.6%
associate-*l*27.6%
associate-*r*27.6%
sqrt-unprod27.7%
sqrt-unprod27.7%
un-div-inv27.7%
Applied egg-rr27.7%
+-lft-identity27.7%
*-commutative27.7%
associate-*r/27.7%
metadata-eval27.7%
unpow1/227.7%
unpow-127.7%
exp-to-pow26.3%
exp-prod26.3%
associate-*l*26.3%
metadata-eval26.3%
exp-to-pow27.8%
Simplified27.8%
Final simplification27.8%
herbie shell --seed 2023264
(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)))))