
(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 9 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 (sqrt (+ (* re re) (* im im)))) 0.0) (* 0.5 (pow (* (cbrt (/ im re)) (cbrt (- im))) 1.5)) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
double code(double re, double im) {
double tmp;
if ((re + sqrt(((re * re) + (im * im)))) <= 0.0) {
tmp = 0.5 * pow((cbrt((im / re)) * cbrt(-im)), 1.5);
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if ((re + Math.sqrt(((re * re) + (im * im)))) <= 0.0) {
tmp = 0.5 * Math.pow((Math.cbrt((im / re)) * Math.cbrt(-im)), 1.5);
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
}
return tmp;
}
function code(re, im) tmp = 0.0 if (Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))) <= 0.0) tmp = Float64(0.5 * (Float64(cbrt(Float64(im / re)) * cbrt(Float64(-im))) ^ 1.5)); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); end return tmp end
code[re_, im_] := If[LessEqual[N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 0.0], N[(0.5 * N[Power[N[(N[Power[N[(im / re), $MachinePrecision], 1/3], $MachinePrecision] * N[Power[(-im), 1/3], $MachinePrecision]), $MachinePrecision], 1.5], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re + \sqrt{re \cdot re + im \cdot im} \leq 0:\\
\;\;\;\;0.5 \cdot {\left(\sqrt[3]{\frac{im}{re}} \cdot \sqrt[3]{-im}\right)}^{1.5}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0Initial program 6.0%
+-commutative6.0%
hypot-def13.6%
Simplified13.6%
Taylor expanded in re around -inf 46.4%
*-commutative46.4%
unpow246.4%
Simplified46.4%
pow1/246.4%
add-cube-cbrt45.7%
pow345.8%
metadata-eval45.8%
pow-pow45.8%
*-commutative45.8%
associate-*l*45.8%
associate-/l*57.8%
associate-/r/57.7%
metadata-eval57.7%
metadata-eval57.7%
metadata-eval57.7%
Applied egg-rr57.7%
associate-*l*57.7%
cbrt-prod86.5%
Applied egg-rr86.5%
*-commutative86.5%
neg-mul-186.5%
Simplified86.5%
if 0.0 < (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 48.1%
+-commutative48.1%
hypot-def90.2%
Simplified90.2%
Final simplification89.6%
(FPCore (re im) :precision binary64 (if (<= re -9e+52) (* 0.5 (sqrt (/ im (/ (- re) im)))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
double code(double re, double im) {
double tmp;
if (re <= -9e+52) {
tmp = 0.5 * sqrt((im / (-re / im)));
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= -9e+52) {
tmp = 0.5 * Math.sqrt((im / (-re / im)));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -9e+52: tmp = 0.5 * math.sqrt((im / (-re / im))) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) return tmp
function code(re, im) tmp = 0.0 if (re <= -9e+52) tmp = Float64(0.5 * sqrt(Float64(im / Float64(Float64(-re) / im)))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -9e+52) tmp = 0.5 * sqrt((im / (-re / im))); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -9e+52], N[(0.5 * N[Sqrt[N[(im / N[((-re) / im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -9 \cdot 10^{+52}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im}{\frac{-re}{im}}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -8.9999999999999999e52Initial program 8.5%
+-commutative8.5%
hypot-def25.1%
Simplified25.1%
Taylor expanded in re around -inf 59.3%
*-commutative59.3%
unpow259.3%
Simplified59.3%
add-log-exp21.8%
*-un-lft-identity21.8%
log-prod21.8%
metadata-eval21.8%
add-log-exp59.3%
*-commutative59.3%
associate-*l*59.3%
associate-/l*68.4%
associate-/r/68.2%
metadata-eval68.2%
Applied egg-rr68.2%
+-lft-identity68.2%
*-commutative68.2%
metadata-eval68.2%
associate-*l/59.3%
times-frac59.3%
neg-mul-159.3%
*-lft-identity59.3%
associate-/l*68.4%
distribute-frac-neg68.4%
Simplified68.4%
if -8.9999999999999999e52 < re Initial program 50.1%
+-commutative50.1%
hypot-def92.0%
Simplified92.0%
Final simplification87.0%
(FPCore (re im)
:precision binary64
(if (<= re 1.22e-116)
(* 0.5 (sqrt (* im 2.0)))
(if (or (<= re 2.1e-57) (not (<= re 3.6e-35)))
(* 0.5 (* 2.0 (sqrt re)))
(* 0.5 (sqrt (* 2.0 (+ re im)))))))
double code(double re, double im) {
double tmp;
if (re <= 1.22e-116) {
tmp = 0.5 * sqrt((im * 2.0));
} else if ((re <= 2.1e-57) || !(re <= 3.6e-35)) {
tmp = 0.5 * (2.0 * sqrt(re));
} else {
tmp = 0.5 * sqrt((2.0 * (re + im)));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= 1.22d-116) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else if ((re <= 2.1d-57) .or. (.not. (re <= 3.6d-35))) then
tmp = 0.5d0 * (2.0d0 * sqrt(re))
else
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 1.22e-116) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else if ((re <= 2.1e-57) || !(re <= 3.6e-35)) {
tmp = 0.5 * (2.0 * Math.sqrt(re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 1.22e-116: tmp = 0.5 * math.sqrt((im * 2.0)) elif (re <= 2.1e-57) or not (re <= 3.6e-35): tmp = 0.5 * (2.0 * math.sqrt(re)) else: tmp = 0.5 * math.sqrt((2.0 * (re + im))) return tmp
function code(re, im) tmp = 0.0 if (re <= 1.22e-116) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); elseif ((re <= 2.1e-57) || !(re <= 3.6e-35)) tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 1.22e-116) tmp = 0.5 * sqrt((im * 2.0)); elseif ((re <= 2.1e-57) || ~((re <= 3.6e-35))) tmp = 0.5 * (2.0 * sqrt(re)); else tmp = 0.5 * sqrt((2.0 * (re + im))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 1.22e-116], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 2.1e-57], N[Not[LessEqual[re, 3.6e-35]], $MachinePrecision]], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 1.22 \cdot 10^{-116}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{elif}\;re \leq 2.1 \cdot 10^{-57} \lor \neg \left(re \leq 3.6 \cdot 10^{-35}\right):\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\end{array}
\end{array}
if re < 1.22e-116Initial program 38.1%
+-commutative38.1%
hypot-def67.9%
Simplified67.9%
Taylor expanded in re around 0 24.1%
if 1.22e-116 < re < 2.0999999999999999e-57 or 3.60000000000000019e-35 < re Initial program 47.5%
+-commutative47.5%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 79.4%
unpow279.4%
rem-square-sqrt81.0%
Simplified81.0%
if 2.0999999999999999e-57 < re < 3.60000000000000019e-35Initial program 58.9%
+-commutative58.9%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around 0 45.8%
Final simplification40.9%
(FPCore (re im) :precision binary64 (if (or (<= re 1.22e-116) (and (not (<= re 4.2e-57)) (<= re 3.7e-36))) (* 0.5 (sqrt (* im 2.0))) (* 0.5 (* 2.0 (sqrt re)))))
double code(double re, double im) {
double tmp;
if ((re <= 1.22e-116) || (!(re <= 4.2e-57) && (re <= 3.7e-36))) {
tmp = 0.5 * sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * 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.22d-116) .or. (.not. (re <= 4.2d-57)) .and. (re <= 3.7d-36)) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if ((re <= 1.22e-116) || (!(re <= 4.2e-57) && (re <= 3.7e-36))) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if (re <= 1.22e-116) or (not (re <= 4.2e-57) and (re <= 3.7e-36)): tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if ((re <= 1.22e-116) || (!(re <= 4.2e-57) && (re <= 3.7e-36))) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if ((re <= 1.22e-116) || (~((re <= 4.2e-57)) && (re <= 3.7e-36))) tmp = 0.5 * sqrt((im * 2.0)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[Or[LessEqual[re, 1.22e-116], And[N[Not[LessEqual[re, 4.2e-57]], $MachinePrecision], LessEqual[re, 3.7e-36]]], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 1.22 \cdot 10^{-116} \lor \neg \left(re \leq 4.2 \cdot 10^{-57}\right) \land re \leq 3.7 \cdot 10^{-36}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < 1.22e-116 or 4.1999999999999999e-57 < re < 3.70000000000000002e-36Initial program 38.9%
+-commutative38.9%
hypot-def69.1%
Simplified69.1%
Taylor expanded in re around 0 24.9%
if 1.22e-116 < re < 4.1999999999999999e-57 or 3.70000000000000002e-36 < re Initial program 47.5%
+-commutative47.5%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 79.4%
unpow279.4%
rem-square-sqrt81.0%
Simplified81.0%
Final simplification40.9%
(FPCore (re im)
:precision binary64
(if (<= re -5e+125)
(* 0.5 (sqrt (/ (* im im) re)))
(if (<= re 6.6e-36)
(* 0.5 (sqrt (* 2.0 (+ re im))))
(* 0.5 (* 2.0 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -5e+125) {
tmp = 0.5 * sqrt(((im * im) / re));
} else if (re <= 6.6e-36) {
tmp = 0.5 * sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * 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+125)) then
tmp = 0.5d0 * sqrt(((im * im) / re))
else if (re <= 6.6d-36) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -5e+125) {
tmp = 0.5 * Math.sqrt(((im * im) / re));
} else if (re <= 6.6e-36) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -5e+125: tmp = 0.5 * math.sqrt(((im * im) / re)) elif re <= 6.6e-36: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -5e+125) tmp = Float64(0.5 * sqrt(Float64(Float64(im * im) / re))); elseif (re <= 6.6e-36) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -5e+125) tmp = 0.5 * sqrt(((im * im) / re)); elseif (re <= 6.6e-36) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -5e+125], N[(0.5 * N[Sqrt[N[(N[(im * im), $MachinePrecision] / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 6.6e-36], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -5 \cdot 10^{+125}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot im}{re}}\\
\mathbf{elif}\;re \leq 6.6 \cdot 10^{-36}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -4.99999999999999962e125Initial program 5.1%
+-commutative5.1%
hypot-def26.5%
Simplified26.5%
Taylor expanded in re around -inf 61.6%
*-commutative61.6%
unpow261.6%
Simplified61.6%
pow1/261.6%
add-cube-cbrt61.2%
pow361.3%
metadata-eval61.3%
pow-pow61.2%
*-commutative61.2%
associate-*l*61.2%
associate-/l*75.9%
associate-/r/76.0%
metadata-eval76.0%
metadata-eval76.0%
metadata-eval76.0%
Applied egg-rr76.0%
pow1/372.2%
pow-pow76.4%
metadata-eval76.4%
pow1/276.4%
*-commutative76.4%
metadata-eval76.4%
*-commutative76.4%
clear-num76.4%
un-div-inv76.5%
times-frac76.5%
*-un-lft-identity76.5%
neg-mul-176.5%
div-inv76.4%
sqrt-prod33.0%
add-sqr-sqrt33.0%
sqrt-unprod27.2%
sqr-neg27.2%
sqrt-unprod0.0%
add-sqr-sqrt14.9%
clear-num14.9%
Applied egg-rr21.7%
associate-*r/21.7%
Simplified21.7%
if -4.99999999999999962e125 < re < 6.59999999999999981e-36Initial program 48.8%
+-commutative48.8%
hypot-def79.7%
Simplified79.7%
Taylor expanded in re around 0 31.2%
if 6.59999999999999981e-36 < re Initial program 41.6%
+-commutative41.6%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 79.7%
unpow279.7%
rem-square-sqrt81.2%
Simplified81.2%
Final simplification42.5%
(FPCore (re im)
:precision binary64
(if (<= re -2.45e+122)
(* 0.5 (sqrt (* 2.0 (- re re))))
(if (<= re 7.4e-37)
(* 0.5 (sqrt (* 2.0 (+ re im))))
(* 0.5 (* 2.0 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -2.45e+122) {
tmp = 0.5 * sqrt((2.0 * (re - re)));
} else if (re <= 7.4e-37) {
tmp = 0.5 * sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * 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 <= (-2.45d+122)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re - re)))
else if (re <= 7.4d-37) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.45e+122) {
tmp = 0.5 * Math.sqrt((2.0 * (re - re)));
} else if (re <= 7.4e-37) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.45e+122: tmp = 0.5 * math.sqrt((2.0 * (re - re))) elif re <= 7.4e-37: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.45e+122) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - re)))); elseif (re <= 7.4e-37) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.45e+122) tmp = 0.5 * sqrt((2.0 * (re - re))); elseif (re <= 7.4e-37) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.45e+122], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 7.4e-37], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.45 \cdot 10^{+122}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - re\right)}\\
\mathbf{elif}\;re \leq 7.4 \cdot 10^{-37}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -2.4499999999999999e122Initial program 5.1%
Taylor expanded in re around -inf 23.0%
mul-1-neg23.0%
Simplified23.0%
if -2.4499999999999999e122 < re < 7.4e-37Initial program 49.0%
+-commutative49.0%
hypot-def80.1%
Simplified80.1%
Taylor expanded in re around 0 31.4%
if 7.4e-37 < re Initial program 41.6%
+-commutative41.6%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 79.7%
unpow279.7%
rem-square-sqrt81.2%
Simplified81.2%
Final simplification42.8%
(FPCore (re im)
:precision binary64
(if (<= re -1.95e+48)
(* 0.5 (sqrt (* im (/ (- im) re))))
(if (<= re 1.65e-35)
(* 0.5 (sqrt (* 2.0 (+ re im))))
(* 0.5 (* 2.0 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -1.95e+48) {
tmp = 0.5 * sqrt((im * (-im / re)));
} else if (re <= 1.65e-35) {
tmp = 0.5 * sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * 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.95d+48)) then
tmp = 0.5d0 * sqrt((im * (-im / re)))
else if (re <= 1.65d-35) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.95e+48) {
tmp = 0.5 * Math.sqrt((im * (-im / re)));
} else if (re <= 1.65e-35) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.95e+48: tmp = 0.5 * math.sqrt((im * (-im / re))) elif re <= 1.65e-35: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.95e+48) tmp = Float64(0.5 * sqrt(Float64(im * Float64(Float64(-im) / re)))); elseif (re <= 1.65e-35) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.95e+48) tmp = 0.5 * sqrt((im * (-im / re))); elseif (re <= 1.65e-35) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.95e+48], N[(0.5 * N[Sqrt[N[(im * N[((-im) / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.65e-35], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.95 \cdot 10^{+48}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\
\mathbf{elif}\;re \leq 1.65 \cdot 10^{-35}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -1.95e48Initial program 8.5%
+-commutative8.5%
hypot-def25.1%
Simplified25.1%
Taylor expanded in re around -inf 59.3%
*-commutative59.3%
unpow259.3%
Simplified59.3%
add-log-exp21.8%
*-un-lft-identity21.8%
log-prod21.8%
metadata-eval21.8%
add-log-exp59.3%
*-commutative59.3%
associate-*l*59.3%
associate-/l*68.4%
associate-/r/68.2%
metadata-eval68.2%
Applied egg-rr68.2%
+-lft-identity68.2%
associate-*l*68.2%
*-commutative68.2%
neg-mul-168.2%
Simplified68.2%
if -1.95e48 < re < 1.65e-35Initial program 54.1%
+-commutative54.1%
hypot-def88.3%
Simplified88.3%
Taylor expanded in re around 0 35.2%
if 1.65e-35 < re Initial program 41.6%
+-commutative41.6%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 79.7%
unpow279.7%
rem-square-sqrt81.2%
Simplified81.2%
Final simplification53.7%
(FPCore (re im)
:precision binary64
(if (<= re -3.75e+50)
(* 0.5 (sqrt (/ im (/ (- re) im))))
(if (<= re 7e-36)
(* 0.5 (sqrt (* 2.0 (+ re im))))
(* 0.5 (* 2.0 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -3.75e+50) {
tmp = 0.5 * sqrt((im / (-re / im)));
} else if (re <= 7e-36) {
tmp = 0.5 * sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * 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 <= (-3.75d+50)) then
tmp = 0.5d0 * sqrt((im / (-re / im)))
else if (re <= 7d-36) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3.75e+50) {
tmp = 0.5 * Math.sqrt((im / (-re / im)));
} else if (re <= 7e-36) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.75e+50: tmp = 0.5 * math.sqrt((im / (-re / im))) elif re <= 7e-36: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.75e+50) tmp = Float64(0.5 * sqrt(Float64(im / Float64(Float64(-re) / im)))); elseif (re <= 7e-36) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.75e+50) tmp = 0.5 * sqrt((im / (-re / im))); elseif (re <= 7e-36) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.75e+50], N[(0.5 * N[Sqrt[N[(im / N[((-re) / im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 7e-36], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.75 \cdot 10^{+50}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im}{\frac{-re}{im}}}\\
\mathbf{elif}\;re \leq 7 \cdot 10^{-36}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -3.75e50Initial program 8.5%
+-commutative8.5%
hypot-def25.1%
Simplified25.1%
Taylor expanded in re around -inf 59.3%
*-commutative59.3%
unpow259.3%
Simplified59.3%
add-log-exp21.8%
*-un-lft-identity21.8%
log-prod21.8%
metadata-eval21.8%
add-log-exp59.3%
*-commutative59.3%
associate-*l*59.3%
associate-/l*68.4%
associate-/r/68.2%
metadata-eval68.2%
Applied egg-rr68.2%
+-lft-identity68.2%
*-commutative68.2%
metadata-eval68.2%
associate-*l/59.3%
times-frac59.3%
neg-mul-159.3%
*-lft-identity59.3%
associate-/l*68.4%
distribute-frac-neg68.4%
Simplified68.4%
if -3.75e50 < re < 6.9999999999999999e-36Initial program 54.1%
+-commutative54.1%
hypot-def88.3%
Simplified88.3%
Taylor expanded in re around 0 35.2%
if 6.9999999999999999e-36 < re Initial program 41.6%
+-commutative41.6%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 79.7%
unpow279.7%
rem-square-sqrt81.2%
Simplified81.2%
Final simplification53.7%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* im 2.0))))
double code(double re, double im) {
return 0.5 * sqrt((im * 2.0));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((im * 2.0d0))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((im * 2.0));
}
def code(re, im): return 0.5 * math.sqrt((im * 2.0))
function code(re, im) return Float64(0.5 * sqrt(Float64(im * 2.0))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((im * 2.0)); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{im \cdot 2}
\end{array}
Initial program 41.3%
+-commutative41.3%
hypot-def77.9%
Simplified77.9%
Taylor expanded in re around 0 21.4%
Final simplification21.4%
(FPCore (re im)
:precision binary64
(let* ((t_0 (sqrt (+ (* re re) (* im im)))))
(if (< re 0.0)
(* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- t_0 re)))))
(* 0.5 (sqrt (* 2.0 (+ t_0 re)))))))
double code(double re, double im) {
double t_0 = sqrt(((re * re) + (im * im)));
double tmp;
if (re < 0.0) {
tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
} else {
tmp = 0.5 * sqrt((2.0 * (t_0 + re)));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt(((re * re) + (im * im)))
if (re < 0.0d0) then
tmp = 0.5d0 * (sqrt(2.0d0) * sqrt(((im * im) / (t_0 - re))))
else
tmp = 0.5d0 * sqrt((2.0d0 * (t_0 + re)))
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = Math.sqrt(((re * re) + (im * im)));
double tmp;
if (re < 0.0) {
tmp = 0.5 * (Math.sqrt(2.0) * Math.sqrt(((im * im) / (t_0 - re))));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (t_0 + re)));
}
return tmp;
}
def code(re, im): t_0 = math.sqrt(((re * re) + (im * im))) tmp = 0 if re < 0.0: tmp = 0.5 * (math.sqrt(2.0) * math.sqrt(((im * im) / (t_0 - re)))) else: tmp = 0.5 * math.sqrt((2.0 * (t_0 + re))) return tmp
function code(re, im) t_0 = sqrt(Float64(Float64(re * re) + Float64(im * im))) tmp = 0.0 if (re < 0.0) tmp = Float64(0.5 * Float64(sqrt(2.0) * sqrt(Float64(Float64(im * im) / Float64(t_0 - re))))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(t_0 + re)))); end return tmp end
function tmp_2 = code(re, im) t_0 = sqrt(((re * re) + (im * im))); tmp = 0.0; if (re < 0.0) tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re)))); else tmp = 0.5 * sqrt((2.0 * (t_0 + re))); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[Less[re, 0.0], N[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[N[(N[(im * im), $MachinePrecision] / N[(t$95$0 - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(t$95$0 + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{re \cdot re + im \cdot im}\\
\mathbf{if}\;re < 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t_0 - re}}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t_0 + re\right)}\\
\end{array}
\end{array}
herbie shell --seed 2023222
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:herbie-target
(if (< re 0.0) (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))