
(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 4 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 55000000000000.0) (sqrt (* (- (hypot re im) re) 0.5)) (/ (* im 0.5) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= 55000000000000.0) {
tmp = sqrt(((hypot(re, im) - re) * 0.5));
} else {
tmp = (im * 0.5) / sqrt(re);
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 55000000000000.0) {
tmp = Math.sqrt(((Math.hypot(re, im) - re) * 0.5));
} else {
tmp = (im * 0.5) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 55000000000000.0: tmp = math.sqrt(((math.hypot(re, im) - re) * 0.5)) else: tmp = (im * 0.5) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 55000000000000.0) tmp = sqrt(Float64(Float64(hypot(re, im) - re) * 0.5)); else tmp = Float64(Float64(im * 0.5) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 55000000000000.0) tmp = sqrt(((hypot(re, im) - re) * 0.5)); else tmp = (im * 0.5) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 55000000000000.0], N[Sqrt[N[(N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision] * 0.5), $MachinePrecision]], $MachinePrecision], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 55000000000000:\\
\;\;\;\;\sqrt{\left(\mathsf{hypot}\left(re, im\right) - re\right) \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < 5.5e13Initial program 42.9%
add-sqr-sqrt42.6%
sqrt-unprod42.9%
*-commutative42.9%
*-commutative42.9%
swap-sqr42.9%
add-sqr-sqrt42.9%
*-commutative42.9%
hypot-define90.4%
metadata-eval90.4%
Applied egg-rr90.4%
associate-*l*90.4%
metadata-eval90.4%
Simplified90.4%
if 5.5e13 < re Initial program 12.6%
Taylor expanded in im around 0 80.9%
associate-*l*81.0%
*-commutative81.0%
associate-*l*81.3%
Simplified81.3%
associate-*r*81.0%
sqrt-unprod81.7%
metadata-eval81.7%
metadata-eval81.7%
*-un-lft-identity81.7%
sqrt-div81.7%
metadata-eval81.7%
Applied egg-rr81.7%
*-commutative81.7%
un-div-inv81.9%
associate-*l/81.9%
Applied egg-rr81.9%
Final simplification88.3%
(FPCore (re im)
:precision binary64
(if (<= re -15500000000000.0)
(sqrt (- re))
(if (<= re 35000000000000.0)
(sqrt (* 0.5 (- im re)))
(/ (* im 0.5) (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= -15500000000000.0) {
tmp = sqrt(-re);
} else if (re <= 35000000000000.0) {
tmp = sqrt((0.5 * (im - re)));
} 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 <= (-15500000000000.0d0)) then
tmp = sqrt(-re)
else if (re <= 35000000000000.0d0) then
tmp = sqrt((0.5d0 * (im - re)))
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 <= -15500000000000.0) {
tmp = Math.sqrt(-re);
} else if (re <= 35000000000000.0) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = (im * 0.5) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -15500000000000.0: tmp = math.sqrt(-re) elif re <= 35000000000000.0: tmp = math.sqrt((0.5 * (im - re))) else: tmp = (im * 0.5) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -15500000000000.0) tmp = sqrt(Float64(-re)); elseif (re <= 35000000000000.0) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64(Float64(im * 0.5) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -15500000000000.0) tmp = sqrt(-re); elseif (re <= 35000000000000.0) tmp = sqrt((0.5 * (im - re))); else tmp = (im * 0.5) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -15500000000000.0], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 35000000000000.0], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -15500000000000:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 35000000000000:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -1.55e13Initial program 23.4%
add-sqr-sqrt23.2%
sqrt-unprod23.4%
*-commutative23.4%
*-commutative23.4%
swap-sqr23.4%
add-sqr-sqrt23.4%
*-commutative23.4%
hypot-define99.9%
metadata-eval99.9%
Applied egg-rr99.9%
associate-*l*99.9%
metadata-eval99.9%
Simplified99.9%
Taylor expanded in re around -inf 77.2%
neg-mul-177.2%
Simplified77.2%
if -1.55e13 < re < 3.5e13Initial program 51.8%
add-sqr-sqrt51.5%
sqrt-unprod51.8%
*-commutative51.8%
*-commutative51.8%
swap-sqr51.8%
add-sqr-sqrt51.8%
*-commutative51.8%
hypot-define86.1%
metadata-eval86.1%
Applied egg-rr86.1%
associate-*l*86.1%
metadata-eval86.1%
Simplified86.1%
Taylor expanded in re around 0 74.3%
neg-mul-174.3%
unsub-neg74.3%
Simplified74.3%
if 3.5e13 < re Initial program 12.6%
Taylor expanded in im around 0 80.9%
associate-*l*81.0%
*-commutative81.0%
associate-*l*81.3%
Simplified81.3%
associate-*r*81.0%
sqrt-unprod81.7%
metadata-eval81.7%
metadata-eval81.7%
*-un-lft-identity81.7%
sqrt-div81.7%
metadata-eval81.7%
Applied egg-rr81.7%
*-commutative81.7%
un-div-inv81.9%
associate-*l/81.9%
Applied egg-rr81.9%
Final simplification76.9%
(FPCore (re im) :precision binary64 (if (<= re -0.085) (sqrt (- re)) (sqrt (* im 0.5))))
double code(double re, double im) {
double tmp;
if (re <= -0.085) {
tmp = sqrt(-re);
} else {
tmp = sqrt((im * 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 <= (-0.085d0)) then
tmp = sqrt(-re)
else
tmp = sqrt((im * 0.5d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -0.085) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -0.085: tmp = math.sqrt(-re) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -0.085) tmp = sqrt(Float64(-re)); else tmp = sqrt(Float64(im * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -0.085) tmp = sqrt(-re); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -0.085], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -0.085:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < -0.0850000000000000061Initial program 28.2%
add-sqr-sqrt28.0%
sqrt-unprod28.2%
*-commutative28.2%
*-commutative28.2%
swap-sqr28.2%
add-sqr-sqrt28.2%
*-commutative28.2%
hypot-define100.0%
metadata-eval100.0%
Applied egg-rr100.0%
associate-*l*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 75.7%
neg-mul-175.7%
Simplified75.7%
if -0.0850000000000000061 < re Initial program 37.6%
add-sqr-sqrt37.3%
sqrt-unprod37.6%
*-commutative37.6%
*-commutative37.6%
swap-sqr37.6%
add-sqr-sqrt37.6%
*-commutative37.6%
hypot-define69.7%
metadata-eval69.7%
Applied egg-rr69.7%
associate-*l*69.7%
metadata-eval69.7%
Simplified69.7%
Taylor expanded in re around 0 56.8%
Final simplification61.5%
(FPCore (re im) :precision binary64 (sqrt (- re)))
double code(double re, double im) {
return sqrt(-re);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt(-re)
end function
public static double code(double re, double im) {
return Math.sqrt(-re);
}
def code(re, im): return math.sqrt(-re)
function code(re, im) return sqrt(Float64(-re)) end
function tmp = code(re, im) tmp = sqrt(-re); end
code[re_, im_] := N[Sqrt[(-re)], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{-re}
\end{array}
Initial program 35.2%
add-sqr-sqrt35.0%
sqrt-unprod35.2%
*-commutative35.2%
*-commutative35.2%
swap-sqr35.2%
add-sqr-sqrt35.2%
*-commutative35.2%
hypot-define77.3%
metadata-eval77.3%
Applied egg-rr77.3%
associate-*l*77.3%
metadata-eval77.3%
Simplified77.3%
Taylor expanded in re around -inf 25.3%
neg-mul-125.3%
Simplified25.3%
Final simplification25.3%
herbie shell --seed 2024036
(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)))))