
(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 (<= (- (sqrt (+ (* re re) (* im im))) re) 0.0) (/ (* im 0.5) (sqrt re)) (sqrt (* 0.5 (- (hypot re im) re)))))
double code(double re, double im) {
double tmp;
if ((sqrt(((re * re) + (im * im))) - re) <= 0.0) {
tmp = (im * 0.5) / sqrt(re);
} else {
tmp = sqrt((0.5 * (hypot(re, im) - re)));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if ((Math.sqrt(((re * re) + (im * im))) - re) <= 0.0) {
tmp = (im * 0.5) / Math.sqrt(re);
} else {
tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
}
return tmp;
}
def code(re, im): tmp = 0 if (math.sqrt(((re * re) + (im * im))) - re) <= 0.0: tmp = (im * 0.5) / math.sqrt(re) else: tmp = math.sqrt((0.5 * (math.hypot(re, im) - re))) return tmp
function code(re, im) tmp = 0.0 if (Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re) <= 0.0) tmp = Float64(Float64(im * 0.5) / sqrt(re)); else tmp = sqrt(Float64(0.5 * Float64(hypot(re, im) - re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if ((sqrt(((re * re) + (im * im))) - re) <= 0.0) tmp = (im * 0.5) / sqrt(re); else tmp = sqrt((0.5 * (hypot(re, im) - re))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision], 0.0], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\end{array}
\end{array}
if (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0Initial program 14.4%
Taylor expanded in im around 0 96.5%
associate-*l*96.8%
*-commutative96.8%
Simplified96.8%
add-log-exp18.1%
*-un-lft-identity18.1%
log-prod18.1%
metadata-eval18.1%
add-log-exp96.8%
associate-*l*96.6%
sqrt-div96.7%
metadata-eval96.7%
un-div-inv96.8%
Applied egg-rr96.8%
+-lft-identity96.8%
associate-*r/96.8%
Simplified96.8%
*-commutative96.8%
associate-*r/96.6%
associate-*l/96.6%
*-commutative96.6%
associate-*r*96.4%
pow1/296.4%
pow1/296.4%
pow-prod-down97.6%
metadata-eval97.6%
pow-to-exp97.6%
metadata-eval97.6%
metadata-eval97.6%
1-exp97.6%
*-un-lft-identity97.6%
Applied egg-rr97.6%
if 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 49.0%
hypot-def90.6%
Simplified90.6%
add-sqr-sqrt89.8%
sqrt-unprod90.6%
*-commutative90.6%
*-commutative90.6%
swap-sqr90.6%
add-sqr-sqrt90.6%
metadata-eval90.6%
Applied egg-rr90.6%
*-commutative90.6%
associate-*r*90.6%
metadata-eval90.6%
Simplified90.6%
Final simplification91.7%
(FPCore (re im)
:precision binary64
(if (<= re -1.8e+178)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 160.0)
(* 0.5 (sqrt (* 2.0 (- im re))))
(/ (* im 0.5) (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= -1.8e+178) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 160.0) {
tmp = 0.5 * sqrt((2.0 * (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.8d+178)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 160.0d0) then
tmp = 0.5d0 * sqrt((2.0d0 * (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.8e+178) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 160.0) {
tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
} else {
tmp = (im * 0.5) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.8e+178: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 160.0: tmp = 0.5 * math.sqrt((2.0 * (im - re))) else: tmp = (im * 0.5) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.8e+178) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 160.0) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re)))); else tmp = Float64(Float64(im * 0.5) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.8e+178) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 160.0) tmp = 0.5 * sqrt((2.0 * (im - re))); else tmp = (im * 0.5) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.8e+178], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 160.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.8 \cdot 10^{+178}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 160:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -1.7999999999999999e178Initial program 4.3%
Taylor expanded in re around -inf 87.2%
*-commutative87.2%
Simplified87.2%
if -1.7999999999999999e178 < re < 160Initial program 62.5%
Taylor expanded in re around 0 76.5%
if 160 < re Initial program 14.3%
Taylor expanded in im around 0 75.9%
associate-*l*76.0%
*-commutative76.0%
Simplified76.0%
add-log-exp17.0%
*-un-lft-identity17.0%
log-prod17.0%
metadata-eval17.0%
add-log-exp76.0%
associate-*l*75.9%
sqrt-div75.9%
metadata-eval75.9%
un-div-inv75.9%
Applied egg-rr75.9%
+-lft-identity75.9%
associate-*r/76.0%
Simplified76.0%
*-commutative76.0%
associate-*r/76.0%
associate-*l/76.0%
*-commutative76.0%
associate-*r*75.7%
pow1/275.7%
pow1/275.7%
pow-prod-down76.5%
metadata-eval76.5%
pow-to-exp76.5%
metadata-eval76.5%
metadata-eval76.5%
1-exp76.5%
*-un-lft-identity76.5%
Applied egg-rr76.5%
Final simplification77.7%
(FPCore (re im) :precision binary64 (if (<= re -2.7e+146) (* 0.5 (sqrt (* re -4.0))) (if (<= re 25.5) (* 0.5 (sqrt (* im 2.0))) (* 0.5 (/ im (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -2.7e+146) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 25.5) {
tmp = 0.5 * sqrt((im * 2.0));
} 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 <= (-2.7d+146)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 25.5d0) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
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 <= -2.7e+146) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 25.5) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = 0.5 * (im / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.7e+146: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 25.5: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = 0.5 * (im / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.7e+146) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 25.5) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(0.5 * Float64(im / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.7e+146) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 25.5) tmp = 0.5 * sqrt((im * 2.0)); else tmp = 0.5 * (im / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.7e+146], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 25.5], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.7 \cdot 10^{+146}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 25.5:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -2.69999999999999989e146Initial program 4.3%
Taylor expanded in re around -inf 83.0%
*-commutative83.0%
Simplified83.0%
if -2.69999999999999989e146 < re < 25.5Initial program 64.0%
Taylor expanded in re around 0 74.6%
*-commutative74.6%
Simplified74.6%
if 25.5 < re Initial program 14.3%
Taylor expanded in re around inf 42.7%
unpow242.7%
Simplified42.7%
add-log-exp17.3%
*-un-lft-identity17.3%
log-prod17.3%
metadata-eval17.3%
add-log-exp42.7%
sqrt-div56.8%
sqrt-prod76.2%
add-sqr-sqrt76.5%
Applied egg-rr76.5%
+-lft-identity76.5%
Simplified76.5%
Final simplification76.2%
(FPCore (re im) :precision binary64 (if (<= re -2.7e+146) (* 0.5 (sqrt (* re -4.0))) (if (<= re 82.0) (* 0.5 (sqrt (* im 2.0))) (/ (* im 0.5) (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= -2.7e+146) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 82.0) {
tmp = 0.5 * sqrt((im * 2.0));
} 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 <= (-2.7d+146)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 82.0d0) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
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 <= -2.7e+146) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 82.0) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = (im * 0.5) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.7e+146: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 82.0: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = (im * 0.5) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.7e+146) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 82.0) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(Float64(im * 0.5) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.7e+146) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 82.0) tmp = 0.5 * sqrt((im * 2.0)); else tmp = (im * 0.5) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.7e+146], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 82.0], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.7 \cdot 10^{+146}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 82:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -2.69999999999999989e146Initial program 4.3%
Taylor expanded in re around -inf 83.0%
*-commutative83.0%
Simplified83.0%
if -2.69999999999999989e146 < re < 82Initial program 64.0%
Taylor expanded in re around 0 74.6%
*-commutative74.6%
Simplified74.6%
if 82 < re Initial program 14.3%
Taylor expanded in im around 0 75.9%
associate-*l*76.0%
*-commutative76.0%
Simplified76.0%
add-log-exp17.0%
*-un-lft-identity17.0%
log-prod17.0%
metadata-eval17.0%
add-log-exp76.0%
associate-*l*75.9%
sqrt-div75.9%
metadata-eval75.9%
un-div-inv75.9%
Applied egg-rr75.9%
+-lft-identity75.9%
associate-*r/76.0%
Simplified76.0%
*-commutative76.0%
associate-*r/76.0%
associate-*l/76.0%
*-commutative76.0%
associate-*r*75.7%
pow1/275.7%
pow1/275.7%
pow-prod-down76.5%
metadata-eval76.5%
pow-to-exp76.5%
metadata-eval76.5%
metadata-eval76.5%
1-exp76.5%
*-un-lft-identity76.5%
Applied egg-rr76.5%
Final simplification76.2%
(FPCore (re im) :precision binary64 (if (<= re -2.7e+146) (* 0.5 (sqrt (* re -4.0))) (* 0.5 (sqrt (* im 2.0)))))
double code(double re, double im) {
double tmp;
if (re <= -2.7e+146) {
tmp = 0.5 * sqrt((re * -4.0));
} else {
tmp = 0.5 * sqrt((im * 2.0));
}
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+146)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else
tmp = 0.5d0 * sqrt((im * 2.0d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.7e+146) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else {
tmp = 0.5 * Math.sqrt((im * 2.0));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.7e+146: tmp = 0.5 * math.sqrt((re * -4.0)) else: tmp = 0.5 * math.sqrt((im * 2.0)) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.7e+146) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); else tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.7e+146) tmp = 0.5 * sqrt((re * -4.0)); else tmp = 0.5 * sqrt((im * 2.0)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.7e+146], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.7 \cdot 10^{+146}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\end{array}
\end{array}
if re < -2.69999999999999989e146Initial program 4.3%
Taylor expanded in re around -inf 83.0%
*-commutative83.0%
Simplified83.0%
if -2.69999999999999989e146 < re Initial program 48.9%
Taylor expanded in re around 0 60.8%
*-commutative60.8%
Simplified60.8%
Final simplification63.5%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* im 2.0))))
double code(double re, double im) {
return 0.5 * sqrt((im * 2.0));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((im * 2.0d0))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((im * 2.0));
}
def code(re, im): return 0.5 * math.sqrt((im * 2.0))
function code(re, im) return Float64(0.5 * sqrt(Float64(im * 2.0))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((im * 2.0)); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{im \cdot 2}
\end{array}
Initial program 43.4%
Taylor expanded in re around 0 56.1%
*-commutative56.1%
Simplified56.1%
Final simplification56.1%
herbie shell --seed 2023207
(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)))))