
(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) (/ (* im 0.5) (sqrt 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 = (im * 0.5) / sqrt(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 = (im * 0.5) / Math.sqrt(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 = (im * 0.5) / math.sqrt(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(Float64(im * 0.5) / sqrt(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 = (im * 0.5) / sqrt(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[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $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:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\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.8%
Taylor expanded in re around inf 51.7%
unpow251.7%
associate-/l*62.4%
Simplified62.4%
Applied egg-rr97.5%
if 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 48.9%
sqr-neg48.9%
sqr-neg48.9%
hypot-def92.6%
Simplified92.6%
Final simplification93.5%
(FPCore (re im)
:precision binary64
(let* ((t_0 (* 0.5 (sqrt (* 2.0 (- im re)))))
(t_1 (* 0.5 (sqrt (* 2.0 (* re -2.0))))))
(if (<= re -8.4e+126)
t_1
(if (<= re -6.5e+77)
t_0
(if (<= re -0.23)
t_1
(if (<= re 9.8e-118) t_0 (/ (* im 0.5) (sqrt re))))))))
double code(double re, double im) {
double t_0 = 0.5 * sqrt((2.0 * (im - re)));
double t_1 = 0.5 * sqrt((2.0 * (re * -2.0)));
double tmp;
if (re <= -8.4e+126) {
tmp = t_1;
} else if (re <= -6.5e+77) {
tmp = t_0;
} else if (re <= -0.23) {
tmp = t_1;
} else if (re <= 9.8e-118) {
tmp = t_0;
} 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) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = 0.5d0 * sqrt((2.0d0 * (im - re)))
t_1 = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
if (re <= (-8.4d+126)) then
tmp = t_1
else if (re <= (-6.5d+77)) then
tmp = t_0
else if (re <= (-0.23d0)) then
tmp = t_1
else if (re <= 9.8d-118) then
tmp = t_0
else
tmp = (im * 0.5d0) / sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = 0.5 * Math.sqrt((2.0 * (im - re)));
double t_1 = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
double tmp;
if (re <= -8.4e+126) {
tmp = t_1;
} else if (re <= -6.5e+77) {
tmp = t_0;
} else if (re <= -0.23) {
tmp = t_1;
} else if (re <= 9.8e-118) {
tmp = t_0;
} else {
tmp = (im * 0.5) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): t_0 = 0.5 * math.sqrt((2.0 * (im - re))) t_1 = 0.5 * math.sqrt((2.0 * (re * -2.0))) tmp = 0 if re <= -8.4e+126: tmp = t_1 elif re <= -6.5e+77: tmp = t_0 elif re <= -0.23: tmp = t_1 elif re <= 9.8e-118: tmp = t_0 else: tmp = (im * 0.5) / math.sqrt(re) return tmp
function code(re, im) t_0 = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re)))) t_1 = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))) tmp = 0.0 if (re <= -8.4e+126) tmp = t_1; elseif (re <= -6.5e+77) tmp = t_0; elseif (re <= -0.23) tmp = t_1; elseif (re <= 9.8e-118) tmp = t_0; else tmp = Float64(Float64(im * 0.5) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) t_0 = 0.5 * sqrt((2.0 * (im - re))); t_1 = 0.5 * sqrt((2.0 * (re * -2.0))); tmp = 0.0; if (re <= -8.4e+126) tmp = t_1; elseif (re <= -6.5e+77) tmp = t_0; elseif (re <= -0.23) tmp = t_1; elseif (re <= 9.8e-118) tmp = t_0; else tmp = (im * 0.5) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -8.4e+126], t$95$1, If[LessEqual[re, -6.5e+77], t$95$0, If[LessEqual[re, -0.23], t$95$1, If[LessEqual[re, 9.8e-118], t$95$0, N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
t_1 := 0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{if}\;re \leq -8.4 \cdot 10^{+126}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;re \leq -6.5 \cdot 10^{+77}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;re \leq -0.23:\\
\;\;\;\;t_1\\
\mathbf{elif}\;re \leq 9.8 \cdot 10^{-118}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -8.3999999999999997e126 or -6.5e77 < re < -0.23000000000000001Initial program 48.8%
Taylor expanded in re around -inf 89.3%
*-commutative89.3%
Simplified89.3%
if -8.3999999999999997e126 < re < -6.5e77 or -0.23000000000000001 < re < 9.7999999999999995e-118Initial program 53.1%
Taylor expanded in re around 0 82.7%
if 9.7999999999999995e-118 < re Initial program 18.2%
Taylor expanded in re around inf 47.8%
unpow247.8%
associate-/l*52.9%
Simplified52.9%
Applied egg-rr80.3%
Final simplification83.5%
(FPCore (re im)
:precision binary64
(let* ((t_0 (* 0.5 (sqrt (* im 2.0))))
(t_1 (* 0.5 (sqrt (* 2.0 (* re -2.0))))))
(if (<= re -8.4e+126)
t_1
(if (<= re -8.4e+77)
t_0
(if (<= re -70.0)
t_1
(if (<= re 9.8e-118) t_0 (/ (* im 0.5) (sqrt re))))))))
double code(double re, double im) {
double t_0 = 0.5 * sqrt((im * 2.0));
double t_1 = 0.5 * sqrt((2.0 * (re * -2.0)));
double tmp;
if (re <= -8.4e+126) {
tmp = t_1;
} else if (re <= -8.4e+77) {
tmp = t_0;
} else if (re <= -70.0) {
tmp = t_1;
} else if (re <= 9.8e-118) {
tmp = t_0;
} 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) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = 0.5d0 * sqrt((im * 2.0d0))
t_1 = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
if (re <= (-8.4d+126)) then
tmp = t_1
else if (re <= (-8.4d+77)) then
tmp = t_0
else if (re <= (-70.0d0)) then
tmp = t_1
else if (re <= 9.8d-118) then
tmp = t_0
else
tmp = (im * 0.5d0) / sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = 0.5 * Math.sqrt((im * 2.0));
double t_1 = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
double tmp;
if (re <= -8.4e+126) {
tmp = t_1;
} else if (re <= -8.4e+77) {
tmp = t_0;
} else if (re <= -70.0) {
tmp = t_1;
} else if (re <= 9.8e-118) {
tmp = t_0;
} else {
tmp = (im * 0.5) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): t_0 = 0.5 * math.sqrt((im * 2.0)) t_1 = 0.5 * math.sqrt((2.0 * (re * -2.0))) tmp = 0 if re <= -8.4e+126: tmp = t_1 elif re <= -8.4e+77: tmp = t_0 elif re <= -70.0: tmp = t_1 elif re <= 9.8e-118: tmp = t_0 else: tmp = (im * 0.5) / math.sqrt(re) return tmp
function code(re, im) t_0 = Float64(0.5 * sqrt(Float64(im * 2.0))) t_1 = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))) tmp = 0.0 if (re <= -8.4e+126) tmp = t_1; elseif (re <= -8.4e+77) tmp = t_0; elseif (re <= -70.0) tmp = t_1; elseif (re <= 9.8e-118) tmp = t_0; else tmp = Float64(Float64(im * 0.5) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) t_0 = 0.5 * sqrt((im * 2.0)); t_1 = 0.5 * sqrt((2.0 * (re * -2.0))); tmp = 0.0; if (re <= -8.4e+126) tmp = t_1; elseif (re <= -8.4e+77) tmp = t_0; elseif (re <= -70.0) tmp = t_1; elseif (re <= 9.8e-118) tmp = t_0; else tmp = (im * 0.5) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -8.4e+126], t$95$1, If[LessEqual[re, -8.4e+77], t$95$0, If[LessEqual[re, -70.0], t$95$1, If[LessEqual[re, 9.8e-118], t$95$0, N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{im \cdot 2}\\
t_1 := 0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{if}\;re \leq -8.4 \cdot 10^{+126}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;re \leq -8.4 \cdot 10^{+77}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;re \leq -70:\\
\;\;\;\;t_1\\
\mathbf{elif}\;re \leq 9.8 \cdot 10^{-118}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -8.3999999999999997e126 or -8.3999999999999995e77 < re < -70Initial program 48.8%
Taylor expanded in re around -inf 89.3%
*-commutative89.3%
Simplified89.3%
if -8.3999999999999997e126 < re < -8.3999999999999995e77 or -70 < re < 9.7999999999999995e-118Initial program 53.1%
Taylor expanded in re around 0 80.5%
expm1-log1p-u76.1%
expm1-udef60.4%
sqrt-unprod60.5%
Applied egg-rr60.5%
expm1-def76.2%
expm1-log1p81.1%
Simplified81.1%
if 9.7999999999999995e-118 < re Initial program 18.2%
Taylor expanded in re around inf 47.8%
unpow247.8%
associate-/l*52.9%
Simplified52.9%
Applied egg-rr80.3%
Final simplification82.7%
(FPCore (re im) :precision binary64 (if (<= re 9.8e-118) (* 0.5 (sqrt (* im 2.0))) (/ (* im 0.5) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= 9.8e-118) {
tmp = 0.5 * sqrt((im * 2.0));
} 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 <= 9.8d-118) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
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 <= 9.8e-118) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = (im * 0.5) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 9.8e-118: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = (im * 0.5) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 9.8e-118) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(Float64(im * 0.5) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 9.8e-118) tmp = 0.5 * sqrt((im * 2.0)); else tmp = (im * 0.5) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 9.8e-118], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 9.8 \cdot 10^{-118}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < 9.7999999999999995e-118Initial program 51.7%
Taylor expanded in re around 0 59.4%
expm1-log1p-u56.2%
expm1-udef45.1%
sqrt-unprod45.1%
Applied egg-rr45.1%
expm1-def56.3%
expm1-log1p59.8%
Simplified59.8%
if 9.7999999999999995e-118 < re Initial program 18.2%
Taylor expanded in re around inf 47.8%
unpow247.8%
associate-/l*52.9%
Simplified52.9%
Applied egg-rr80.3%
Final simplification65.8%
(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.9%
Taylor expanded in re around 0 49.0%
expm1-log1p-u46.4%
expm1-udef41.0%
sqrt-unprod41.0%
Applied egg-rr41.0%
expm1-def46.5%
expm1-log1p49.3%
Simplified49.3%
Final simplification49.3%
herbie shell --seed 2023275
(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)))))