
(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 (+ (* re re) (* im im))) re) 0.0) (* 0.5 (* im (sqrt (/ 1.0 re)))) (* 0.5 (sqrt (* 2.0 (- (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((1.0 / re)));
} else {
tmp = 0.5 * sqrt((2.0 * (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((1.0 / re)));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (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((1.0 / re))) else: tmp = 0.5 * math.sqrt((2.0 * (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(im * sqrt(Float64(1.0 / re)))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * 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((1.0 / re))); else tmp = 0.5 * sqrt((2.0 * (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[(im * N[Sqrt[N[(1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $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(im \cdot \sqrt{\frac{1}{re}}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \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 8.3%
*-commutative8.3%
hypot-udef17.2%
sqrt-prod17.1%
Applied egg-rr17.1%
*-commutative17.1%
sqrt-prod17.2%
pow1/217.2%
metadata-eval17.2%
pow-to-exp16.7%
*-commutative16.7%
metadata-eval16.7%
Applied egg-rr16.7%
Taylor expanded in im around 0 92.9%
if 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 48.9%
sub-neg48.9%
sqr-neg48.9%
sub-neg48.9%
sqr-neg48.9%
hypot-def89.5%
Simplified89.5%
Final simplification90.1%
(FPCore (re im)
:precision binary64
(if (<= re -7e+45)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 1.05e+229)
(* 0.5 (sqrt (* im 2.0)))
(* 0.5 (sqrt (* 2.0 (- re re)))))))
double code(double re, double im) {
double tmp;
if (re <= -7e+45) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 1.05e+229) {
tmp = 0.5 * sqrt((im * 2.0));
} 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 <= (-7d+45)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 1.05d+229) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
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 <= -7e+45) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 1.05e+229) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re - re)));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -7e+45: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 1.05e+229: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = 0.5 * math.sqrt((2.0 * (re - re))) return tmp
function code(re, im) tmp = 0.0 if (re <= -7e+45) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 1.05e+229) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); 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 <= -7e+45) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 1.05e+229) tmp = 0.5 * sqrt((im * 2.0)); else tmp = 0.5 * sqrt((2.0 * (re - re))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -7e+45], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.05e+229], N[(0.5 * N[Sqrt[N[(im * 2.0), $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 -7 \cdot 10^{+45}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 1.05 \cdot 10^{+229}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - re\right)}\\
\end{array}
\end{array}
if re < -7.00000000000000046e45Initial program 46.4%
sub-neg46.4%
sqr-neg46.4%
sub-neg46.4%
sqr-neg46.4%
hypot-def98.1%
Simplified98.1%
Taylor expanded in re around -inf 83.4%
*-commutative83.4%
Simplified83.4%
if -7.00000000000000046e45 < re < 1.04999999999999994e229Initial program 43.7%
sub-neg43.7%
sqr-neg43.7%
sub-neg43.7%
sqr-neg43.7%
hypot-def74.4%
Simplified74.4%
Taylor expanded in re around 0 63.4%
*-commutative63.4%
Simplified63.4%
if 1.04999999999999994e229 < re Initial program 2.4%
Taylor expanded in re around inf 32.4%
Final simplification65.2%
(FPCore (re im)
:precision binary64
(if (<= re -1.1e+46)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 6.2e-37)
(* 0.5 (sqrt (* 2.0 (- im re))))
(* 0.5 (* im (sqrt (/ 1.0 re)))))))
double code(double re, double im) {
double tmp;
if (re <= -1.1e+46) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 6.2e-37) {
tmp = 0.5 * sqrt((2.0 * (im - re)));
} else {
tmp = 0.5 * (im * sqrt((1.0 / 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.1d+46)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 6.2d-37) then
tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
else
tmp = 0.5d0 * (im * sqrt((1.0d0 / re)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.1e+46) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 6.2e-37) {
tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
} else {
tmp = 0.5 * (im * Math.sqrt((1.0 / re)));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.1e+46: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 6.2e-37: tmp = 0.5 * math.sqrt((2.0 * (im - re))) else: tmp = 0.5 * (im * math.sqrt((1.0 / re))) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.1e+46) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 6.2e-37) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re)))); else tmp = Float64(0.5 * Float64(im * sqrt(Float64(1.0 / re)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.1e+46) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 6.2e-37) tmp = 0.5 * sqrt((2.0 * (im - re))); else tmp = 0.5 * (im * sqrt((1.0 / re))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.1e+46], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 6.2e-37], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Sqrt[N[(1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.1 \cdot 10^{+46}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 6.2 \cdot 10^{-37}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot \sqrt{\frac{1}{re}}\right)\\
\end{array}
\end{array}
if re < -1.1e46Initial program 46.4%
sub-neg46.4%
sqr-neg46.4%
sub-neg46.4%
sqr-neg46.4%
hypot-def98.1%
Simplified98.1%
Taylor expanded in re around -inf 83.4%
*-commutative83.4%
Simplified83.4%
if -1.1e46 < re < 6.19999999999999987e-37Initial program 55.9%
Taylor expanded in re around 0 78.8%
if 6.19999999999999987e-37 < re Initial program 16.7%
*-commutative16.7%
hypot-udef43.2%
sqrt-prod43.1%
Applied egg-rr43.1%
*-commutative43.1%
sqrt-prod43.2%
pow1/243.2%
metadata-eval43.2%
pow-to-exp41.2%
*-commutative41.2%
metadata-eval41.2%
Applied egg-rr41.2%
Taylor expanded in im around 0 74.6%
Final simplification78.3%
(FPCore (re im) :precision binary64 (if (<= re -4.9e+45) (* 0.5 (sqrt (* re -4.0))) (* 0.5 (sqrt (* im 2.0)))))
double code(double re, double im) {
double tmp;
if (re <= -4.9e+45) {
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 <= (-4.9d+45)) 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 <= -4.9e+45) {
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 <= -4.9e+45: 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 <= -4.9e+45) 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 <= -4.9e+45) 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, -4.9e+45], 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 -4.9 \cdot 10^{+45}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\end{array}
\end{array}
if re < -4.9000000000000002e45Initial program 46.4%
sub-neg46.4%
sqr-neg46.4%
sub-neg46.4%
sqr-neg46.4%
hypot-def98.1%
Simplified98.1%
Taylor expanded in re around -inf 83.4%
*-commutative83.4%
Simplified83.4%
if -4.9000000000000002e45 < re Initial program 40.3%
sub-neg40.3%
sqr-neg40.3%
sub-neg40.3%
sqr-neg40.3%
hypot-def70.9%
Simplified70.9%
Taylor expanded in re around 0 58.6%
*-commutative58.6%
Simplified58.6%
Final simplification63.4%
(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 41.5%
sub-neg41.5%
sqr-neg41.5%
sub-neg41.5%
sqr-neg41.5%
hypot-def76.2%
Simplified76.2%
Taylor expanded in re around 0 51.1%
*-commutative51.1%
Simplified51.1%
Final simplification51.1%
herbie shell --seed 2024013
(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)))))