
(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 (<= re 1.55e+31) (* 0.5 (sqrt (* 2.0 (- (hypot re im) re)))) (* 0.5 (* im (pow re -0.5)))))
double code(double re, double im) {
double tmp;
if (re <= 1.55e+31) {
tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
} else {
tmp = 0.5 * (im * pow(re, -0.5));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 1.55e+31) {
tmp = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
} else {
tmp = 0.5 * (im * Math.pow(re, -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 1.55e+31: tmp = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re))) else: tmp = 0.5 * (im * math.pow(re, -0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= 1.55e+31) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re)))); else tmp = Float64(0.5 * Float64(im * (re ^ -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 1.55e+31) tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re))); else tmp = 0.5 * (im * (re ^ -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 1.55e+31], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 1.55 \cdot 10^{+31}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\
\end{array}
\end{array}
if re < 1.5500000000000001e31Initial program 45.2%
hypot-def92.6%
Simplified92.6%
if 1.5500000000000001e31 < re Initial program 7.2%
Taylor expanded in re around inf 57.5%
unpow257.5%
Simplified57.5%
Taylor expanded in im around 0 83.4%
*-commutative83.4%
unpow1/283.4%
sqr-pow83.0%
sqr-pow83.4%
rem-exp-log78.4%
exp-neg78.4%
exp-prod78.4%
distribute-lft-neg-out78.4%
distribute-rgt-neg-in78.4%
metadata-eval78.4%
exp-to-pow83.4%
Simplified83.4%
Final simplification90.8%
(FPCore (re im)
:precision binary64
(if (<= re -2.9e+141)
(* 0.5 (sqrt (* 2.0 (* re -2.0))))
(if (or (<= re 1.32e-30) (and (not (<= re 4.8e+14)) (<= re 1.55e+31)))
(* 0.5 (sqrt (* 2.0 (- im re))))
(* 0.5 (* im (pow re -0.5))))))
double code(double re, double im) {
double tmp;
if (re <= -2.9e+141) {
tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
} else if ((re <= 1.32e-30) || (!(re <= 4.8e+14) && (re <= 1.55e+31))) {
tmp = 0.5 * sqrt((2.0 * (im - re)));
} else {
tmp = 0.5 * (im * pow(re, -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 <= (-2.9d+141)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
else if ((re <= 1.32d-30) .or. (.not. (re <= 4.8d+14)) .and. (re <= 1.55d+31)) then
tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
else
tmp = 0.5d0 * (im * (re ** (-0.5d0)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.9e+141) {
tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
} else if ((re <= 1.32e-30) || (!(re <= 4.8e+14) && (re <= 1.55e+31))) {
tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
} else {
tmp = 0.5 * (im * Math.pow(re, -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.9e+141: tmp = 0.5 * math.sqrt((2.0 * (re * -2.0))) elif (re <= 1.32e-30) or (not (re <= 4.8e+14) and (re <= 1.55e+31)): tmp = 0.5 * math.sqrt((2.0 * (im - re))) else: tmp = 0.5 * (im * math.pow(re, -0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.9e+141) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))); elseif ((re <= 1.32e-30) || (!(re <= 4.8e+14) && (re <= 1.55e+31))) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re)))); else tmp = Float64(0.5 * Float64(im * (re ^ -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.9e+141) tmp = 0.5 * sqrt((2.0 * (re * -2.0))); elseif ((re <= 1.32e-30) || (~((re <= 4.8e+14)) && (re <= 1.55e+31))) tmp = 0.5 * sqrt((2.0 * (im - re))); else tmp = 0.5 * (im * (re ^ -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.9e+141], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 1.32e-30], And[N[Not[LessEqual[re, 4.8e+14]], $MachinePrecision], LessEqual[re, 1.55e+31]]], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.9 \cdot 10^{+141}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{elif}\;re \leq 1.32 \cdot 10^{-30} \lor \neg \left(re \leq 4.8 \cdot 10^{+14}\right) \land re \leq 1.55 \cdot 10^{+31}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\
\end{array}
\end{array}
if re < -2.90000000000000007e141Initial program 6.4%
Taylor expanded in re around -inf 95.9%
*-commutative95.9%
Simplified95.9%
if -2.90000000000000007e141 < re < 1.32e-30 or 4.8e14 < re < 1.5500000000000001e31Initial program 58.0%
Taylor expanded in re around 0 76.8%
if 1.32e-30 < re < 4.8e14 or 1.5500000000000001e31 < re Initial program 8.6%
Taylor expanded in re around inf 55.3%
unpow255.3%
Simplified55.3%
Taylor expanded in im around 0 82.4%
*-commutative82.4%
unpow1/282.4%
sqr-pow82.0%
sqr-pow82.4%
rem-exp-log77.9%
exp-neg77.9%
exp-prod77.9%
distribute-lft-neg-out77.9%
distribute-rgt-neg-in77.9%
metadata-eval77.9%
exp-to-pow82.4%
Simplified82.4%
Final simplification81.4%
(FPCore (re im) :precision binary64 (if (<= re -5e-310) (* 0.5 (sqrt (* 2.0 (* re -2.0)))) (* 0.5 (* im (pow re -0.5)))))
double code(double re, double im) {
double tmp;
if (re <= -5e-310) {
tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
} else {
tmp = 0.5 * (im * pow(re, -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 <= (-5d-310)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
else
tmp = 0.5d0 * (im * (re ** (-0.5d0)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -5e-310) {
tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
} else {
tmp = 0.5 * (im * Math.pow(re, -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -5e-310: tmp = 0.5 * math.sqrt((2.0 * (re * -2.0))) else: tmp = 0.5 * (im * math.pow(re, -0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -5e-310) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))); else tmp = Float64(0.5 * Float64(im * (re ^ -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -5e-310) tmp = 0.5 * sqrt((2.0 * (re * -2.0))); else tmp = 0.5 * (im * (re ^ -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -5e-310], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -5 \cdot 10^{-310}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\
\end{array}
\end{array}
if re < -4.999999999999985e-310Initial program 47.3%
Taylor expanded in re around -inf 55.5%
*-commutative55.5%
Simplified55.5%
if -4.999999999999985e-310 < re Initial program 26.6%
Taylor expanded in re around inf 31.4%
unpow231.4%
Simplified31.4%
Taylor expanded in im around 0 52.0%
*-commutative52.0%
unpow1/252.0%
sqr-pow51.8%
sqr-pow52.0%
rem-exp-log49.2%
exp-neg49.2%
exp-prod49.2%
distribute-lft-neg-out49.2%
distribute-rgt-neg-in49.2%
metadata-eval49.2%
exp-to-pow52.0%
Simplified52.0%
Final simplification53.9%
(FPCore (re im) :precision binary64 (* 0.5 (* im (pow re -0.5))))
double code(double re, double im) {
return 0.5 * (im * pow(re, -0.5));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * (im * (re ** (-0.5d0)))
end function
public static double code(double re, double im) {
return 0.5 * (im * Math.pow(re, -0.5));
}
def code(re, im): return 0.5 * (im * math.pow(re, -0.5))
function code(re, im) return Float64(0.5 * Float64(im * (re ^ -0.5))) end
function tmp = code(re, im) tmp = 0.5 * (im * (re ^ -0.5)); end
code[re_, im_] := N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \left(im \cdot {re}^{-0.5}\right)
\end{array}
Initial program 37.7%
Taylor expanded in re around inf 15.0%
unpow215.0%
Simplified15.0%
Taylor expanded in im around 0 24.0%
*-commutative24.0%
unpow1/224.0%
sqr-pow23.9%
sqr-pow24.0%
rem-exp-log22.7%
exp-neg22.7%
exp-prod22.7%
distribute-lft-neg-out22.7%
distribute-rgt-neg-in22.7%
metadata-eval22.7%
exp-to-pow24.0%
Simplified24.0%
Final simplification24.0%
(FPCore (re im) :precision binary64 (* 0.5 (* im (sqrt re))))
double code(double re, double im) {
return 0.5 * (im * sqrt(re));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * (im * sqrt(re))
end function
public static double code(double re, double im) {
return 0.5 * (im * Math.sqrt(re));
}
def code(re, im): return 0.5 * (im * math.sqrt(re))
function code(re, im) return Float64(0.5 * Float64(im * sqrt(re))) end
function tmp = code(re, im) tmp = 0.5 * (im * sqrt(re)); end
code[re_, im_] := N[(0.5 * N[(im * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \left(im \cdot \sqrt{re}\right)
\end{array}
Initial program 37.7%
Taylor expanded in re around inf 15.0%
unpow215.0%
Simplified15.0%
Taylor expanded in im around 0 24.0%
*-commutative24.0%
unpow1/224.0%
sqr-pow23.9%
sqr-pow24.0%
rem-exp-log22.7%
exp-neg22.7%
exp-prod22.7%
distribute-lft-neg-out22.7%
distribute-rgt-neg-in22.7%
metadata-eval22.7%
exp-to-pow24.0%
Simplified24.0%
expm1-log1p-u23.7%
expm1-udef10.5%
pow-to-exp10.5%
metadata-eval10.5%
distribute-rgt-neg-in10.5%
distribute-lft-neg-out10.5%
exp-prod10.5%
unpow1/210.5%
add-sqr-sqrt1.2%
sqrt-unprod6.3%
sqr-neg6.3%
sqrt-unprod5.1%
add-sqr-sqrt6.5%
add-exp-log6.5%
Applied egg-rr6.5%
expm1-def2.9%
expm1-log1p2.9%
Simplified2.9%
Final simplification2.9%
herbie shell --seed 2023199
(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)))))