
(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 -3.9e+145) (sqrt (* 0.5 (* -0.5 (* im (/ im re))))) (sqrt (* 0.5 (+ re (hypot im re))))))
double code(double re, double im) {
double tmp;
if (re <= -3.9e+145) {
tmp = sqrt((0.5 * (-0.5 * (im * (im / re)))));
} else {
tmp = sqrt((0.5 * (re + hypot(im, re))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= -3.9e+145) {
tmp = Math.sqrt((0.5 * (-0.5 * (im * (im / re)))));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(im, re))));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.9e+145: tmp = math.sqrt((0.5 * (-0.5 * (im * (im / re))))) else: tmp = math.sqrt((0.5 * (re + math.hypot(im, re)))) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.9e+145) tmp = sqrt(Float64(0.5 * Float64(-0.5 * Float64(im * Float64(im / re))))); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(im, re)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.9e+145) tmp = sqrt((0.5 * (-0.5 * (im * (im / re))))); else tmp = sqrt((0.5 * (re + hypot(im, re)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.9e+145], N[Sqrt[N[(0.5 * N[(-0.5 * N[(im * N[(im / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[im ^ 2 + re ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.9 \cdot 10^{+145}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(-0.5 \cdot \left(im \cdot \frac{im}{re}\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(im, re\right)\right)}\\
\end{array}
\end{array}
if re < -3.8999999999999998e145Initial program 2.8%
sqr-neg2.8%
+-commutative2.8%
sqr-neg2.8%
+-commutative2.8%
distribute-rgt-in2.8%
cancel-sign-sub2.8%
distribute-rgt-out--2.8%
sub-neg2.8%
remove-double-neg2.8%
+-commutative2.8%
hypot-define32.8%
Simplified32.8%
*-commutative32.8%
hypot-define2.8%
+-commutative2.8%
*-commutative2.8%
add-sqr-sqrt2.8%
sqrt-unprod2.8%
*-commutative2.8%
*-commutative2.8%
swap-sqr2.8%
Applied egg-rr32.8%
*-commutative32.8%
associate-*r*32.8%
metadata-eval32.8%
hypot-undefine2.8%
unpow22.8%
unpow22.8%
+-commutative2.8%
unpow22.8%
unpow22.8%
hypot-undefine32.8%
Simplified32.8%
Taylor expanded in re around -inf 55.5%
unpow255.5%
associate-/l*65.9%
Applied egg-rr65.9%
if -3.8999999999999998e145 < re Initial program 47.6%
sqr-neg47.6%
+-commutative47.6%
sqr-neg47.6%
+-commutative47.6%
distribute-rgt-in47.6%
cancel-sign-sub47.6%
distribute-rgt-out--47.6%
sub-neg47.6%
remove-double-neg47.6%
+-commutative47.6%
hypot-define89.1%
Simplified89.1%
*-commutative89.1%
hypot-define47.6%
+-commutative47.6%
*-commutative47.6%
add-sqr-sqrt47.3%
sqrt-unprod47.6%
*-commutative47.6%
*-commutative47.6%
swap-sqr47.6%
Applied egg-rr89.1%
*-commutative89.1%
associate-*r*89.1%
metadata-eval89.1%
hypot-undefine47.6%
unpow247.6%
unpow247.6%
+-commutative47.6%
unpow247.6%
unpow247.6%
hypot-undefine89.1%
Simplified89.1%
Final simplification85.6%
(FPCore (re im)
:precision binary64
(if (<= re -1.05e+142)
(sqrt (* 0.5 (* -0.5 (* im (/ im re)))))
(if (<= re 1.16e-5)
(sqrt (* 0.5 (+ im (* re (+ 1.0 (* 0.5 (/ re im)))))))
(sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -1.05e+142) {
tmp = sqrt((0.5 * (-0.5 * (im * (im / re)))));
} else if (re <= 1.16e-5) {
tmp = sqrt((0.5 * (im + (re * (1.0 + (0.5 * (re / im)))))));
} 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 <= (-1.05d+142)) then
tmp = sqrt((0.5d0 * ((-0.5d0) * (im * (im / re)))))
else if (re <= 1.16d-5) then
tmp = sqrt((0.5d0 * (im + (re * (1.0d0 + (0.5d0 * (re / im)))))))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.05e+142) {
tmp = Math.sqrt((0.5 * (-0.5 * (im * (im / re)))));
} else if (re <= 1.16e-5) {
tmp = Math.sqrt((0.5 * (im + (re * (1.0 + (0.5 * (re / im)))))));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.05e+142: tmp = math.sqrt((0.5 * (-0.5 * (im * (im / re))))) elif re <= 1.16e-5: tmp = math.sqrt((0.5 * (im + (re * (1.0 + (0.5 * (re / im))))))) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.05e+142) tmp = sqrt(Float64(0.5 * Float64(-0.5 * Float64(im * Float64(im / re))))); elseif (re <= 1.16e-5) tmp = sqrt(Float64(0.5 * Float64(im + Float64(re * Float64(1.0 + Float64(0.5 * Float64(re / im))))))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.05e+142) tmp = sqrt((0.5 * (-0.5 * (im * (im / re))))); elseif (re <= 1.16e-5) tmp = sqrt((0.5 * (im + (re * (1.0 + (0.5 * (re / im))))))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.05e+142], N[Sqrt[N[(0.5 * N[(-0.5 * N[(im * N[(im / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[re, 1.16e-5], N[Sqrt[N[(0.5 * N[(im + N[(re * N[(1.0 + N[(0.5 * N[(re / im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.05 \cdot 10^{+142}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(-0.5 \cdot \left(im \cdot \frac{im}{re}\right)\right)}\\
\mathbf{elif}\;re \leq 1.16 \cdot 10^{-5}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im + re \cdot \left(1 + 0.5 \cdot \frac{re}{im}\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -1.05e142Initial program 2.8%
sqr-neg2.8%
+-commutative2.8%
sqr-neg2.8%
+-commutative2.8%
distribute-rgt-in2.8%
cancel-sign-sub2.8%
distribute-rgt-out--2.8%
sub-neg2.8%
remove-double-neg2.8%
+-commutative2.8%
hypot-define32.8%
Simplified32.8%
*-commutative32.8%
hypot-define2.8%
+-commutative2.8%
*-commutative2.8%
add-sqr-sqrt2.8%
sqrt-unprod2.8%
*-commutative2.8%
*-commutative2.8%
swap-sqr2.8%
Applied egg-rr32.8%
*-commutative32.8%
associate-*r*32.8%
metadata-eval32.8%
hypot-undefine2.8%
unpow22.8%
unpow22.8%
+-commutative2.8%
unpow22.8%
unpow22.8%
hypot-undefine32.8%
Simplified32.8%
Taylor expanded in re around -inf 55.5%
unpow255.5%
associate-/l*65.9%
Applied egg-rr65.9%
if -1.05e142 < re < 1.1600000000000001e-5Initial program 54.7%
sqr-neg54.7%
+-commutative54.7%
sqr-neg54.7%
+-commutative54.7%
distribute-rgt-in54.7%
cancel-sign-sub54.7%
distribute-rgt-out--54.7%
sub-neg54.7%
remove-double-neg54.7%
+-commutative54.7%
hypot-define83.2%
Simplified83.2%
*-commutative83.2%
hypot-define54.7%
+-commutative54.7%
*-commutative54.7%
add-sqr-sqrt54.3%
sqrt-unprod54.7%
*-commutative54.7%
*-commutative54.7%
swap-sqr54.7%
Applied egg-rr83.2%
*-commutative83.2%
associate-*r*83.2%
metadata-eval83.2%
hypot-undefine54.7%
unpow254.7%
unpow254.7%
+-commutative54.7%
unpow254.7%
unpow254.7%
hypot-undefine83.2%
Simplified83.2%
Taylor expanded in re around 0 34.2%
if 1.1600000000000001e-5 < re Initial program 34.5%
sqr-neg34.5%
+-commutative34.5%
sqr-neg34.5%
+-commutative34.5%
distribute-rgt-in34.5%
cancel-sign-sub34.5%
distribute-rgt-out--34.5%
sub-neg34.5%
remove-double-neg34.5%
+-commutative34.5%
hypot-define100.0%
Simplified100.0%
Taylor expanded in im around 0 76.6%
*-commutative76.6%
unpow276.6%
rem-square-sqrt78.0%
associate-*r*78.0%
metadata-eval78.0%
*-lft-identity78.0%
Simplified78.0%
Final simplification51.9%
(FPCore (re im) :precision binary64 (if (<= re -9e+162) (* 0.5 (sqrt (* 2.0 (- re re)))) (if (<= re 1.5e-5) (sqrt (* 0.5 (+ re im))) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -9e+162) {
tmp = 0.5 * sqrt((2.0 * (re - re)));
} else if (re <= 1.5e-5) {
tmp = sqrt((0.5 * (re + im)));
} 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 <= (-9d+162)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re - re)))
else if (re <= 1.5d-5) then
tmp = sqrt((0.5d0 * (re + im)))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -9e+162) {
tmp = 0.5 * Math.sqrt((2.0 * (re - re)));
} else if (re <= 1.5e-5) {
tmp = Math.sqrt((0.5 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -9e+162: tmp = 0.5 * math.sqrt((2.0 * (re - re))) elif re <= 1.5e-5: tmp = math.sqrt((0.5 * (re + im))) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -9e+162) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - re)))); elseif (re <= 1.5e-5) tmp = sqrt(Float64(0.5 * Float64(re + im))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -9e+162) tmp = 0.5 * sqrt((2.0 * (re - re))); elseif (re <= 1.5e-5) tmp = sqrt((0.5 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -9e+162], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.5e-5], N[Sqrt[N[(0.5 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -9 \cdot 10^{+162}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - re\right)}\\
\mathbf{elif}\;re \leq 1.5 \cdot 10^{-5}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -8.99999999999999944e162Initial program 2.5%
Taylor expanded in re around -inf 24.7%
mul-1-neg24.7%
Simplified24.7%
if -8.99999999999999944e162 < re < 1.50000000000000004e-5Initial program 53.3%
sqr-neg53.3%
+-commutative53.3%
sqr-neg53.3%
+-commutative53.3%
distribute-rgt-in53.3%
cancel-sign-sub53.3%
distribute-rgt-out--53.3%
sub-neg53.3%
remove-double-neg53.3%
+-commutative53.3%
hypot-define81.7%
Simplified81.7%
*-commutative81.7%
hypot-define53.3%
+-commutative53.3%
*-commutative53.3%
add-sqr-sqrt52.9%
sqrt-unprod53.3%
*-commutative53.3%
*-commutative53.3%
swap-sqr53.3%
Applied egg-rr81.7%
*-commutative81.7%
associate-*r*81.7%
metadata-eval81.7%
hypot-undefine53.3%
unpow253.3%
unpow253.3%
+-commutative53.3%
unpow253.3%
unpow253.3%
hypot-undefine81.7%
Simplified81.7%
Taylor expanded in re around 0 35.0%
if 1.50000000000000004e-5 < re Initial program 34.5%
sqr-neg34.5%
+-commutative34.5%
sqr-neg34.5%
+-commutative34.5%
distribute-rgt-in34.5%
cancel-sign-sub34.5%
distribute-rgt-out--34.5%
sub-neg34.5%
remove-double-neg34.5%
+-commutative34.5%
hypot-define100.0%
Simplified100.0%
Taylor expanded in im around 0 76.6%
*-commutative76.6%
unpow276.6%
rem-square-sqrt78.0%
associate-*r*78.0%
metadata-eval78.0%
*-lft-identity78.0%
Simplified78.0%
Final simplification46.4%
(FPCore (re im) :precision binary64 (if (<= re -1.05e+142) (sqrt (* 0.5 (* -0.5 (* im (/ im re))))) (if (<= re 3.6e-6) (sqrt (* 0.5 (+ re im))) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -1.05e+142) {
tmp = sqrt((0.5 * (-0.5 * (im * (im / re)))));
} else if (re <= 3.6e-6) {
tmp = sqrt((0.5 * (re + im)));
} 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 <= (-1.05d+142)) then
tmp = sqrt((0.5d0 * ((-0.5d0) * (im * (im / re)))))
else if (re <= 3.6d-6) then
tmp = sqrt((0.5d0 * (re + im)))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.05e+142) {
tmp = Math.sqrt((0.5 * (-0.5 * (im * (im / re)))));
} else if (re <= 3.6e-6) {
tmp = Math.sqrt((0.5 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.05e+142: tmp = math.sqrt((0.5 * (-0.5 * (im * (im / re))))) elif re <= 3.6e-6: tmp = math.sqrt((0.5 * (re + im))) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.05e+142) tmp = sqrt(Float64(0.5 * Float64(-0.5 * Float64(im * Float64(im / re))))); elseif (re <= 3.6e-6) tmp = sqrt(Float64(0.5 * Float64(re + im))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.05e+142) tmp = sqrt((0.5 * (-0.5 * (im * (im / re))))); elseif (re <= 3.6e-6) tmp = sqrt((0.5 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.05e+142], N[Sqrt[N[(0.5 * N[(-0.5 * N[(im * N[(im / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[re, 3.6e-6], N[Sqrt[N[(0.5 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.05 \cdot 10^{+142}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(-0.5 \cdot \left(im \cdot \frac{im}{re}\right)\right)}\\
\mathbf{elif}\;re \leq 3.6 \cdot 10^{-6}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -1.05e142Initial program 2.8%
sqr-neg2.8%
+-commutative2.8%
sqr-neg2.8%
+-commutative2.8%
distribute-rgt-in2.8%
cancel-sign-sub2.8%
distribute-rgt-out--2.8%
sub-neg2.8%
remove-double-neg2.8%
+-commutative2.8%
hypot-define32.8%
Simplified32.8%
*-commutative32.8%
hypot-define2.8%
+-commutative2.8%
*-commutative2.8%
add-sqr-sqrt2.8%
sqrt-unprod2.8%
*-commutative2.8%
*-commutative2.8%
swap-sqr2.8%
Applied egg-rr32.8%
*-commutative32.8%
associate-*r*32.8%
metadata-eval32.8%
hypot-undefine2.8%
unpow22.8%
unpow22.8%
+-commutative2.8%
unpow22.8%
unpow22.8%
hypot-undefine32.8%
Simplified32.8%
Taylor expanded in re around -inf 55.5%
unpow255.5%
associate-/l*65.9%
Applied egg-rr65.9%
if -1.05e142 < re < 3.59999999999999984e-6Initial program 54.7%
sqr-neg54.7%
+-commutative54.7%
sqr-neg54.7%
+-commutative54.7%
distribute-rgt-in54.7%
cancel-sign-sub54.7%
distribute-rgt-out--54.7%
sub-neg54.7%
remove-double-neg54.7%
+-commutative54.7%
hypot-define83.2%
Simplified83.2%
*-commutative83.2%
hypot-define54.7%
+-commutative54.7%
*-commutative54.7%
add-sqr-sqrt54.3%
sqrt-unprod54.7%
*-commutative54.7%
*-commutative54.7%
swap-sqr54.7%
Applied egg-rr83.2%
*-commutative83.2%
associate-*r*83.2%
metadata-eval83.2%
hypot-undefine54.7%
unpow254.7%
unpow254.7%
+-commutative54.7%
unpow254.7%
unpow254.7%
hypot-undefine83.2%
Simplified83.2%
Taylor expanded in re around 0 35.3%
if 3.59999999999999984e-6 < re Initial program 34.5%
sqr-neg34.5%
+-commutative34.5%
sqr-neg34.5%
+-commutative34.5%
distribute-rgt-in34.5%
cancel-sign-sub34.5%
distribute-rgt-out--34.5%
sub-neg34.5%
remove-double-neg34.5%
+-commutative34.5%
hypot-define100.0%
Simplified100.0%
Taylor expanded in im around 0 76.6%
*-commutative76.6%
unpow276.6%
rem-square-sqrt78.0%
associate-*r*78.0%
metadata-eval78.0%
*-lft-identity78.0%
Simplified78.0%
Final simplification52.5%
(FPCore (re im) :precision binary64 (if (<= re 3.2e-8) (sqrt (* 0.5 im)) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= 3.2e-8) {
tmp = sqrt((0.5 * im));
} 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 <= 3.2d-8) then
tmp = sqrt((0.5d0 * im))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 3.2e-8) {
tmp = Math.sqrt((0.5 * im));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 3.2e-8: tmp = math.sqrt((0.5 * im)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 3.2e-8) tmp = sqrt(Float64(0.5 * im)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 3.2e-8) tmp = sqrt((0.5 * im)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 3.2e-8], N[Sqrt[N[(0.5 * im), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 3.2 \cdot 10^{-8}:\\
\;\;\;\;\sqrt{0.5 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 3.2000000000000002e-8Initial program 43.7%
sqr-neg43.7%
+-commutative43.7%
sqr-neg43.7%
+-commutative43.7%
distribute-rgt-in43.7%
cancel-sign-sub43.7%
distribute-rgt-out--43.7%
sub-neg43.7%
remove-double-neg43.7%
+-commutative43.7%
hypot-define72.6%
Simplified72.6%
*-commutative72.6%
hypot-define43.7%
+-commutative43.7%
*-commutative43.7%
add-sqr-sqrt43.4%
sqrt-unprod43.7%
*-commutative43.7%
*-commutative43.7%
swap-sqr43.7%
Applied egg-rr72.6%
*-commutative72.6%
associate-*r*72.6%
metadata-eval72.6%
hypot-undefine43.7%
unpow243.7%
unpow243.7%
+-commutative43.7%
unpow243.7%
unpow243.7%
hypot-undefine72.6%
Simplified72.6%
Taylor expanded in re around 0 29.4%
if 3.2000000000000002e-8 < re Initial program 34.5%
sqr-neg34.5%
+-commutative34.5%
sqr-neg34.5%
+-commutative34.5%
distribute-rgt-in34.5%
cancel-sign-sub34.5%
distribute-rgt-out--34.5%
sub-neg34.5%
remove-double-neg34.5%
+-commutative34.5%
hypot-define100.0%
Simplified100.0%
Taylor expanded in im around 0 76.6%
*-commutative76.6%
unpow276.6%
rem-square-sqrt78.0%
associate-*r*78.0%
metadata-eval78.0%
*-lft-identity78.0%
Simplified78.0%
Final simplification43.8%
(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.0%
sqr-neg41.0%
+-commutative41.0%
sqr-neg41.0%
+-commutative41.0%
distribute-rgt-in41.0%
cancel-sign-sub41.0%
distribute-rgt-out--41.0%
sub-neg41.0%
remove-double-neg41.0%
+-commutative41.0%
hypot-define80.7%
Simplified80.7%
Taylor expanded in im around 0 28.9%
*-commutative28.9%
unpow228.9%
rem-square-sqrt29.4%
associate-*r*29.4%
metadata-eval29.4%
*-lft-identity29.4%
Simplified29.4%
Final simplification29.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 2024044
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:alt
(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)))))