
(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 (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))) 0.0) (* 0.5 (pow (* (pow (/ 1.0 re) 0.25) (pow im 0.5)) 2.0)) (sqrt (* 0.5 (- (hypot re im) re)))))
double code(double re, double im) {
double tmp;
if (sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0) {
tmp = 0.5 * pow((pow((1.0 / re), 0.25) * pow(im, 0.5)), 2.0);
} else {
tmp = sqrt((0.5 * (hypot(re, im) - re)));
}
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 = 0.5 * Math.pow((Math.pow((1.0 / re), 0.25) * Math.pow(im, 0.5)), 2.0);
} else {
tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
}
return tmp;
}
def code(re, im): tmp = 0 if math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re))) <= 0.0: tmp = 0.5 * math.pow((math.pow((1.0 / re), 0.25) * math.pow(im, 0.5)), 2.0) else: tmp = math.sqrt((0.5 * (math.hypot(re, im) - re))) 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(0.5 * (Float64((Float64(1.0 / re) ^ 0.25) * (im ^ 0.5)) ^ 2.0)); 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((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0) tmp = 0.5 * ((((1.0 / re) ^ 0.25) * (im ^ 0.5)) ^ 2.0); else tmp = sqrt((0.5 * (hypot(re, im) - re))); 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[(0.5 * N[Power[N[(N[Power[N[(1.0 / re), $MachinePrecision], 0.25], $MachinePrecision] * N[Power[im, 0.5], $MachinePrecision]), $MachinePrecision], 2.0], $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{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \leq 0:\\
\;\;\;\;0.5 \cdot {\left({\left(\frac{1}{re}\right)}^{0.25} \cdot {im}^{0.5}\right)}^{2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\end{array}
\end{array}
if (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 14.4%
hypot-udef14.4%
add-sqr-sqrt14.4%
pow214.4%
pow1/214.4%
sqrt-pow114.4%
metadata-eval14.4%
Applied egg-rr14.4%
Taylor expanded in re around inf 47.9%
log-pow91.8%
log-pow47.9%
distribute-rgt-in47.9%
exp-sum47.9%
exp-to-pow48.2%
log-pow91.6%
*-commutative91.6%
associate-*l*91.6%
metadata-eval91.6%
exp-to-pow99.1%
Simplified99.1%
if 0.0 < (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 42.3%
sub-neg42.3%
sqr-neg42.3%
sub-neg42.3%
sqr-neg42.3%
hypot-def87.8%
Simplified87.8%
add-sqr-sqrt87.2%
sqrt-unprod87.8%
*-commutative87.8%
*-commutative87.8%
swap-sqr87.8%
add-sqr-sqrt87.8%
metadata-eval87.8%
Applied egg-rr87.8%
*-commutative87.8%
associate-*r*87.8%
metadata-eval87.8%
Simplified87.8%
Final simplification89.4%
(FPCore (re im) :precision binary64 (if (<= (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))) 0.0) (* 0.5 (sqrt (* 2.0 (* 0.5 (/ (pow im 2.0) re))))) (sqrt (* 0.5 (- (hypot re im) re)))))
double code(double re, double im) {
double tmp;
if (sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0) {
tmp = 0.5 * sqrt((2.0 * (0.5 * (pow(im, 2.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((2.0 * (Math.sqrt(((re * re) + (im * im))) - re))) <= 0.0) {
tmp = 0.5 * Math.sqrt((2.0 * (0.5 * (Math.pow(im, 2.0) / re))));
} else {
tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
}
return tmp;
}
def code(re, im): tmp = 0 if math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re))) <= 0.0: tmp = 0.5 * math.sqrt((2.0 * (0.5 * (math.pow(im, 2.0) / re)))) else: tmp = math.sqrt((0.5 * (math.hypot(re, im) - re))) 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(0.5 * sqrt(Float64(2.0 * Float64(0.5 * Float64((im ^ 2.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((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0) tmp = 0.5 * sqrt((2.0 * (0.5 * ((im ^ 2.0) / re)))); else tmp = sqrt((0.5 * (hypot(re, im) - re))); 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[(0.5 * N[Sqrt[N[(2.0 * N[(0.5 * N[(N[Power[im, 2.0], $MachinePrecision] / 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{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \frac{{im}^{2}}{re}\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\end{array}
\end{array}
if (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 14.4%
Taylor expanded in re around inf 50.6%
if 0.0 < (sqrt.f64 (*.f64 2 (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 42.3%
sub-neg42.3%
sqr-neg42.3%
sub-neg42.3%
sqr-neg42.3%
hypot-def87.8%
Simplified87.8%
add-sqr-sqrt87.2%
sqrt-unprod87.8%
*-commutative87.8%
*-commutative87.8%
swap-sqr87.8%
add-sqr-sqrt87.8%
metadata-eval87.8%
Applied egg-rr87.8%
*-commutative87.8%
associate-*r*87.8%
metadata-eval87.8%
Simplified87.8%
Final simplification82.6%
(FPCore (re im) :precision binary64 (sqrt (* 0.5 (- (hypot re im) re))))
double code(double re, double im) {
return sqrt((0.5 * (hypot(re, im) - re)));
}
public static double code(double re, double im) {
return Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
}
def code(re, im): return math.sqrt((0.5 * (math.hypot(re, im) - re)))
function code(re, im) return sqrt(Float64(0.5 * Float64(hypot(re, im) - re))) end
function tmp = code(re, im) tmp = sqrt((0.5 * (hypot(re, im) - re))); end
code[re_, im_] := N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}
\end{array}
Initial program 38.4%
sub-neg38.4%
sqr-neg38.4%
sub-neg38.4%
sqr-neg38.4%
hypot-def77.5%
Simplified77.5%
add-sqr-sqrt77.0%
sqrt-unprod77.5%
*-commutative77.5%
*-commutative77.5%
swap-sqr77.5%
add-sqr-sqrt77.5%
metadata-eval77.5%
Applied egg-rr77.5%
*-commutative77.5%
associate-*r*77.5%
metadata-eval77.5%
Simplified77.5%
Final simplification77.5%
(FPCore (re im)
:precision binary64
(if (<= re -800.0)
(* 0.5 (sqrt (* 2.0 (* re -2.0))))
(if (<= re 1.12e+237)
(* 0.5 (sqrt (* 2.0 im)))
(* 0.5 (sqrt (* 2.0 (- re re)))))))
double code(double re, double im) {
double tmp;
if (re <= -800.0) {
tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
} else if (re <= 1.12e+237) {
tmp = 0.5 * sqrt((2.0 * im));
} else {
tmp = 0.5 * sqrt((2.0 * (re - re)));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-800.0d0)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
else if (re <= 1.12d+237) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else
tmp = 0.5d0 * sqrt((2.0d0 * (re - re)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -800.0) {
tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
} else if (re <= 1.12e+237) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re - re)));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -800.0: tmp = 0.5 * math.sqrt((2.0 * (re * -2.0))) elif re <= 1.12e+237: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = 0.5 * math.sqrt((2.0 * (re - re))) return tmp
function code(re, im) tmp = 0.0 if (re <= -800.0) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))); elseif (re <= 1.12e+237) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - re)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -800.0) tmp = 0.5 * sqrt((2.0 * (re * -2.0))); elseif (re <= 1.12e+237) tmp = 0.5 * sqrt((2.0 * im)); else tmp = 0.5 * sqrt((2.0 * (re - re))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -800.0], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.12e+237], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -800:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{elif}\;re \leq 1.12 \cdot 10^{+237}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - re\right)}\\
\end{array}
\end{array}
if re < -800Initial program 38.1%
Taylor expanded in re around -inf 84.5%
*-commutative84.5%
Simplified84.5%
if -800 < re < 1.11999999999999997e237Initial program 40.7%
hypot-udef71.7%
add-sqr-sqrt71.3%
pow271.3%
pow1/271.3%
sqrt-pow171.2%
metadata-eval71.2%
Applied egg-rr71.2%
Taylor expanded in im around inf 57.2%
unpow257.2%
prod-exp57.2%
+-commutative57.2%
mul-1-neg57.2%
log-rec57.2%
remove-double-neg57.2%
+-commutative57.2%
log-prod57.3%
log-pow57.3%
metadata-eval57.3%
+-commutative57.3%
mul-1-neg57.3%
log-rec57.3%
remove-double-neg57.3%
Simplified62.0%
if 1.11999999999999997e237 < re Initial program 2.0%
Taylor expanded in re around inf 48.0%
Final simplification67.1%
(FPCore (re im) :precision binary64 (if (<= re -0.075) (* 0.5 (sqrt (* 2.0 (* re -2.0)))) (* 0.5 (sqrt (* 2.0 im)))))
double code(double re, double im) {
double tmp;
if (re <= -0.075) {
tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
} else {
tmp = 0.5 * sqrt((2.0 * im));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-0.075d0)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
else
tmp = 0.5d0 * sqrt((2.0d0 * im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -0.075) {
tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
} else {
tmp = 0.5 * Math.sqrt((2.0 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -0.075: tmp = 0.5 * math.sqrt((2.0 * (re * -2.0))) else: tmp = 0.5 * math.sqrt((2.0 * im)) return tmp
function code(re, im) tmp = 0.0 if (re <= -0.075) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -0.075) tmp = 0.5 * sqrt((2.0 * (re * -2.0))); else tmp = 0.5 * sqrt((2.0 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -0.075], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -0.075:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\end{array}
\end{array}
if re < -0.0749999999999999972Initial program 38.1%
Taylor expanded in re around -inf 84.5%
*-commutative84.5%
Simplified84.5%
if -0.0749999999999999972 < re Initial program 38.4%
hypot-udef70.3%
add-sqr-sqrt69.9%
pow269.9%
pow1/269.9%
sqrt-pow169.9%
metadata-eval69.9%
Applied egg-rr69.9%
Taylor expanded in im around inf 54.1%
unpow254.1%
prod-exp54.2%
+-commutative54.2%
mul-1-neg54.2%
log-rec54.2%
remove-double-neg54.2%
+-commutative54.2%
log-prod54.2%
log-pow54.2%
metadata-eval54.2%
+-commutative54.2%
mul-1-neg54.2%
log-rec54.2%
remove-double-neg54.2%
Simplified58.7%
Final simplification65.3%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 im))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * im))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * im));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * im))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * im))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * im)); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Initial program 38.4%
hypot-udef77.5%
add-sqr-sqrt77.0%
pow277.0%
pow1/277.0%
sqrt-pow177.0%
metadata-eval77.0%
Applied egg-rr77.0%
Taylor expanded in im around inf 44.9%
unpow244.9%
prod-exp44.9%
+-commutative44.9%
mul-1-neg44.9%
log-rec44.9%
remove-double-neg44.9%
+-commutative44.9%
log-prod45.0%
log-pow45.0%
metadata-eval45.0%
+-commutative45.0%
mul-1-neg45.0%
log-rec45.0%
remove-double-neg45.0%
Simplified48.6%
Final simplification48.6%
herbie shell --seed 2023319
(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)))))