
(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) (* 0.5 (* (* im (sqrt 0.5)) (* (sqrt 2.0) (sqrt (/ 1.0 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 = 0.5 * ((im * sqrt(0.5)) * (sqrt(2.0) * sqrt((1.0 / 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 = 0.5 * ((im * Math.sqrt(0.5)) * (Math.sqrt(2.0) * Math.sqrt((1.0 / 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 = 0.5 * ((im * math.sqrt(0.5)) * (math.sqrt(2.0) * math.sqrt((1.0 / 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(0.5 * Float64(Float64(im * sqrt(0.5)) * Float64(sqrt(2.0) * sqrt(Float64(1.0 / 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 = 0.5 * ((im * sqrt(0.5)) * (sqrt(2.0) * sqrt((1.0 / 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[(0.5 * N[(N[(im * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision] * N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[N[(1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $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:\\
\;\;\;\;0.5 \cdot \left(\left(im \cdot \sqrt{0.5}\right) \cdot \left(\sqrt{2} \cdot \sqrt{\frac{1}{re}}\right)\right)\\
\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 6.9%
Taylor expanded in im around 0 91.6%
*-commutative91.6%
associate-*l*91.7%
*-commutative91.7%
Simplified91.7%
if 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 46.5%
*-commutative46.5%
hypot-udef89.6%
*-commutative89.6%
add-sqr-sqrt88.9%
sqrt-unprod89.6%
*-commutative89.6%
*-commutative89.6%
swap-sqr89.6%
add-sqr-sqrt89.6%
metadata-eval89.6%
Applied egg-rr89.6%
*-commutative89.6%
associate-*r*89.6%
metadata-eval89.6%
Simplified89.6%
Final simplification89.9%
(FPCore (re im) :precision binary64 (if (<= (- (sqrt (+ (* re re) (* im im))) re) 0.0) (* 0.5 (* (sqrt 2.0) (* (* im (sqrt 0.5)) (sqrt (/ 1.0 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 = 0.5 * (sqrt(2.0) * ((im * sqrt(0.5)) * sqrt((1.0 / 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 = 0.5 * (Math.sqrt(2.0) * ((im * Math.sqrt(0.5)) * Math.sqrt((1.0 / 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 = 0.5 * (math.sqrt(2.0) * ((im * math.sqrt(0.5)) * math.sqrt((1.0 / 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(0.5 * Float64(sqrt(2.0) * Float64(Float64(im * sqrt(0.5)) * sqrt(Float64(1.0 / 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 = 0.5 * (sqrt(2.0) * ((im * sqrt(0.5)) * sqrt((1.0 / 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[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[(N[(im * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision] * N[Sqrt[N[(1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $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:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \left(\left(im \cdot \sqrt{0.5}\right) \cdot \sqrt{\frac{1}{re}}\right)\right)\\
\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 6.9%
Taylor expanded in im around 0 91.6%
associate-*l*91.6%
*-commutative91.6%
Simplified91.6%
if 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 46.5%
*-commutative46.5%
hypot-udef89.6%
*-commutative89.6%
add-sqr-sqrt88.9%
sqrt-unprod89.6%
*-commutative89.6%
*-commutative89.6%
swap-sqr89.6%
add-sqr-sqrt89.6%
metadata-eval89.6%
Applied egg-rr89.6%
*-commutative89.6%
associate-*r*89.6%
metadata-eval89.6%
Simplified89.6%
Final simplification89.9%
(FPCore (re im) :precision binary64 (if (<= re 8e+126) (sqrt (* 0.5 (- (hypot re im) re))) (* 0.5 (sqrt (/ (* im im) re)))))
double code(double re, double im) {
double tmp;
if (re <= 8e+126) {
tmp = sqrt((0.5 * (hypot(re, im) - re)));
} else {
tmp = 0.5 * sqrt(((im * im) / re));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 8e+126) {
tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
} else {
tmp = 0.5 * Math.sqrt(((im * im) / re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 8e+126: tmp = math.sqrt((0.5 * (math.hypot(re, im) - re))) else: tmp = 0.5 * math.sqrt(((im * im) / re)) return tmp
function code(re, im) tmp = 0.0 if (re <= 8e+126) tmp = sqrt(Float64(0.5 * Float64(hypot(re, im) - re))); else tmp = Float64(0.5 * sqrt(Float64(Float64(im * im) / re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 8e+126) tmp = sqrt((0.5 * (hypot(re, im) - re))); else tmp = 0.5 * sqrt(((im * im) / re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 8e+126], N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(0.5 * N[Sqrt[N[(N[(im * im), $MachinePrecision] / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 8 \cdot 10^{+126}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot im}{re}}\\
\end{array}
\end{array}
if re < 7.9999999999999994e126Initial program 46.5%
*-commutative46.5%
hypot-udef87.3%
*-commutative87.3%
add-sqr-sqrt86.6%
sqrt-unprod87.3%
*-commutative87.3%
*-commutative87.3%
swap-sqr87.3%
add-sqr-sqrt87.3%
metadata-eval87.3%
Applied egg-rr87.3%
*-commutative87.3%
associate-*r*87.3%
metadata-eval87.3%
Simplified87.3%
if 7.9999999999999994e126 < re Initial program 2.8%
hypot-def21.8%
Simplified21.8%
Taylor expanded in re around inf 57.0%
unpow257.0%
Simplified57.0%
Final simplification83.2%
(FPCore (re im)
:precision binary64
(if (<= re -2.7e-5)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 8e+130)
(* 0.5 (sqrt (* im 2.0)))
(* 0.5 (sqrt (/ (* im im) re))))))
double code(double re, double im) {
double tmp;
if (re <= -2.7e-5) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 8e+130) {
tmp = 0.5 * sqrt((im * 2.0));
} else {
tmp = 0.5 * sqrt(((im * im) / 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-5)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 8d+130) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else
tmp = 0.5d0 * sqrt(((im * im) / re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.7e-5) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 8e+130) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = 0.5 * Math.sqrt(((im * im) / re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.7e-5: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 8e+130: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = 0.5 * math.sqrt(((im * im) / re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.7e-5) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 8e+130) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(0.5 * sqrt(Float64(Float64(im * im) / re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.7e-5) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 8e+130) tmp = 0.5 * sqrt((im * 2.0)); else tmp = 0.5 * sqrt(((im * im) / re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.7e-5], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 8e+130], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(N[(im * im), $MachinePrecision] / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.7 \cdot 10^{-5}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 8 \cdot 10^{+130}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot im}{re}}\\
\end{array}
\end{array}
if re < -2.6999999999999999e-5Initial program 37.0%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 89.1%
*-commutative89.1%
Simplified89.1%
if -2.6999999999999999e-5 < re < 8.0000000000000005e130Initial program 50.5%
hypot-def80.9%
Simplified80.9%
Taylor expanded in re around 0 73.6%
*-commutative73.6%
Simplified73.6%
if 8.0000000000000005e130 < re Initial program 2.7%
hypot-def22.3%
Simplified22.3%
Taylor expanded in re around inf 58.6%
unpow258.6%
Simplified58.6%
Final simplification75.8%
(FPCore (re im) :precision binary64 (if (<= re -1.3e-5) (* 0.5 (sqrt (* re -4.0))) (* 0.5 (sqrt (* im 2.0)))))
double code(double re, double im) {
double tmp;
if (re <= -1.3e-5) {
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 <= (-1.3d-5)) 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 <= -1.3e-5) {
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 <= -1.3e-5: 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 <= -1.3e-5) 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 <= -1.3e-5) 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, -1.3e-5], 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 -1.3 \cdot 10^{-5}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\end{array}
\end{array}
if re < -1.29999999999999992e-5Initial program 37.0%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 89.1%
*-commutative89.1%
Simplified89.1%
if -1.29999999999999992e-5 < re Initial program 41.8%
hypot-def70.2%
Simplified70.2%
Taylor expanded in re around 0 62.1%
*-commutative62.1%
Simplified62.1%
Final simplification69.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 40.5%
hypot-def78.3%
Simplified78.3%
Taylor expanded in re around 0 49.4%
*-commutative49.4%
Simplified49.4%
Final simplification49.4%
herbie shell --seed 2023230
(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)))))