
(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 (<= (- (sqrt (+ (* re re) (* im im))) re) 0.0) (/ (* im 0.5) (sqrt re)) (sqrt (* 0.5 (- (hypot re im) re)))))
double code(double re, double im) {
double tmp;
if ((sqrt(((re * re) + (im * im))) - re) <= 0.0) {
tmp = (im * 0.5) / sqrt(re);
} else {
tmp = sqrt((0.5 * (hypot(re, im) - re)));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if ((Math.sqrt(((re * re) + (im * im))) - re) <= 0.0) {
tmp = (im * 0.5) / Math.sqrt(re);
} else {
tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
}
return tmp;
}
def code(re, im): tmp = 0 if (math.sqrt(((re * re) + (im * im))) - re) <= 0.0: tmp = (im * 0.5) / math.sqrt(re) else: tmp = math.sqrt((0.5 * (math.hypot(re, im) - re))) return tmp
function code(re, im) tmp = 0.0 if (Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re) <= 0.0) tmp = Float64(Float64(im * 0.5) / sqrt(re)); else tmp = sqrt(Float64(0.5 * Float64(hypot(re, im) - re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if ((sqrt(((re * re) + (im * im))) - re) <= 0.0) tmp = (im * 0.5) / sqrt(re); else tmp = sqrt((0.5 * (hypot(re, im) - re))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - re), $MachinePrecision], 0.0], N[(N[(im * 0.5), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\end{array}
\end{array}
if (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0Initial program 5.0%
Taylor expanded in im around 0 92.0%
associate-*l*92.2%
*-commutative92.2%
Simplified92.2%
associate-*r*92.2%
sqrt-unprod93.4%
metadata-eval93.4%
metadata-eval93.4%
*-un-lft-identity93.4%
sqrt-div93.4%
metadata-eval93.4%
un-div-inv93.4%
*-commutative93.4%
Applied egg-rr93.4%
if 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 46.7%
add-sqr-sqrt46.4%
sqrt-unprod46.7%
*-commutative46.7%
*-commutative46.7%
swap-sqr46.7%
add-sqr-sqrt46.7%
*-commutative46.7%
hypot-define90.2%
metadata-eval90.2%
Applied egg-rr90.2%
associate-*l*90.2%
metadata-eval90.2%
Simplified90.2%
Final simplification90.8%
(FPCore (re im)
:precision binary64
(if (<= re -4.2e+71)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 3.6e-67)
(sqrt (* 0.5 (- im re)))
(if (or (<= re 1.4e-51) (not (<= re 7.2e+81)))
(* im (/ 0.5 (sqrt re)))
(sqrt (* im 0.5))))))
double code(double re, double im) {
double tmp;
if (re <= -4.2e+71) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 3.6e-67) {
tmp = sqrt((0.5 * (im - re)));
} else if ((re <= 1.4e-51) || !(re <= 7.2e+81)) {
tmp = im * (0.5 / 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 <= (-4.2d+71)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 3.6d-67) then
tmp = sqrt((0.5d0 * (im - re)))
else if ((re <= 1.4d-51) .or. (.not. (re <= 7.2d+81))) then
tmp = im * (0.5d0 / 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 <= -4.2e+71) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 3.6e-67) {
tmp = Math.sqrt((0.5 * (im - re)));
} else if ((re <= 1.4e-51) || !(re <= 7.2e+81)) {
tmp = im * (0.5 / Math.sqrt(re));
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -4.2e+71: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 3.6e-67: tmp = math.sqrt((0.5 * (im - re))) elif (re <= 1.4e-51) or not (re <= 7.2e+81): tmp = im * (0.5 / math.sqrt(re)) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -4.2e+71) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 3.6e-67) tmp = sqrt(Float64(0.5 * Float64(im - re))); elseif ((re <= 1.4e-51) || !(re <= 7.2e+81)) tmp = Float64(im * Float64(0.5 / sqrt(re))); else tmp = sqrt(Float64(im * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4.2e+71) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 3.6e-67) tmp = sqrt((0.5 * (im - re))); elseif ((re <= 1.4e-51) || ~((re <= 7.2e+81))) tmp = im * (0.5 / sqrt(re)); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -4.2e+71], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.6e-67], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[Or[LessEqual[re, 1.4e-51], N[Not[LessEqual[re, 7.2e+81]], $MachinePrecision]], N[(im * N[(0.5 / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.2 \cdot 10^{+71}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 3.6 \cdot 10^{-67}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{elif}\;re \leq 1.4 \cdot 10^{-51} \lor \neg \left(re \leq 7.2 \cdot 10^{+81}\right):\\
\;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < -4.19999999999999978e71Initial program 37.4%
sub-neg37.4%
sqr-neg37.4%
sub-neg37.4%
sqr-neg37.4%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around -inf 89.4%
*-commutative89.4%
Simplified89.4%
if -4.19999999999999978e71 < re < 3.59999999999999999e-67Initial program 56.8%
add-sqr-sqrt56.4%
sqrt-unprod56.8%
*-commutative56.8%
*-commutative56.8%
swap-sqr56.8%
add-sqr-sqrt56.8%
*-commutative56.8%
hypot-define90.5%
metadata-eval90.5%
Applied egg-rr90.5%
associate-*l*90.5%
metadata-eval90.5%
Simplified90.5%
Taylor expanded in re around 0 77.0%
mul-1-neg77.0%
unsub-neg77.0%
Simplified77.0%
if 3.59999999999999999e-67 < re < 1.4e-51 or 7.20000000000000011e81 < re Initial program 3.5%
Taylor expanded in im around 0 91.2%
associate-*l*91.3%
*-commutative91.3%
Simplified91.3%
associate-*r*91.4%
sqrt-unprod92.4%
metadata-eval92.4%
metadata-eval92.4%
*-un-lft-identity92.4%
sqrt-div92.3%
metadata-eval92.3%
un-div-inv92.5%
*-commutative92.5%
Applied egg-rr92.5%
associate-/l*92.3%
*-commutative92.3%
Applied egg-rr92.3%
if 1.4e-51 < re < 7.20000000000000011e81Initial program 38.4%
add-sqr-sqrt38.1%
sqrt-unprod38.4%
*-commutative38.4%
*-commutative38.4%
swap-sqr38.4%
add-sqr-sqrt38.4%
*-commutative38.4%
hypot-define71.2%
metadata-eval71.2%
Applied egg-rr71.2%
associate-*l*71.2%
metadata-eval71.2%
Simplified71.2%
Taylor expanded in re around 0 69.7%
Final simplification81.4%
(FPCore (re im)
:precision binary64
(if (<= re -4.2e+71)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 2.7e-64)
(sqrt (* 0.5 (- im re)))
(if (<= re 1.4e-51)
(* im (/ 0.5 (sqrt re)))
(if (<= re 5.4e+81) (sqrt (* im 0.5)) (/ (* im 0.5) (sqrt re)))))))
double code(double re, double im) {
double tmp;
if (re <= -4.2e+71) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 2.7e-64) {
tmp = sqrt((0.5 * (im - re)));
} else if (re <= 1.4e-51) {
tmp = im * (0.5 / sqrt(re));
} else if (re <= 5.4e+81) {
tmp = sqrt((im * 0.5));
} 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 <= (-4.2d+71)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 2.7d-64) then
tmp = sqrt((0.5d0 * (im - re)))
else if (re <= 1.4d-51) then
tmp = im * (0.5d0 / sqrt(re))
else if (re <= 5.4d+81) then
tmp = sqrt((im * 0.5d0))
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 <= -4.2e+71) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 2.7e-64) {
tmp = Math.sqrt((0.5 * (im - re)));
} else if (re <= 1.4e-51) {
tmp = im * (0.5 / Math.sqrt(re));
} else if (re <= 5.4e+81) {
tmp = Math.sqrt((im * 0.5));
} else {
tmp = (im * 0.5) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -4.2e+71: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 2.7e-64: tmp = math.sqrt((0.5 * (im - re))) elif re <= 1.4e-51: tmp = im * (0.5 / math.sqrt(re)) elif re <= 5.4e+81: tmp = math.sqrt((im * 0.5)) else: tmp = (im * 0.5) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -4.2e+71) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 2.7e-64) tmp = sqrt(Float64(0.5 * Float64(im - re))); elseif (re <= 1.4e-51) tmp = Float64(im * Float64(0.5 / sqrt(re))); elseif (re <= 5.4e+81) tmp = sqrt(Float64(im * 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 <= -4.2e+71) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 2.7e-64) tmp = sqrt((0.5 * (im - re))); elseif (re <= 1.4e-51) tmp = im * (0.5 / sqrt(re)); elseif (re <= 5.4e+81) tmp = sqrt((im * 0.5)); else tmp = (im * 0.5) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -4.2e+71], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.7e-64], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[re, 1.4e-51], N[(im * N[(0.5 / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5.4e+81], N[Sqrt[N[(im * 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 -4.2 \cdot 10^{+71}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 2.7 \cdot 10^{-64}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{elif}\;re \leq 1.4 \cdot 10^{-51}:\\
\;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\
\mathbf{elif}\;re \leq 5.4 \cdot 10^{+81}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;\frac{im \cdot 0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -4.19999999999999978e71Initial program 37.4%
sub-neg37.4%
sqr-neg37.4%
sub-neg37.4%
sqr-neg37.4%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around -inf 89.4%
*-commutative89.4%
Simplified89.4%
if -4.19999999999999978e71 < re < 2.69999999999999986e-64Initial program 56.8%
add-sqr-sqrt56.4%
sqrt-unprod56.8%
*-commutative56.8%
*-commutative56.8%
swap-sqr56.8%
add-sqr-sqrt56.8%
*-commutative56.8%
hypot-define90.5%
metadata-eval90.5%
Applied egg-rr90.5%
associate-*l*90.5%
metadata-eval90.5%
Simplified90.5%
Taylor expanded in re around 0 77.0%
mul-1-neg77.0%
unsub-neg77.0%
Simplified77.0%
if 2.69999999999999986e-64 < re < 1.4e-51Initial program 5.5%
Taylor expanded in im around 0 97.6%
associate-*l*98.2%
*-commutative98.2%
Simplified98.2%
associate-*r*98.2%
sqrt-unprod99.7%
metadata-eval99.7%
metadata-eval99.7%
*-un-lft-identity99.7%
sqrt-div100.0%
metadata-eval100.0%
un-div-inv99.7%
*-commutative99.7%
Applied egg-rr99.7%
associate-/l*100.0%
*-commutative100.0%
Applied egg-rr100.0%
if 1.4e-51 < re < 5.3999999999999999e81Initial program 38.4%
add-sqr-sqrt38.1%
sqrt-unprod38.4%
*-commutative38.4%
*-commutative38.4%
swap-sqr38.4%
add-sqr-sqrt38.4%
*-commutative38.4%
hypot-define71.2%
metadata-eval71.2%
Applied egg-rr71.2%
associate-*l*71.2%
metadata-eval71.2%
Simplified71.2%
Taylor expanded in re around 0 69.7%
if 5.3999999999999999e81 < re Initial program 3.2%
Taylor expanded in im around 0 90.4%
associate-*l*90.4%
*-commutative90.4%
Simplified90.4%
associate-*r*90.4%
sqrt-unprod91.4%
metadata-eval91.4%
metadata-eval91.4%
*-un-lft-identity91.4%
sqrt-div91.3%
metadata-eval91.3%
un-div-inv91.5%
*-commutative91.5%
Applied egg-rr91.5%
Final simplification81.4%
(FPCore (re im) :precision binary64 (if (<= re 5e-293) (sqrt (* 0.5 (- im re))) (sqrt (* im 0.5))))
double code(double re, double im) {
double tmp;
if (re <= 5e-293) {
tmp = sqrt((0.5 * (im - 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 <= 5d-293) then
tmp = sqrt((0.5d0 * (im - 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 <= 5e-293) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 5e-293: tmp = math.sqrt((0.5 * (im - re))) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= 5e-293) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = sqrt(Float64(im * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 5e-293) tmp = sqrt((0.5 * (im - re))); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 5e-293], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 5 \cdot 10^{-293}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < 5.0000000000000003e-293Initial program 51.0%
add-sqr-sqrt50.7%
sqrt-unprod51.0%
*-commutative51.0%
*-commutative51.0%
swap-sqr51.0%
add-sqr-sqrt51.0%
*-commutative51.0%
hypot-define100.0%
metadata-eval100.0%
Applied egg-rr100.0%
associate-*l*100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in re around 0 57.7%
mul-1-neg57.7%
unsub-neg57.7%
Simplified57.7%
if 5.0000000000000003e-293 < re Initial program 29.6%
add-sqr-sqrt29.4%
sqrt-unprod29.6%
*-commutative29.6%
*-commutative29.6%
swap-sqr29.6%
add-sqr-sqrt29.6%
*-commutative29.6%
hypot-define57.1%
metadata-eval57.1%
Applied egg-rr57.1%
associate-*l*57.1%
metadata-eval57.1%
Simplified57.1%
Taylor expanded in re around 0 50.6%
Final simplification54.0%
(FPCore (re im) :precision binary64 (if (<= re -1.75e+71) (* 0.5 (sqrt (* re -4.0))) (sqrt (* im 0.5))))
double code(double re, double im) {
double tmp;
if (re <= -1.75e+71) {
tmp = 0.5 * sqrt((re * -4.0));
} 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 <= (-1.75d+71)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else
tmp = sqrt((im * 0.5d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.75e+71) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else {
tmp = Math.sqrt((im * 0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.75e+71: tmp = 0.5 * math.sqrt((re * -4.0)) else: tmp = math.sqrt((im * 0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.75e+71) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); else tmp = sqrt(Float64(im * 0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.75e+71) tmp = 0.5 * sqrt((re * -4.0)); else tmp = sqrt((im * 0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.75e+71], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.75 \cdot 10^{+71}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\end{array}
\end{array}
if re < -1.75e71Initial program 37.4%
sub-neg37.4%
sqr-neg37.4%
sub-neg37.4%
sqr-neg37.4%
hypot-define100.0%
Simplified100.0%
Taylor expanded in re around -inf 89.4%
*-commutative89.4%
Simplified89.4%
if -1.75e71 < re Initial program 40.3%
add-sqr-sqrt40.0%
sqrt-unprod40.3%
*-commutative40.3%
*-commutative40.3%
swap-sqr40.3%
add-sqr-sqrt40.3%
*-commutative40.3%
hypot-define71.6%
metadata-eval71.6%
Applied egg-rr71.6%
associate-*l*71.6%
metadata-eval71.6%
Simplified71.6%
Taylor expanded in re around 0 59.4%
Final simplification65.5%
(FPCore (re im) :precision binary64 (sqrt (* im 0.5)))
double code(double re, double im) {
return sqrt((im * 0.5));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt((im * 0.5d0))
end function
public static double code(double re, double im) {
return Math.sqrt((im * 0.5));
}
def code(re, im): return math.sqrt((im * 0.5))
function code(re, im) return sqrt(Float64(im * 0.5)) end
function tmp = code(re, im) tmp = sqrt((im * 0.5)); end
code[re_, im_] := N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{im \cdot 0.5}
\end{array}
Initial program 39.7%
add-sqr-sqrt39.4%
sqrt-unprod39.7%
*-commutative39.7%
*-commutative39.7%
swap-sqr39.7%
add-sqr-sqrt39.7%
*-commutative39.7%
hypot-define77.4%
metadata-eval77.4%
Applied egg-rr77.4%
associate-*l*77.4%
metadata-eval77.4%
Simplified77.4%
Taylor expanded in re around 0 50.4%
Final simplification50.4%
herbie shell --seed 2024043
(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)))))