
(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 (<= (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))) 0.0) (* im (sqrt (/ 0.25 re))) (sqrt (* (- (hypot im re) re) 0.5))))
double code(double re, double im) {
double tmp;
if (sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0) {
tmp = im * sqrt((0.25 / re));
} else {
tmp = sqrt(((hypot(im, re) - re) * 0.5));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) - re))) <= 0.0) {
tmp = im * Math.sqrt((0.25 / re));
} else {
tmp = Math.sqrt(((Math.hypot(im, re) - re) * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re))) <= 0.0: tmp = im * math.sqrt((0.25 / re)) else: tmp = math.sqrt(((math.hypot(im, re) - re) * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re))) <= 0.0) tmp = Float64(im * sqrt(Float64(0.25 / re))); else tmp = sqrt(Float64(Float64(hypot(im, re) - re) * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))) <= 0.0) tmp = im * sqrt((0.25 / re)); else tmp = sqrt(((hypot(im, re) - re) * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(im * N[Sqrt[N[(0.25 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(N[(N[Sqrt[im ^ 2 + re ^ 2], $MachinePrecision] - re), $MachinePrecision] * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)} \leq 0:\\
\;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left(\mathsf{hypot}\left(im, re\right) - re\right) \cdot 0.5}\\
\end{array}
\end{array}
if (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 19.2%
sub-neg19.2%
sqr-neg19.2%
sub-neg19.2%
sqr-neg19.2%
hypot-define19.2%
Simplified19.2%
Taylor expanded in re around inf 43.9%
Taylor expanded in im around 0 99.6%
unpow1/299.6%
rem-exp-log94.8%
rec-exp94.8%
exp-prod94.8%
distribute-lft-neg-out94.8%
rec-exp94.8%
exp-to-pow99.5%
unpow1/299.5%
associate-/l*99.6%
*-rgt-identity99.6%
associate-/l*99.6%
*-commutative99.6%
associate-/l*99.5%
Simplified99.5%
add-sqr-sqrt99.0%
sqrt-unprod99.5%
frac-times99.4%
metadata-eval99.4%
add-sqr-sqrt99.6%
Applied egg-rr99.6%
if 0.0 < (sqrt.f64 (*.f64 #s(literal 2 binary64) (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 43.8%
sub-neg43.8%
sqr-neg43.8%
sub-neg43.8%
sqr-neg43.8%
hypot-define90.6%
Simplified90.6%
*-commutative90.6%
hypot-define43.8%
*-commutative43.8%
add-sqr-sqrt43.6%
sqrt-unprod43.8%
*-commutative43.8%
*-commutative43.8%
swap-sqr43.8%
Applied egg-rr90.6%
associate-*l*90.6%
hypot-undefine43.8%
unpow243.8%
unpow243.8%
+-commutative43.8%
unpow243.8%
unpow243.8%
hypot-undefine90.6%
metadata-eval90.6%
Simplified90.6%
(FPCore (re im)
:precision binary64
(if (<= re -2.15e+21)
(sqrt (- re))
(if (<= re 4.1e+38)
(* 0.5 (sqrt (+ (* 2.0 im) (* re (- (/ re im) 2.0)))))
(* 0.5 (* im (pow re -0.5))))))
double code(double re, double im) {
double tmp;
if (re <= -2.15e+21) {
tmp = sqrt(-re);
} else if (re <= 4.1e+38) {
tmp = 0.5 * sqrt(((2.0 * im) + (re * ((re / im) - 2.0))));
} else {
tmp = 0.5 * (im * pow(re, -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 <= (-2.15d+21)) then
tmp = sqrt(-re)
else if (re <= 4.1d+38) then
tmp = 0.5d0 * sqrt(((2.0d0 * im) + (re * ((re / im) - 2.0d0))))
else
tmp = 0.5d0 * (im * (re ** (-0.5d0)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.15e+21) {
tmp = Math.sqrt(-re);
} else if (re <= 4.1e+38) {
tmp = 0.5 * Math.sqrt(((2.0 * im) + (re * ((re / im) - 2.0))));
} else {
tmp = 0.5 * (im * Math.pow(re, -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.15e+21: tmp = math.sqrt(-re) elif re <= 4.1e+38: tmp = 0.5 * math.sqrt(((2.0 * im) + (re * ((re / im) - 2.0)))) else: tmp = 0.5 * (im * math.pow(re, -0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.15e+21) tmp = sqrt(Float64(-re)); elseif (re <= 4.1e+38) tmp = Float64(0.5 * sqrt(Float64(Float64(2.0 * im) + Float64(re * Float64(Float64(re / im) - 2.0))))); else tmp = Float64(0.5 * Float64(im * (re ^ -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.15e+21) tmp = sqrt(-re); elseif (re <= 4.1e+38) tmp = 0.5 * sqrt(((2.0 * im) + (re * ((re / im) - 2.0)))); else tmp = 0.5 * (im * (re ^ -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.15e+21], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 4.1e+38], N[(0.5 * N[Sqrt[N[(N[(2.0 * im), $MachinePrecision] + N[(re * N[(N[(re / im), $MachinePrecision] - 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.15 \cdot 10^{+21}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 4.1 \cdot 10^{+38}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im + re \cdot \left(\frac{re}{im} - 2\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\
\end{array}
\end{array}
if re < -2.15e21Initial program 35.6%
sub-neg35.6%
sqr-neg35.6%
sub-neg35.6%
sqr-neg35.6%
hypot-define100.0%
Simplified100.0%
*-commutative100.0%
hypot-define35.6%
*-commutative35.6%
add-sqr-sqrt35.3%
sqrt-unprod35.6%
*-commutative35.6%
*-commutative35.6%
swap-sqr35.6%
Applied egg-rr100.0%
associate-*l*100.0%
hypot-undefine35.6%
unpow235.6%
unpow235.6%
+-commutative35.6%
unpow235.6%
unpow235.6%
hypot-undefine100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 87.0%
neg-mul-187.0%
Simplified87.0%
if -2.15e21 < re < 4.1000000000000003e38Initial program 52.2%
sub-neg52.2%
sqr-neg52.2%
sub-neg52.2%
sqr-neg52.2%
hypot-define87.3%
Simplified87.3%
Taylor expanded in re around 0 78.2%
if 4.1000000000000003e38 < re Initial program 15.9%
sub-neg15.9%
sqr-neg15.9%
sub-neg15.9%
sqr-neg15.9%
hypot-define44.4%
Simplified44.4%
Taylor expanded in re around inf 51.0%
div-inv51.0%
sqrt-prod59.5%
sqrt-pow178.2%
metadata-eval78.2%
pow178.2%
*-un-lft-identity78.2%
metadata-eval78.2%
metadata-eval78.2%
sqrt-unprod77.3%
associate-*r*77.5%
*-commutative77.5%
inv-pow77.5%
sqrt-pow177.5%
metadata-eval77.5%
associate-*r*77.3%
sqrt-unprod78.3%
metadata-eval78.3%
metadata-eval78.3%
*-un-lft-identity78.3%
Applied egg-rr78.3%
Final simplification80.2%
(FPCore (re im) :precision binary64 (if (<= re -3.8e+28) (sqrt (- re)) (if (<= re 1.6e+39) (sqrt (* 0.5 (- im re))) (* 0.5 (* im (pow re -0.5))))))
double code(double re, double im) {
double tmp;
if (re <= -3.8e+28) {
tmp = sqrt(-re);
} else if (re <= 1.6e+39) {
tmp = sqrt((0.5 * (im - re)));
} else {
tmp = 0.5 * (im * pow(re, -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 <= (-3.8d+28)) then
tmp = sqrt(-re)
else if (re <= 1.6d+39) then
tmp = sqrt((0.5d0 * (im - re)))
else
tmp = 0.5d0 * (im * (re ** (-0.5d0)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3.8e+28) {
tmp = Math.sqrt(-re);
} else if (re <= 1.6e+39) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = 0.5 * (im * Math.pow(re, -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.8e+28: tmp = math.sqrt(-re) elif re <= 1.6e+39: tmp = math.sqrt((0.5 * (im - re))) else: tmp = 0.5 * (im * math.pow(re, -0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.8e+28) tmp = sqrt(Float64(-re)); elseif (re <= 1.6e+39) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64(0.5 * Float64(im * (re ^ -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.8e+28) tmp = sqrt(-re); elseif (re <= 1.6e+39) tmp = sqrt((0.5 * (im - re))); else tmp = 0.5 * (im * (re ^ -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.8e+28], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 1.6e+39], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.8 \cdot 10^{+28}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 1.6 \cdot 10^{+39}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\
\end{array}
\end{array}
if re < -3.7999999999999999e28Initial program 35.6%
sub-neg35.6%
sqr-neg35.6%
sub-neg35.6%
sqr-neg35.6%
hypot-define100.0%
Simplified100.0%
*-commutative100.0%
hypot-define35.6%
*-commutative35.6%
add-sqr-sqrt35.3%
sqrt-unprod35.6%
*-commutative35.6%
*-commutative35.6%
swap-sqr35.6%
Applied egg-rr100.0%
associate-*l*100.0%
hypot-undefine35.6%
unpow235.6%
unpow235.6%
+-commutative35.6%
unpow235.6%
unpow235.6%
hypot-undefine100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 87.0%
neg-mul-187.0%
Simplified87.0%
if -3.7999999999999999e28 < re < 1.59999999999999996e39Initial program 52.2%
sub-neg52.2%
sqr-neg52.2%
sub-neg52.2%
sqr-neg52.2%
hypot-define87.3%
Simplified87.3%
*-commutative87.3%
hypot-define52.2%
*-commutative52.2%
add-sqr-sqrt51.9%
sqrt-unprod52.2%
*-commutative52.2%
*-commutative52.2%
swap-sqr52.2%
Applied egg-rr87.3%
associate-*l*87.3%
hypot-undefine52.2%
unpow252.2%
unpow252.2%
+-commutative52.2%
unpow252.2%
unpow252.2%
hypot-undefine87.3%
metadata-eval87.3%
Simplified87.3%
Taylor expanded in re around 0 78.1%
neg-mul-178.1%
sub-neg78.1%
Simplified78.1%
if 1.59999999999999996e39 < re Initial program 15.9%
sub-neg15.9%
sqr-neg15.9%
sub-neg15.9%
sqr-neg15.9%
hypot-define44.4%
Simplified44.4%
Taylor expanded in re around inf 51.0%
div-inv51.0%
sqrt-prod59.5%
sqrt-pow178.2%
metadata-eval78.2%
pow178.2%
*-un-lft-identity78.2%
metadata-eval78.2%
metadata-eval78.2%
sqrt-unprod77.3%
associate-*r*77.5%
*-commutative77.5%
inv-pow77.5%
sqrt-pow177.5%
metadata-eval77.5%
associate-*r*77.3%
sqrt-unprod78.3%
metadata-eval78.3%
metadata-eval78.3%
*-un-lft-identity78.3%
Applied egg-rr78.3%
Final simplification80.1%
(FPCore (re im) :precision binary64 (if (<= re -2.4e+22) (sqrt (- re)) (if (<= re 2.05e+38) (sqrt (* 0.5 (- im re))) (* im (sqrt (/ 0.25 re))))))
double code(double re, double im) {
double tmp;
if (re <= -2.4e+22) {
tmp = sqrt(-re);
} else if (re <= 2.05e+38) {
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 <= (-2.4d+22)) then
tmp = sqrt(-re)
else if (re <= 2.05d+38) 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 <= -2.4e+22) {
tmp = Math.sqrt(-re);
} else if (re <= 2.05e+38) {
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 <= -2.4e+22: tmp = math.sqrt(-re) elif re <= 2.05e+38: 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 <= -2.4e+22) tmp = sqrt(Float64(-re)); elseif (re <= 2.05e+38) 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 <= -2.4e+22) tmp = sqrt(-re); elseif (re <= 2.05e+38) tmp = sqrt((0.5 * (im - re))); else tmp = im * sqrt((0.25 / re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.4e+22], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 2.05e+38], 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 -2.4 \cdot 10^{+22}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 2.05 \cdot 10^{+38}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\
\end{array}
\end{array}
if re < -2.4e22Initial program 35.6%
sub-neg35.6%
sqr-neg35.6%
sub-neg35.6%
sqr-neg35.6%
hypot-define100.0%
Simplified100.0%
*-commutative100.0%
hypot-define35.6%
*-commutative35.6%
add-sqr-sqrt35.3%
sqrt-unprod35.6%
*-commutative35.6%
*-commutative35.6%
swap-sqr35.6%
Applied egg-rr100.0%
associate-*l*100.0%
hypot-undefine35.6%
unpow235.6%
unpow235.6%
+-commutative35.6%
unpow235.6%
unpow235.6%
hypot-undefine100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 87.0%
neg-mul-187.0%
Simplified87.0%
if -2.4e22 < re < 2.0500000000000002e38Initial program 52.2%
sub-neg52.2%
sqr-neg52.2%
sub-neg52.2%
sqr-neg52.2%
hypot-define87.3%
Simplified87.3%
*-commutative87.3%
hypot-define52.2%
*-commutative52.2%
add-sqr-sqrt51.9%
sqrt-unprod52.2%
*-commutative52.2%
*-commutative52.2%
swap-sqr52.2%
Applied egg-rr87.3%
associate-*l*87.3%
hypot-undefine52.2%
unpow252.2%
unpow252.2%
+-commutative52.2%
unpow252.2%
unpow252.2%
hypot-undefine87.3%
metadata-eval87.3%
Simplified87.3%
Taylor expanded in re around 0 78.1%
neg-mul-178.1%
sub-neg78.1%
Simplified78.1%
if 2.0500000000000002e38 < re Initial program 15.9%
sub-neg15.9%
sqr-neg15.9%
sub-neg15.9%
sqr-neg15.9%
hypot-define44.4%
Simplified44.4%
Taylor expanded in re around inf 51.0%
Taylor expanded in im around 0 78.2%
unpow1/278.2%
rem-exp-log73.7%
rec-exp73.7%
exp-prod73.7%
distribute-lft-neg-out73.7%
rec-exp73.7%
exp-to-pow78.1%
unpow1/278.1%
associate-/l*78.2%
*-rgt-identity78.2%
associate-/l*78.2%
*-commutative78.2%
associate-/l*78.1%
Simplified78.1%
add-sqr-sqrt77.8%
sqrt-unprod78.1%
frac-times78.1%
metadata-eval78.1%
add-sqr-sqrt78.2%
Applied egg-rr78.2%
Final simplification80.1%
(FPCore (re im) :precision binary64 (if (<= re -3.2e+28) (sqrt (- re)) (sqrt (* im 0.5))))
double code(double re, double im) {
double tmp;
if (re <= -3.2e+28) {
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 <= (-3.2d+28)) 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 <= -3.2e+28) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.2e+28: tmp = math.sqrt(-re) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.2e+28) 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 <= -3.2e+28) tmp = sqrt(-re); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.2e+28], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.2 \cdot 10^{+28}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < -3.2e28Initial program 35.6%
sub-neg35.6%
sqr-neg35.6%
sub-neg35.6%
sqr-neg35.6%
hypot-define100.0%
Simplified100.0%
*-commutative100.0%
hypot-define35.6%
*-commutative35.6%
add-sqr-sqrt35.3%
sqrt-unprod35.6%
*-commutative35.6%
*-commutative35.6%
swap-sqr35.6%
Applied egg-rr100.0%
associate-*l*100.0%
hypot-undefine35.6%
unpow235.6%
unpow235.6%
+-commutative35.6%
unpow235.6%
unpow235.6%
hypot-undefine100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 87.0%
neg-mul-187.0%
Simplified87.0%
if -3.2e28 < re Initial program 41.9%
sub-neg41.9%
sqr-neg41.9%
sub-neg41.9%
sqr-neg41.9%
hypot-define75.2%
Simplified75.2%
*-commutative75.2%
hypot-define41.9%
*-commutative41.9%
add-sqr-sqrt41.7%
sqrt-unprod41.9%
*-commutative41.9%
*-commutative41.9%
swap-sqr41.9%
Applied egg-rr75.2%
associate-*l*75.2%
hypot-undefine41.9%
unpow241.9%
unpow241.9%
+-commutative41.9%
unpow241.9%
unpow241.9%
hypot-undefine75.2%
metadata-eval75.2%
Simplified75.2%
Taylor expanded in im around inf 63.4%
(FPCore (re im) :precision binary64 (if (<= re -5e-310) (sqrt (- re)) 0.0))
double code(double re, double im) {
double tmp;
if (re <= -5e-310) {
tmp = sqrt(-re);
} else {
tmp = 0.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-310)) then
tmp = sqrt(-re)
else
tmp = 0.0d0
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 = 0.0;
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -5e-310: tmp = math.sqrt(-re) else: tmp = 0.0 return tmp
function code(re, im) tmp = 0.0 if (re <= -5e-310) tmp = sqrt(Float64(-re)); else tmp = 0.0; end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -5e-310) tmp = sqrt(-re); else tmp = 0.0; end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -5e-310], N[Sqrt[(-re)], $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if re < -4.999999999999985e-310Initial program 52.1%
sub-neg52.1%
sqr-neg52.1%
sub-neg52.1%
sqr-neg52.1%
hypot-define100.0%
Simplified100.0%
*-commutative100.0%
hypot-define52.1%
*-commutative52.1%
add-sqr-sqrt51.7%
sqrt-unprod52.1%
*-commutative52.1%
*-commutative52.1%
swap-sqr52.1%
Applied egg-rr100.0%
associate-*l*100.0%
hypot-undefine52.1%
unpow252.1%
unpow252.1%
+-commutative52.1%
unpow252.1%
unpow252.1%
hypot-undefine100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around -inf 55.5%
neg-mul-155.5%
Simplified55.5%
if -4.999999999999985e-310 < re Initial program 30.2%
sub-neg30.2%
sqr-neg30.2%
sub-neg30.2%
sqr-neg30.2%
hypot-define63.9%
Simplified63.9%
*-commutative63.9%
hypot-define30.2%
*-commutative30.2%
add-sqr-sqrt30.1%
sqrt-unprod30.2%
*-commutative30.2%
*-commutative30.2%
swap-sqr30.2%
Applied egg-rr63.9%
associate-*l*63.9%
hypot-undefine30.2%
unpow230.2%
unpow230.2%
+-commutative30.2%
unpow230.2%
unpow230.2%
hypot-undefine63.9%
metadata-eval63.9%
Simplified63.9%
Taylor expanded in im around 0 10.8%
Taylor expanded in re around 0 10.8%
(FPCore (re im) :precision binary64 0.0)
double code(double re, double im) {
return 0.0;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.0d0
end function
public static double code(double re, double im) {
return 0.0;
}
def code(re, im): return 0.0
function code(re, im) return 0.0 end
function tmp = code(re, im) tmp = 0.0; end
code[re_, im_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 40.5%
sub-neg40.5%
sqr-neg40.5%
sub-neg40.5%
sqr-neg40.5%
hypot-define80.8%
Simplified80.8%
*-commutative80.8%
hypot-define40.5%
*-commutative40.5%
add-sqr-sqrt40.2%
sqrt-unprod40.5%
*-commutative40.5%
*-commutative40.5%
swap-sqr40.5%
Applied egg-rr80.8%
associate-*l*80.8%
hypot-undefine40.5%
unpow240.5%
unpow240.5%
+-commutative40.5%
unpow240.5%
unpow240.5%
hypot-undefine80.8%
metadata-eval80.8%
Simplified80.8%
Taylor expanded in im around 0 7.1%
Taylor expanded in re around 0 7.1%
herbie shell --seed 2024117
(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)))))