
(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) (* im (sqrt (/ 0.25 re))) (sqrt (* (- (hypot re im) re) 0.5))))
double code(double re, double im) {
double tmp;
if (sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0) {
tmp = im * sqrt((0.25 / re));
} else {
tmp = sqrt(((hypot(re, im) - re) * 0.5));
}
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 = im * Math.sqrt((0.25 / re));
} else {
tmp = Math.sqrt(((Math.hypot(re, im) - re) * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re))) <= 0.0: tmp = im * math.sqrt((0.25 / re)) else: tmp = math.sqrt(((math.hypot(re, im) - re) * 0.5)) 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(im * sqrt(Float64(0.25 / re))); else tmp = sqrt(Float64(Float64(hypot(re, im) - re) * 0.5)); 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 = im * sqrt((0.25 / re)); else tmp = sqrt(((hypot(re, im) - re) * 0.5)); 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[(im * N[Sqrt[N[(0.25 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision] * 0.5), $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:\\
\;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 0.5}\\
\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 6.0%
Taylor expanded in re around inf 98.1%
*-commutative98.1%
*-commutative98.1%
associate-*l*98.2%
associate-*r*98.7%
Simplified98.7%
*-commutative98.7%
sqrt-div98.6%
metadata-eval98.6%
un-div-inv98.6%
*-commutative98.6%
*-commutative98.6%
associate-*r*98.0%
*-commutative98.0%
sqrt-unprod99.6%
metadata-eval99.6%
metadata-eval99.6%
*-un-lft-identity99.6%
Applied egg-rr99.6%
associate-/l*99.5%
Simplified99.5%
*-un-lft-identity99.5%
*-commutative99.5%
add-sqr-sqrt99.1%
sqrt-unprod99.5%
frac-times99.6%
metadata-eval99.6%
add-sqr-sqrt99.8%
Applied egg-rr99.8%
*-rgt-identity99.8%
Simplified99.8%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 48.7%
add-sqr-sqrt48.3%
sqrt-unprod48.7%
*-commutative48.7%
*-commutative48.7%
swap-sqr48.7%
add-sqr-sqrt48.7%
*-commutative48.7%
hypot-define92.2%
metadata-eval92.2%
Applied egg-rr92.2%
associate-*l*92.2%
metadata-eval92.2%
Simplified92.2%
Final simplification93.1%
(FPCore (re im)
:precision binary64
(if (<= re -7.5e-10)
(sqrt (- re))
(if (<= re 1.45e-106)
(sqrt (* 0.5 (* im (- 1.0 (/ re im)))))
(* im (sqrt (/ 0.25 re))))))
double code(double re, double im) {
double tmp;
if (re <= -7.5e-10) {
tmp = sqrt(-re);
} else if (re <= 1.45e-106) {
tmp = sqrt((0.5 * (im * (1.0 - (re / im)))));
} 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 <= (-7.5d-10)) then
tmp = sqrt(-re)
else if (re <= 1.45d-106) then
tmp = sqrt((0.5d0 * (im * (1.0d0 - (re / im)))))
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 <= -7.5e-10) {
tmp = Math.sqrt(-re);
} else if (re <= 1.45e-106) {
tmp = Math.sqrt((0.5 * (im * (1.0 - (re / im)))));
} else {
tmp = im * Math.sqrt((0.25 / re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -7.5e-10: tmp = math.sqrt(-re) elif re <= 1.45e-106: tmp = math.sqrt((0.5 * (im * (1.0 - (re / im))))) else: tmp = im * math.sqrt((0.25 / re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -7.5e-10) tmp = sqrt(Float64(-re)); elseif (re <= 1.45e-106) tmp = sqrt(Float64(0.5 * Float64(im * Float64(1.0 - Float64(re / im))))); else tmp = Float64(im * sqrt(Float64(0.25 / re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -7.5e-10) tmp = sqrt(-re); elseif (re <= 1.45e-106) tmp = sqrt((0.5 * (im * (1.0 - (re / im))))); else tmp = im * sqrt((0.25 / re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -7.5e-10], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 1.45e-106], N[Sqrt[N[(0.5 * N[(im * N[(1.0 - N[(re / im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(im * N[Sqrt[N[(0.25 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -7.5 \cdot 10^{-10}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 1.45 \cdot 10^{-106}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im \cdot \left(1 - \frac{re}{im}\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\
\end{array}
\end{array}
if re < -7.49999999999999995e-10Initial program 40.8%
add-sqr-sqrt40.5%
sqrt-unprod40.8%
*-commutative40.8%
*-commutative40.8%
swap-sqr40.8%
add-sqr-sqrt40.8%
*-commutative40.8%
hypot-define100.0%
metadata-eval100.0%
Applied egg-rr100.0%
associate-*l*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 76.4%
neg-mul-176.4%
Simplified76.4%
if -7.49999999999999995e-10 < re < 1.45e-106Initial program 61.5%
add-sqr-sqrt61.1%
sqrt-unprod61.5%
*-commutative61.5%
*-commutative61.5%
swap-sqr61.5%
add-sqr-sqrt61.5%
*-commutative61.5%
hypot-define97.3%
metadata-eval97.3%
Applied egg-rr97.3%
associate-*l*97.3%
metadata-eval97.3%
Simplified97.3%
Taylor expanded in im around inf 86.8%
mul-1-neg86.8%
unsub-neg86.8%
Simplified86.8%
if 1.45e-106 < re Initial program 20.0%
Taylor expanded in re around inf 73.7%
*-commutative73.7%
*-commutative73.7%
associate-*l*73.7%
associate-*r*74.0%
Simplified74.0%
*-commutative74.0%
sqrt-div74.0%
metadata-eval74.0%
un-div-inv74.0%
*-commutative74.0%
*-commutative74.0%
associate-*r*73.7%
*-commutative73.7%
sqrt-unprod74.7%
metadata-eval74.7%
metadata-eval74.7%
*-un-lft-identity74.7%
Applied egg-rr74.7%
associate-/l*74.6%
Simplified74.6%
*-un-lft-identity74.6%
*-commutative74.6%
add-sqr-sqrt74.4%
sqrt-unprod74.6%
frac-times74.7%
metadata-eval74.7%
add-sqr-sqrt74.7%
Applied egg-rr74.7%
*-rgt-identity74.7%
Simplified74.7%
Final simplification80.3%
(FPCore (re im) :precision binary64 (if (<= re -1.25e-9) (sqrt (- re)) (if (<= re 8e-107) (sqrt (* 0.5 (- im re))) (* im (sqrt (/ 0.25 re))))))
double code(double re, double im) {
double tmp;
if (re <= -1.25e-9) {
tmp = sqrt(-re);
} else if (re <= 8e-107) {
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.25d-9)) then
tmp = sqrt(-re)
else if (re <= 8d-107) 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.25e-9) {
tmp = Math.sqrt(-re);
} else if (re <= 8e-107) {
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.25e-9: tmp = math.sqrt(-re) elif re <= 8e-107: 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.25e-9) tmp = sqrt(Float64(-re)); elseif (re <= 8e-107) 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.25e-9) tmp = sqrt(-re); elseif (re <= 8e-107) 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.25e-9], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 8e-107], 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.25 \cdot 10^{-9}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 8 \cdot 10^{-107}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\
\end{array}
\end{array}
if re < -1.25e-9Initial program 40.8%
add-sqr-sqrt40.5%
sqrt-unprod40.8%
*-commutative40.8%
*-commutative40.8%
swap-sqr40.8%
add-sqr-sqrt40.8%
*-commutative40.8%
hypot-define100.0%
metadata-eval100.0%
Applied egg-rr100.0%
associate-*l*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 76.4%
neg-mul-176.4%
Simplified76.4%
if -1.25e-9 < re < 8e-107Initial program 61.5%
add-sqr-sqrt61.1%
sqrt-unprod61.5%
*-commutative61.5%
*-commutative61.5%
swap-sqr61.5%
add-sqr-sqrt61.5%
*-commutative61.5%
hypot-define97.3%
metadata-eval97.3%
Applied egg-rr97.3%
associate-*l*97.3%
metadata-eval97.3%
Simplified97.3%
Taylor expanded in re around 0 86.8%
neg-mul-186.8%
unsub-neg86.8%
Simplified86.8%
if 8e-107 < re Initial program 20.0%
Taylor expanded in re around inf 73.7%
*-commutative73.7%
*-commutative73.7%
associate-*l*73.7%
associate-*r*74.0%
Simplified74.0%
*-commutative74.0%
sqrt-div74.0%
metadata-eval74.0%
un-div-inv74.0%
*-commutative74.0%
*-commutative74.0%
associate-*r*73.7%
*-commutative73.7%
sqrt-unprod74.7%
metadata-eval74.7%
metadata-eval74.7%
*-un-lft-identity74.7%
Applied egg-rr74.7%
associate-/l*74.6%
Simplified74.6%
*-un-lft-identity74.6%
*-commutative74.6%
add-sqr-sqrt74.4%
sqrt-unprod74.6%
frac-times74.7%
metadata-eval74.7%
add-sqr-sqrt74.7%
Applied egg-rr74.7%
*-rgt-identity74.7%
Simplified74.7%
Final simplification80.3%
(FPCore (re im) :precision binary64 (if (<= re -5.8e-7) (sqrt (- re)) (sqrt (* im 0.5))))
double code(double re, double im) {
double tmp;
if (re <= -5.8e-7) {
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 <= (-5.8d-7)) 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 <= -5.8e-7) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -5.8e-7: tmp = math.sqrt(-re) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -5.8e-7) 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 <= -5.8e-7) tmp = sqrt(-re); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -5.8e-7], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -5.8 \cdot 10^{-7}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < -5.7999999999999995e-7Initial program 40.8%
add-sqr-sqrt40.5%
sqrt-unprod40.8%
*-commutative40.8%
*-commutative40.8%
swap-sqr40.8%
add-sqr-sqrt40.8%
*-commutative40.8%
hypot-define100.0%
metadata-eval100.0%
Applied egg-rr100.0%
associate-*l*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 76.4%
neg-mul-176.4%
Simplified76.4%
if -5.7999999999999995e-7 < re Initial program 44.7%
add-sqr-sqrt44.4%
sqrt-unprod44.7%
*-commutative44.7%
*-commutative44.7%
swap-sqr44.7%
add-sqr-sqrt44.7%
*-commutative44.7%
hypot-define74.0%
metadata-eval74.0%
Applied egg-rr74.0%
associate-*l*74.0%
metadata-eval74.0%
Simplified74.0%
Taylor expanded in re around 0 62.4%
Final simplification66.6%
(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(Float64(-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 43.5%
add-sqr-sqrt43.2%
sqrt-unprod43.5%
*-commutative43.5%
*-commutative43.5%
swap-sqr43.5%
add-sqr-sqrt43.5%
*-commutative43.5%
hypot-define81.7%
metadata-eval81.7%
Applied egg-rr81.7%
associate-*l*81.7%
metadata-eval81.7%
Simplified81.7%
Taylor expanded in re around -inf 28.4%
neg-mul-128.4%
Simplified28.4%
Final simplification28.4%
herbie shell --seed 2024076
(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)))))