
(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 6.2e-21) (sqrt (* 0.5 (- (hypot re im) re))) (* 0.5 (/ im (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= 6.2e-21) {
tmp = sqrt((0.5 * (hypot(re, im) - re)));
} else {
tmp = 0.5 * (im / sqrt(re));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 6.2e-21) {
tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
} else {
tmp = 0.5 * (im / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 6.2e-21: tmp = math.sqrt((0.5 * (math.hypot(re, im) - re))) else: tmp = 0.5 * (im / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= 6.2e-21) tmp = sqrt(Float64(0.5 * Float64(hypot(re, im) - re))); else tmp = Float64(0.5 * Float64(im / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 6.2e-21) tmp = sqrt((0.5 * (hypot(re, im) - re))); else tmp = 0.5 * (im / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 6.2e-21], N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 6.2 \cdot 10^{-21}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < 6.1999999999999997e-21Initial program 54.1%
*-commutative54.1%
hypot-udef95.4%
*-commutative95.4%
add-sqr-sqrt94.6%
sqrt-unprod95.4%
*-commutative95.4%
*-commutative95.4%
swap-sqr95.4%
add-sqr-sqrt95.4%
metadata-eval95.4%
Applied egg-rr95.4%
*-commutative95.4%
associate-*r*95.9%
metadata-eval95.9%
Simplified95.9%
if 6.1999999999999997e-21 < re Initial program 10.3%
Taylor expanded in im around 0 81.2%
*-commutative81.2%
associate-*l*81.2%
*-commutative81.2%
Simplified81.2%
expm1-log1p-u80.2%
expm1-udef36.3%
Applied egg-rr36.3%
expm1-def80.7%
expm1-log1p81.8%
associate-*r/81.8%
metadata-eval81.8%
unpow-181.8%
exp-to-pow76.7%
*-commutative76.7%
neg-mul-176.7%
unpow1/276.7%
exp-prod76.7%
distribute-lft-neg-out76.7%
exp-neg76.7%
exp-to-pow81.8%
unpow1/281.8%
associate-*r/81.9%
*-rgt-identity81.9%
Simplified81.9%
Final simplification91.9%
(FPCore (re im)
:precision binary64
(let* ((t_0 (* 0.5 (sqrt (* re -4.0))))
(t_1 (* 0.5 (sqrt (* 2.0 (- im re))))))
(if (<= re -9.2e+103)
t_0
(if (<= re -8.8e-33)
t_1
(if (<= re -1.1e-82)
t_0
(if (<= re 7.5e-22) t_1 (* 0.5 (/ im (sqrt re)))))))))
double code(double re, double im) {
double t_0 = 0.5 * sqrt((re * -4.0));
double t_1 = 0.5 * sqrt((2.0 * (im - re)));
double tmp;
if (re <= -9.2e+103) {
tmp = t_0;
} else if (re <= -8.8e-33) {
tmp = t_1;
} else if (re <= -1.1e-82) {
tmp = t_0;
} else if (re <= 7.5e-22) {
tmp = t_1;
} else {
tmp = 0.5 * (im / 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((re * (-4.0d0)))
t_1 = 0.5d0 * sqrt((2.0d0 * (im - re)))
if (re <= (-9.2d+103)) then
tmp = t_0
else if (re <= (-8.8d-33)) then
tmp = t_1
else if (re <= (-1.1d-82)) then
tmp = t_0
else if (re <= 7.5d-22) then
tmp = t_1
else
tmp = 0.5d0 * (im / sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = 0.5 * Math.sqrt((re * -4.0));
double t_1 = 0.5 * Math.sqrt((2.0 * (im - re)));
double tmp;
if (re <= -9.2e+103) {
tmp = t_0;
} else if (re <= -8.8e-33) {
tmp = t_1;
} else if (re <= -1.1e-82) {
tmp = t_0;
} else if (re <= 7.5e-22) {
tmp = t_1;
} else {
tmp = 0.5 * (im / Math.sqrt(re));
}
return tmp;
}
def code(re, im): t_0 = 0.5 * math.sqrt((re * -4.0)) t_1 = 0.5 * math.sqrt((2.0 * (im - re))) tmp = 0 if re <= -9.2e+103: tmp = t_0 elif re <= -8.8e-33: tmp = t_1 elif re <= -1.1e-82: tmp = t_0 elif re <= 7.5e-22: tmp = t_1 else: tmp = 0.5 * (im / math.sqrt(re)) return tmp
function code(re, im) t_0 = Float64(0.5 * sqrt(Float64(re * -4.0))) t_1 = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re)))) tmp = 0.0 if (re <= -9.2e+103) tmp = t_0; elseif (re <= -8.8e-33) tmp = t_1; elseif (re <= -1.1e-82) tmp = t_0; elseif (re <= 7.5e-22) tmp = t_1; else tmp = Float64(0.5 * Float64(im / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) t_0 = 0.5 * sqrt((re * -4.0)); t_1 = 0.5 * sqrt((2.0 * (im - re))); tmp = 0.0; if (re <= -9.2e+103) tmp = t_0; elseif (re <= -8.8e-33) tmp = t_1; elseif (re <= -1.1e-82) tmp = t_0; elseif (re <= 7.5e-22) tmp = t_1; else tmp = 0.5 * (im / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -9.2e+103], t$95$0, If[LessEqual[re, -8.8e-33], t$95$1, If[LessEqual[re, -1.1e-82], t$95$0, If[LessEqual[re, 7.5e-22], t$95$1, N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{re \cdot -4}\\
t_1 := 0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{if}\;re \leq -9.2 \cdot 10^{+103}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;re \leq -8.8 \cdot 10^{-33}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;re \leq -1.1 \cdot 10^{-82}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;re \leq 7.5 \cdot 10^{-22}:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -9.20000000000000034e103 or -8.80000000000000022e-33 < re < -1.09999999999999993e-82Initial program 40.4%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 87.8%
*-commutative87.8%
Simplified87.8%
if -9.20000000000000034e103 < re < -8.80000000000000022e-33 or -1.09999999999999993e-82 < re < 7.49999999999999978e-22Initial program 58.5%
Taylor expanded in re around 0 82.1%
if 7.49999999999999978e-22 < re Initial program 10.3%
Taylor expanded in im around 0 81.2%
*-commutative81.2%
associate-*l*81.2%
*-commutative81.2%
Simplified81.2%
expm1-log1p-u80.2%
expm1-udef36.3%
Applied egg-rr36.3%
expm1-def80.7%
expm1-log1p81.8%
associate-*r/81.8%
metadata-eval81.8%
unpow-181.8%
exp-to-pow76.7%
*-commutative76.7%
neg-mul-176.7%
unpow1/276.7%
exp-prod76.7%
distribute-lft-neg-out76.7%
exp-neg76.7%
exp-to-pow81.8%
unpow1/281.8%
associate-*r/81.9%
*-rgt-identity81.9%
Simplified81.9%
Final simplification83.1%
(FPCore (re im) :precision binary64 (if (<= re -2.15e-82) (* 0.5 (sqrt (* re -4.0))) (if (<= re 5e-16) (* 0.5 (sqrt (* im 2.0))) (* 0.5 (/ im (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -2.15e-82) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 5e-16) {
tmp = 0.5 * sqrt((im * 2.0));
} else {
tmp = 0.5 * (im / 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 <= (-2.15d-82)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 5d-16) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else
tmp = 0.5d0 * (im / sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.15e-82) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 5e-16) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = 0.5 * (im / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.15e-82: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 5e-16: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = 0.5 * (im / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.15e-82) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 5e-16) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(0.5 * Float64(im / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.15e-82) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 5e-16) tmp = 0.5 * sqrt((im * 2.0)); else tmp = 0.5 * (im / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.15e-82], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5e-16], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.15 \cdot 10^{-82}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 5 \cdot 10^{-16}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -2.15000000000000009e-82Initial program 55.9%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 71.1%
*-commutative71.1%
Simplified71.1%
if -2.15000000000000009e-82 < re < 5.0000000000000004e-16Initial program 52.8%
hypot-def91.4%
Simplified91.4%
Taylor expanded in re around 0 85.9%
*-commutative85.9%
Simplified85.9%
if 5.0000000000000004e-16 < re Initial program 9.1%
Taylor expanded in im around 0 82.0%
*-commutative82.0%
associate-*l*82.0%
*-commutative82.0%
Simplified82.0%
expm1-log1p-u81.0%
expm1-udef37.2%
Applied egg-rr37.2%
expm1-def81.5%
expm1-log1p82.6%
associate-*r/82.6%
metadata-eval82.6%
unpow-182.6%
exp-to-pow77.4%
*-commutative77.4%
neg-mul-177.4%
unpow1/277.4%
exp-prod77.5%
distribute-lft-neg-out77.5%
exp-neg77.4%
exp-to-pow82.6%
unpow1/282.6%
associate-*r/82.7%
*-rgt-identity82.7%
Simplified82.7%
Final simplification80.6%
(FPCore (re im) :precision binary64 (if (<= re -5e-82) (* 0.5 (sqrt (* re -4.0))) (* 0.5 (sqrt (* im 2.0)))))
double code(double re, double im) {
double tmp;
if (re <= -5e-82) {
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 <= (-5d-82)) 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 <= -5e-82) {
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 <= -5e-82: 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 <= -5e-82) 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 <= -5e-82) 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, -5e-82], 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 -5 \cdot 10^{-82}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\end{array}
\end{array}
if re < -4.9999999999999998e-82Initial program 55.9%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 71.1%
*-commutative71.1%
Simplified71.1%
if -4.9999999999999998e-82 < re Initial program 35.8%
hypot-def69.1%
Simplified69.1%
Taylor expanded in re around 0 61.4%
*-commutative61.4%
Simplified61.4%
Final simplification64.3%
(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.8%
hypot-def78.2%
Simplified78.2%
Taylor expanded in re around 0 53.5%
*-commutative53.5%
Simplified53.5%
Final simplification53.5%
herbie shell --seed 2023229
(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)))))