
(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 10.4%
sub-neg10.4%
sqr-neg10.4%
sub-neg10.4%
sqr-neg10.4%
hypot-define17.1%
Simplified17.1%
add-sqr-sqrt17.1%
sqrt-unprod17.1%
*-commutative17.1%
*-commutative17.1%
swap-sqr17.1%
add-sqr-sqrt17.1%
*-commutative17.1%
metadata-eval17.1%
Applied egg-rr17.1%
associate-*l*17.1%
metadata-eval17.1%
Simplified17.1%
Taylor expanded in re around inf 92.6%
associate-*l*92.3%
unpow292.3%
rem-square-sqrt93.8%
Simplified93.8%
associate-*r*93.8%
sqrt-div93.8%
metadata-eval93.8%
un-div-inv94.1%
Applied egg-rr94.1%
if 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 47.9%
sub-neg47.9%
sqr-neg47.9%
sub-neg47.9%
sqr-neg47.9%
hypot-define92.1%
Simplified92.1%
add-sqr-sqrt91.5%
sqrt-unprod92.1%
*-commutative92.1%
*-commutative92.1%
swap-sqr92.1%
add-sqr-sqrt92.1%
*-commutative92.1%
metadata-eval92.1%
Applied egg-rr92.1%
associate-*l*92.1%
metadata-eval92.1%
Simplified92.1%
Final simplification92.4%
(FPCore (re im) :precision binary64 (if (<= re -3.1e+53) (* 0.5 (sqrt (* re -4.0))) (if (<= re 5.5e-9) (sqrt (* 0.5 (- im re))) (* im (/ 0.5 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -3.1e+53) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 5.5e-9) {
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 <= (-3.1d+53)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 5.5d-9) 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 <= -3.1e+53) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 5.5e-9) {
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 <= -3.1e+53: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 5.5e-9: 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 <= -3.1e+53) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 5.5e-9) 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 <= -3.1e+53) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 5.5e-9) tmp = sqrt((0.5 * (im - re))); else tmp = im * (0.5 / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.1e+53], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5.5e-9], 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 -3.1 \cdot 10^{+53}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 5.5 \cdot 10^{-9}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -3.10000000000000019e53Initial program 31.4%
Taylor expanded in re around -inf 78.9%
*-commutative78.9%
Simplified78.9%
if -3.10000000000000019e53 < re < 5.4999999999999996e-9Initial program 60.3%
sub-neg60.3%
sqr-neg60.3%
sub-neg60.3%
sqr-neg60.3%
hypot-define93.1%
Simplified93.1%
add-sqr-sqrt92.4%
sqrt-unprod93.1%
*-commutative93.1%
*-commutative93.1%
swap-sqr93.1%
add-sqr-sqrt93.1%
*-commutative93.1%
metadata-eval93.1%
Applied egg-rr93.1%
associate-*l*93.1%
metadata-eval93.1%
Simplified93.1%
Taylor expanded in re around 0 83.3%
neg-mul-183.3%
unsub-neg83.3%
Simplified83.3%
if 5.4999999999999996e-9 < re Initial program 15.2%
sub-neg15.2%
sqr-neg15.2%
sub-neg15.2%
sqr-neg15.2%
hypot-define41.7%
Simplified41.7%
add-sqr-sqrt41.6%
sqrt-unprod41.7%
*-commutative41.7%
*-commutative41.7%
swap-sqr41.7%
add-sqr-sqrt41.7%
*-commutative41.7%
metadata-eval41.7%
Applied egg-rr41.7%
associate-*l*41.7%
metadata-eval41.7%
Simplified41.7%
Taylor expanded in re around inf 76.1%
associate-*l*76.0%
unpow276.0%
rem-square-sqrt76.8%
Simplified76.8%
associate-*r*76.8%
sqrt-div76.8%
metadata-eval76.8%
un-div-inv76.9%
Applied egg-rr76.9%
associate-*r/76.8%
Simplified76.8%
Final simplification80.8%
(FPCore (re im) :precision binary64 (if (<= re -3.6e+53) (* 0.5 (sqrt (* re -4.0))) (if (<= re 5.8e-9) (sqrt (* 0.5 (- im re))) (/ (* im 0.5) (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= -3.6e+53) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 5.8e-9) {
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 <= (-3.6d+53)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 5.8d-9) 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 <= -3.6e+53) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 5.8e-9) {
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 <= -3.6e+53: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 5.8e-9: 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 <= -3.6e+53) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 5.8e-9) tmp = sqrt(Float64(0.5 * 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 <= -3.6e+53) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 5.8e-9) tmp = sqrt((0.5 * (im - re))); else tmp = (im * 0.5) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.6e+53], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5.8e-9], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.6 \cdot 10^{+53}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 5.8 \cdot 10^{-9}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -3.6e53Initial program 31.4%
Taylor expanded in re around -inf 78.9%
*-commutative78.9%
Simplified78.9%
if -3.6e53 < re < 5.79999999999999982e-9Initial program 60.3%
sub-neg60.3%
sqr-neg60.3%
sub-neg60.3%
sqr-neg60.3%
hypot-define93.1%
Simplified93.1%
add-sqr-sqrt92.4%
sqrt-unprod93.1%
*-commutative93.1%
*-commutative93.1%
swap-sqr93.1%
add-sqr-sqrt93.1%
*-commutative93.1%
metadata-eval93.1%
Applied egg-rr93.1%
associate-*l*93.1%
metadata-eval93.1%
Simplified93.1%
Taylor expanded in re around 0 83.3%
neg-mul-183.3%
unsub-neg83.3%
Simplified83.3%
if 5.79999999999999982e-9 < re Initial program 15.2%
sub-neg15.2%
sqr-neg15.2%
sub-neg15.2%
sqr-neg15.2%
hypot-define41.7%
Simplified41.7%
add-sqr-sqrt41.6%
sqrt-unprod41.7%
*-commutative41.7%
*-commutative41.7%
swap-sqr41.7%
add-sqr-sqrt41.7%
*-commutative41.7%
metadata-eval41.7%
Applied egg-rr41.7%
associate-*l*41.7%
metadata-eval41.7%
Simplified41.7%
Taylor expanded in re around inf 76.1%
associate-*l*76.0%
unpow276.0%
rem-square-sqrt76.8%
Simplified76.8%
associate-*r*76.8%
sqrt-div76.8%
metadata-eval76.8%
un-div-inv76.9%
Applied egg-rr76.9%
Final simplification80.8%
(FPCore (re im) :precision binary64 (if (<= re -3.2e+54) (* 0.5 (sqrt (* re -4.0))) (sqrt (* im 0.5))))
double code(double re, double im) {
double tmp;
if (re <= -3.2e+54) {
tmp = 0.5 * sqrt((re * -4.0));
} 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 <= (-3.2d+54)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else
tmp = sqrt((im * 0.5d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3.2e+54) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.2e+54: tmp = 0.5 * math.sqrt((re * -4.0)) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.2e+54) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); else tmp = sqrt(Float64(im * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.2e+54) tmp = 0.5 * sqrt((re * -4.0)); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.2e+54], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.2 \cdot 10^{+54}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < -3.2e54Initial program 31.4%
Taylor expanded in re around -inf 78.9%
*-commutative78.9%
Simplified78.9%
if -3.2e54 < re Initial program 47.0%
sub-neg47.0%
sqr-neg47.0%
sub-neg47.0%
sqr-neg47.0%
hypot-define77.9%
Simplified77.9%
add-sqr-sqrt77.3%
sqrt-unprod77.9%
*-commutative77.9%
*-commutative77.9%
swap-sqr77.9%
add-sqr-sqrt77.9%
*-commutative77.9%
metadata-eval77.9%
Applied egg-rr77.9%
associate-*l*77.9%
metadata-eval77.9%
Simplified77.9%
Taylor expanded in re around 0 65.9%
Final simplification69.0%
(FPCore (re im) :precision binary64 (sqrt (* 0.5 (- im re))))
double code(double re, double im) {
return sqrt((0.5 * (im - re)));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt((0.5d0 * (im - re)))
end function
public static double code(double re, double im) {
return Math.sqrt((0.5 * (im - re)));
}
def code(re, im): return math.sqrt((0.5 * (im - re)))
function code(re, im) return sqrt(Float64(0.5 * Float64(im - re))) end
function tmp = code(re, im) tmp = sqrt((0.5 * (im - re))); end
code[re_, im_] := N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot \left(im - re\right)}
\end{array}
Initial program 43.3%
sub-neg43.3%
sqr-neg43.3%
sub-neg43.3%
sqr-neg43.3%
hypot-define83.1%
Simplified83.1%
add-sqr-sqrt82.5%
sqrt-unprod83.1%
*-commutative83.1%
*-commutative83.1%
swap-sqr83.1%
add-sqr-sqrt83.1%
*-commutative83.1%
metadata-eval83.1%
Applied egg-rr83.1%
associate-*l*83.1%
metadata-eval83.1%
Simplified83.1%
Taylor expanded in re around 0 58.7%
neg-mul-158.7%
unsub-neg58.7%
Simplified58.7%
Final simplification58.7%
(FPCore (re im) :precision binary64 (sqrt (* im 0.5)))
double code(double re, double im) {
return sqrt((im * 0.5));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt((im * 0.5d0))
end function
public static double code(double re, double im) {
return Math.sqrt((im * 0.5));
}
def code(re, im): return math.sqrt((im * 0.5))
function code(re, im) return sqrt(Float64(im * 0.5)) end
function tmp = code(re, im) tmp = sqrt((im * 0.5)); end
code[re_, im_] := N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{im \cdot 0.5}
\end{array}
Initial program 43.3%
sub-neg43.3%
sqr-neg43.3%
sub-neg43.3%
sqr-neg43.3%
hypot-define83.1%
Simplified83.1%
add-sqr-sqrt82.5%
sqrt-unprod83.1%
*-commutative83.1%
*-commutative83.1%
swap-sqr83.1%
add-sqr-sqrt83.1%
*-commutative83.1%
metadata-eval83.1%
Applied egg-rr83.1%
associate-*l*83.1%
metadata-eval83.1%
Simplified83.1%
Taylor expanded in re around 0 56.5%
Final simplification56.5%
herbie shell --seed 2024050
(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)))))