
(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 6 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 7.5e-23) (sqrt (* 0.5 (- (hypot re im) re))) (/ (* 0.5 im) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= 7.5e-23) {
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 <= 7.5e-23) {
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 <= 7.5e-23: 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 <= 7.5e-23) tmp = sqrt(Float64(0.5 * Float64(hypot(re, im) - re))); else tmp = Float64(Float64(0.5 * im) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 7.5e-23) 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, 7.5e-23], N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 7.5 \cdot 10^{-23}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < 7.4999999999999998e-23Initial program 52.6%
pow152.6%
Applied egg-rr92.2%
unpow192.2%
*-commutative92.2%
associate-*r*92.7%
metadata-eval92.7%
Simplified92.7%
if 7.4999999999999998e-23 < re Initial program 8.6%
pow18.6%
Applied egg-rr33.7%
unpow133.7%
*-commutative33.7%
associate-*r*33.7%
metadata-eval33.7%
Simplified33.7%
Taylor expanded in re around inf 78.6%
unpow278.6%
rem-square-sqrt79.5%
Simplified79.5%
sqrt-div79.4%
metadata-eval79.4%
un-div-inv79.6%
Applied egg-rr79.6%
Final simplification89.2%
(FPCore (re im) :precision binary64 (if (<= re -9.8e+35) (sqrt (- re)) (if (<= re 1.4e-22) (sqrt (* 0.5 (- im re))) (/ (* 0.5 im) (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= -9.8e+35) {
tmp = sqrt(-re);
} else if (re <= 1.4e-22) {
tmp = sqrt((0.5 * (im - re)));
} 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 <= (-9.8d+35)) then
tmp = sqrt(-re)
else if (re <= 1.4d-22) then
tmp = sqrt((0.5d0 * (im - re)))
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 <= -9.8e+35) {
tmp = Math.sqrt(-re);
} else if (re <= 1.4e-22) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = (0.5 * im) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -9.8e+35: tmp = math.sqrt(-re) elif re <= 1.4e-22: tmp = math.sqrt((0.5 * (im - re))) else: tmp = (0.5 * im) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -9.8e+35) tmp = sqrt(Float64(-re)); elseif (re <= 1.4e-22) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64(Float64(0.5 * im) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -9.8e+35) tmp = sqrt(-re); elseif (re <= 1.4e-22) tmp = sqrt((0.5 * (im - re))); else tmp = (0.5 * im) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -9.8e+35], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 1.4e-22], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -9.8 \cdot 10^{+35}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 1.4 \cdot 10^{-22}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -9.8000000000000005e35Initial program 42.6%
pow142.6%
Applied egg-rr98.4%
unpow198.4%
*-commutative98.4%
associate-*r*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 87.8%
neg-mul-187.8%
Simplified87.8%
if -9.8000000000000005e35 < re < 1.39999999999999997e-22Initial program 57.3%
pow157.3%
Applied egg-rr89.2%
unpow189.2%
*-commutative89.2%
associate-*r*89.2%
metadata-eval89.2%
Simplified89.2%
Taylor expanded in re around 0 74.9%
neg-mul-174.9%
unsub-neg74.9%
Simplified74.9%
if 1.39999999999999997e-22 < re Initial program 8.6%
pow18.6%
Applied egg-rr33.7%
unpow133.7%
*-commutative33.7%
associate-*r*33.7%
metadata-eval33.7%
Simplified33.7%
Taylor expanded in re around inf 78.6%
unpow278.6%
rem-square-sqrt79.5%
Simplified79.5%
sqrt-div79.4%
metadata-eval79.4%
un-div-inv79.6%
Applied egg-rr79.6%
Final simplification79.2%
(FPCore (re im) :precision binary64 (if (<= re -9.8e+35) (sqrt (- re)) (if (<= re 5e-23) (sqrt (* 0.5 (- im re))) (* im (/ 0.5 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -9.8e+35) {
tmp = sqrt(-re);
} else if (re <= 5e-23) {
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 <= (-9.8d+35)) then
tmp = sqrt(-re)
else if (re <= 5d-23) 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 <= -9.8e+35) {
tmp = Math.sqrt(-re);
} else if (re <= 5e-23) {
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 <= -9.8e+35: tmp = math.sqrt(-re) elif re <= 5e-23: 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 <= -9.8e+35) tmp = sqrt(Float64(-re)); elseif (re <= 5e-23) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64(im * Float64(0.5 / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -9.8e+35) tmp = sqrt(-re); elseif (re <= 5e-23) tmp = sqrt((0.5 * (im - re))); else tmp = im * (0.5 / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -9.8e+35], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 5e-23], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(im * N[(0.5 / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -9.8 \cdot 10^{+35}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 5 \cdot 10^{-23}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -9.8000000000000005e35Initial program 42.6%
pow142.6%
Applied egg-rr98.4%
unpow198.4%
*-commutative98.4%
associate-*r*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 87.8%
neg-mul-187.8%
Simplified87.8%
if -9.8000000000000005e35 < re < 5.0000000000000002e-23Initial program 57.3%
pow157.3%
Applied egg-rr89.2%
unpow189.2%
*-commutative89.2%
associate-*r*89.2%
metadata-eval89.2%
Simplified89.2%
Taylor expanded in re around 0 74.9%
neg-mul-174.9%
unsub-neg74.9%
Simplified74.9%
if 5.0000000000000002e-23 < re Initial program 8.6%
pow18.6%
Applied egg-rr33.7%
unpow133.7%
*-commutative33.7%
associate-*r*33.7%
metadata-eval33.7%
Simplified33.7%
Taylor expanded in re around inf 78.6%
unpow278.6%
rem-square-sqrt79.5%
Simplified79.5%
add-cube-cbrt78.5%
pow378.5%
associate-*l*78.5%
sqrt-div78.5%
metadata-eval78.5%
un-div-inv78.5%
Applied egg-rr78.5%
rem-cube-cbrt79.4%
*-commutative79.4%
Applied egg-rr79.4%
Final simplification79.1%
(FPCore (re im) :precision binary64 (if (<= re -3e+22) (sqrt (- re)) (sqrt (* 0.5 im))))
double code(double re, double im) {
double tmp;
if (re <= -3e+22) {
tmp = sqrt(-re);
} else {
tmp = sqrt((0.5 * im));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-3d+22)) then
tmp = sqrt(-re)
else
tmp = sqrt((0.5d0 * im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3e+22) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt((0.5 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3e+22: tmp = math.sqrt(-re) else: tmp = math.sqrt((0.5 * im)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3e+22) tmp = sqrt(Float64(-re)); else tmp = sqrt(Float64(0.5 * im)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3e+22) tmp = sqrt(-re); else tmp = sqrt((0.5 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3e+22], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[N[(0.5 * im), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3 \cdot 10^{+22}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot im}\\
\end{array}
\end{array}
if re < -3e22Initial program 44.7%
pow144.7%
Applied egg-rr98.5%
unpow198.5%
*-commutative98.5%
associate-*r*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 85.7%
neg-mul-185.7%
Simplified85.7%
if -3e22 < re Initial program 39.9%
Taylor expanded in re around 0 57.0%
*-commutative57.0%
Simplified57.0%
pow157.0%
add-sqr-sqrt56.6%
sqrt-unprod57.0%
*-commutative57.0%
*-commutative57.0%
swap-sqr57.0%
add-sqr-sqrt57.0%
metadata-eval57.0%
Applied egg-rr57.0%
unpow157.0%
associate-*l*57.0%
metadata-eval57.0%
Simplified57.0%
Final simplification64.2%
(FPCore (re im) :precision binary64 (if (<= re -5e-310) (sqrt (- re)) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= -5e-310) {
tmp = sqrt(-re);
} else {
tmp = 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 <= (-5d-310)) then
tmp = sqrt(-re)
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -5e-310) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -5e-310: tmp = math.sqrt(-re) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -5e-310) tmp = sqrt(Float64(-re)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -5e-310) tmp = sqrt(-re); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -5e-310], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -4.999999999999985e-310Initial program 56.2%
pow156.2%
Applied egg-rr99.3%
unpow199.3%
*-commutative99.3%
associate-*r*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 56.8%
neg-mul-156.8%
Simplified56.8%
if -4.999999999999985e-310 < re Initial program 24.4%
pow124.4%
Applied egg-rr52.2%
unpow152.2%
*-commutative52.2%
associate-*r*52.2%
metadata-eval52.2%
Simplified52.2%
Taylor expanded in re around -inf 0.0%
neg-mul-10.0%
Simplified0.0%
add-sqr-sqrt0.0%
sqrt-unprod4.7%
sqr-neg4.7%
sqrt-unprod5.7%
add-sqr-sqrt5.7%
*-un-lft-identity5.7%
Applied egg-rr5.7%
*-lft-identity5.7%
Simplified5.7%
(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(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 41.1%
pow141.1%
Applied egg-rr76.8%
unpow176.8%
*-commutative76.8%
associate-*r*77.2%
metadata-eval77.2%
Simplified77.2%
Taylor expanded in re around -inf 29.7%
neg-mul-129.7%
Simplified29.7%
add-sqr-sqrt29.7%
sqrt-unprod18.9%
sqr-neg18.9%
sqrt-unprod2.7%
add-sqr-sqrt2.7%
*-un-lft-identity2.7%
Applied egg-rr2.7%
*-lft-identity2.7%
Simplified2.7%
herbie shell --seed 2024143
(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)))))