
(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 7 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.12e-54) (sqrt (* 0.5 (- (hypot re im) re))) (/ (* 0.5 im) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= 1.12e-54) {
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 <= 1.12e-54) {
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 <= 1.12e-54: 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 <= 1.12e-54) 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 <= 1.12e-54) 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, 1.12e-54], 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 1.12 \cdot 10^{-54}:\\
\;\;\;\;\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 < 1.11999999999999994e-54Initial program 57.3%
pow157.3%
Applied egg-rr96.8%
unpow196.8%
*-commutative96.8%
associate-*r*96.8%
metadata-eval96.8%
Simplified96.8%
if 1.11999999999999994e-54 < re Initial program 18.3%
Taylor expanded in re around inf 48.1%
*-commutative48.1%
sqrt-div58.8%
sqrt-pow175.9%
metadata-eval75.9%
pow175.9%
associate-*l/75.9%
Applied egg-rr75.9%
Final simplification90.5%
(FPCore (re im) :precision binary64 (if (<= re -7.2e+61) (sqrt (- re)) (if (<= re 5e-50) (sqrt (* 0.5 (- im re))) (/ (* 0.5 im) (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= -7.2e+61) {
tmp = sqrt(-re);
} else if (re <= 5e-50) {
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 <= (-7.2d+61)) then
tmp = sqrt(-re)
else if (re <= 5d-50) 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 <= -7.2e+61) {
tmp = Math.sqrt(-re);
} else if (re <= 5e-50) {
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 <= -7.2e+61: tmp = math.sqrt(-re) elif re <= 5e-50: 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 <= -7.2e+61) tmp = sqrt(Float64(-re)); elseif (re <= 5e-50) 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 <= -7.2e+61) tmp = sqrt(-re); elseif (re <= 5e-50) tmp = sqrt((0.5 * (im - re))); else tmp = (0.5 * im) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -7.2e+61], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 5e-50], 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 -7.2 \cdot 10^{+61}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 5 \cdot 10^{-50}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -7.20000000000000021e61Initial program 36.8%
pow136.8%
Applied egg-rr100.0%
unpow1100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 82.5%
neg-mul-182.5%
Simplified82.5%
if -7.20000000000000021e61 < re < 4.99999999999999968e-50Initial program 64.6%
pow164.6%
Applied egg-rr95.6%
unpow195.6%
*-commutative95.6%
associate-*r*95.6%
metadata-eval95.6%
Simplified95.6%
Taylor expanded in re around 0 80.3%
neg-mul-180.3%
sub-neg80.3%
Simplified80.3%
if 4.99999999999999968e-50 < re Initial program 18.3%
Taylor expanded in re around inf 48.1%
*-commutative48.1%
sqrt-div58.8%
sqrt-pow175.9%
metadata-eval75.9%
pow175.9%
associate-*l/75.9%
Applied egg-rr75.9%
Final simplification79.4%
(FPCore (re im) :precision binary64 (if (<= re -3.1e+64) (sqrt (- re)) (if (<= re 1.26e-51) (sqrt (* 0.5 (- im re))) (* im (sqrt (/ 0.25 re))))))
double code(double re, double im) {
double tmp;
if (re <= -3.1e+64) {
tmp = sqrt(-re);
} else if (re <= 1.26e-51) {
tmp = sqrt((0.5 * (im - re)));
} else {
tmp = im * sqrt((0.25 / re));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-3.1d+64)) then
tmp = sqrt(-re)
else if (re <= 1.26d-51) then
tmp = sqrt((0.5d0 * (im - re)))
else
tmp = im * sqrt((0.25d0 / re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3.1e+64) {
tmp = Math.sqrt(-re);
} else if (re <= 1.26e-51) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = im * Math.sqrt((0.25 / re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.1e+64: tmp = math.sqrt(-re) elif re <= 1.26e-51: tmp = math.sqrt((0.5 * (im - re))) else: tmp = im * math.sqrt((0.25 / re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.1e+64) tmp = sqrt(Float64(-re)); elseif (re <= 1.26e-51) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64(im * sqrt(Float64(0.25 / re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.1e+64) tmp = sqrt(-re); elseif (re <= 1.26e-51) tmp = sqrt((0.5 * (im - re))); else tmp = im * sqrt((0.25 / re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.1e+64], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 1.26e-51], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(im * N[Sqrt[N[(0.25 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.1 \cdot 10^{+64}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 1.26 \cdot 10^{-51}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\
\end{array}
\end{array}
if re < -3.0999999999999999e64Initial program 36.8%
pow136.8%
Applied egg-rr100.0%
unpow1100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 82.5%
neg-mul-182.5%
Simplified82.5%
if -3.0999999999999999e64 < re < 1.2600000000000001e-51Initial program 64.6%
pow164.6%
Applied egg-rr95.6%
unpow195.6%
*-commutative95.6%
associate-*r*95.6%
metadata-eval95.6%
Simplified95.6%
Taylor expanded in re around 0 80.3%
neg-mul-180.3%
sub-neg80.3%
Simplified80.3%
if 1.2600000000000001e-51 < re Initial program 18.3%
Taylor expanded in re around inf 48.1%
*-commutative48.1%
sqrt-div58.8%
sqrt-pow175.9%
metadata-eval75.9%
pow175.9%
associate-*l/75.9%
Applied egg-rr75.9%
associate-/l*75.8%
*-commutative75.8%
Applied egg-rr75.8%
*-un-lft-identity75.8%
*-commutative75.8%
add-sqr-sqrt75.5%
sqrt-unprod75.8%
frac-times75.7%
metadata-eval75.7%
add-sqr-sqrt75.9%
Applied egg-rr75.9%
*-rgt-identity75.9%
Simplified75.9%
Final simplification79.4%
(FPCore (re im) :precision binary64 (if (<= re -1.8e+64) (sqrt (- re)) (if (<= re 1.42e-52) (sqrt (* 0.5 (- im re))) (* im (/ 0.5 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -1.8e+64) {
tmp = sqrt(-re);
} else if (re <= 1.42e-52) {
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 <= (-1.8d+64)) then
tmp = sqrt(-re)
else if (re <= 1.42d-52) 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 <= -1.8e+64) {
tmp = Math.sqrt(-re);
} else if (re <= 1.42e-52) {
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 <= -1.8e+64: tmp = math.sqrt(-re) elif re <= 1.42e-52: 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 <= -1.8e+64) tmp = sqrt(Float64(-re)); elseif (re <= 1.42e-52) 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 <= -1.8e+64) tmp = sqrt(-re); elseif (re <= 1.42e-52) tmp = sqrt((0.5 * (im - re))); else tmp = im * (0.5 / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.8e+64], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 1.42e-52], 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 -1.8 \cdot 10^{+64}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 1.42 \cdot 10^{-52}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -1.80000000000000007e64Initial program 36.8%
pow136.8%
Applied egg-rr100.0%
unpow1100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 82.5%
neg-mul-182.5%
Simplified82.5%
if -1.80000000000000007e64 < re < 1.4200000000000001e-52Initial program 64.6%
pow164.6%
Applied egg-rr95.6%
unpow195.6%
*-commutative95.6%
associate-*r*95.6%
metadata-eval95.6%
Simplified95.6%
Taylor expanded in re around 0 80.3%
neg-mul-180.3%
sub-neg80.3%
Simplified80.3%
if 1.4200000000000001e-52 < re Initial program 18.3%
Taylor expanded in re around inf 48.1%
sqrt-div58.8%
sqrt-pow175.9%
metadata-eval75.9%
pow175.9%
associate-*r/75.9%
Applied egg-rr75.9%
*-commutative75.9%
associate-/l*75.8%
Simplified75.8%
(FPCore (re im) :precision binary64 (if (<= re -8.5e-118) (sqrt (- re)) (sqrt (* 0.5 im))))
double code(double re, double im) {
double tmp;
if (re <= -8.5e-118) {
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 <= (-8.5d-118)) 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 <= -8.5e-118) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt((0.5 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -8.5e-118: tmp = math.sqrt(-re) else: tmp = math.sqrt((0.5 * im)) return tmp
function code(re, im) tmp = 0.0 if (re <= -8.5e-118) 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 <= -8.5e-118) tmp = sqrt(-re); else tmp = sqrt((0.5 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -8.5e-118], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[N[(0.5 * im), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -8.5 \cdot 10^{-118}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot im}\\
\end{array}
\end{array}
if re < -8.50000000000000087e-118Initial program 54.5%
pow154.5%
Applied egg-rr100.0%
unpow1100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 70.7%
neg-mul-170.7%
Simplified70.7%
if -8.50000000000000087e-118 < re Initial program 41.6%
pow141.6%
Applied egg-rr71.1%
unpow171.1%
*-commutative71.1%
associate-*r*71.1%
metadata-eval71.1%
Simplified71.1%
Taylor expanded in re around 0 62.0%
*-commutative62.0%
Simplified62.0%
Final simplification64.7%
(FPCore (re im) :precision binary64 (if (<= re -2e-310) (sqrt (- re)) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= -2e-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 <= (-2d-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 <= -2e-310) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2e-310: tmp = math.sqrt(-re) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -2e-310) tmp = sqrt(Float64(-re)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2e-310) tmp = sqrt(-re); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2e-310], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2 \cdot 10^{-310}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -1.999999999999994e-310Initial program 57.4%
pow157.4%
Applied egg-rr100.0%
unpow1100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 49.7%
neg-mul-149.7%
Simplified49.7%
if -1.999999999999994e-310 < re Initial program 32.6%
pow132.6%
Applied egg-rr58.3%
unpow158.3%
*-commutative58.3%
associate-*r*58.3%
metadata-eval58.3%
Simplified58.3%
Taylor expanded in re around -inf 0.0%
neg-mul-10.0%
Simplified0.0%
neg-sub00.0%
sub-neg0.0%
add-sqr-sqrt0.0%
sqrt-unprod4.4%
sqr-neg4.4%
sqrt-unprod5.5%
add-sqr-sqrt5.5%
Applied egg-rr5.5%
+-lft-identity5.5%
Simplified5.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(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 45.6%
pow145.6%
Applied egg-rr80.1%
unpow180.1%
*-commutative80.1%
associate-*r*80.1%
metadata-eval80.1%
Simplified80.1%
Taylor expanded in re around -inf 26.0%
neg-mul-126.0%
Simplified26.0%
neg-sub026.0%
sub-neg26.0%
add-sqr-sqrt26.0%
sqrt-unprod16.7%
sqr-neg16.7%
sqrt-unprod2.6%
add-sqr-sqrt2.6%
Applied egg-rr2.6%
+-lft-identity2.6%
Simplified2.6%
herbie shell --seed 2024123
(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)))))