
(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 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0) (* 0.5 (sqrt (* im (* im (/ -1.0 re))))) (sqrt (* 0.5 (+ re (hypot re im))))))
double code(double re, double im) {
double tmp;
if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) {
tmp = 0.5 * sqrt((im * (im * (-1.0 / re))));
} else {
tmp = sqrt((0.5 * (re + hypot(re, im))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (Math.sqrt((2.0 * (re + Math.sqrt(((re * re) + (im * im)))))) <= 0.0) {
tmp = 0.5 * Math.sqrt((im * (im * (-1.0 / re))));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
return tmp;
}
def code(re, im): tmp = 0 if math.sqrt((2.0 * (re + math.sqrt(((re * re) + (im * im)))))) <= 0.0: tmp = 0.5 * math.sqrt((im * (im * (-1.0 / re)))) else: tmp = math.sqrt((0.5 * (re + math.hypot(re, im)))) return tmp
function code(re, im) tmp = 0.0 if (sqrt(Float64(2.0 * Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))))) <= 0.0) tmp = Float64(0.5 * sqrt(Float64(im * Float64(im * Float64(-1.0 / re))))); else tmp = sqrt(Float64(0.5 * Float64(re + hypot(re, im)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) tmp = 0.5 * sqrt((im * (im * (-1.0 / re)))); else tmp = sqrt((0.5 * (re + hypot(re, im)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[N[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(0.5 * N[Sqrt[N[(im * N[(im * N[(-1.0 / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \left(im \cdot \frac{-1}{re}\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 8.3%
sqr-neg8.3%
+-commutative8.3%
sqr-neg8.3%
+-commutative8.3%
distribute-rgt-in8.3%
cancel-sign-sub8.3%
distribute-rgt-out--8.3%
sub-neg8.3%
remove-double-neg8.3%
+-commutative8.3%
Simplified8.3%
Taylor expanded in re around -inf 45.1%
mul-1-neg45.1%
Simplified45.1%
div-inv45.1%
unpow245.1%
associate-*l*51.0%
Applied egg-rr51.0%
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 42.8%
sqr-neg42.8%
+-commutative42.8%
sqr-neg42.8%
+-commutative42.8%
distribute-rgt-in42.8%
cancel-sign-sub42.8%
distribute-rgt-out--42.8%
sub-neg42.8%
remove-double-neg42.8%
+-commutative42.8%
Simplified87.5%
add-sqr-sqrt86.8%
sqrt-unprod87.5%
*-commutative87.5%
*-commutative87.5%
swap-sqr87.5%
add-sqr-sqrt87.5%
metadata-eval87.5%
Applied egg-rr87.5%
*-commutative87.5%
associate-*r*88.4%
metadata-eval88.4%
Simplified88.4%
Final simplification82.7%
(FPCore (re im) :precision binary64 (if (<= re -8.8e-66) (+ (+ 1.0 (sqrt (* (* 2.0 im) 0.25))) -1.0) (if (<= re 3.5e+54) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -8.8e-66) {
tmp = (1.0 + sqrt(((2.0 * im) * 0.25))) + -1.0;
} else if (re <= 3.5e+54) {
tmp = 0.5 * sqrt((2.0 * (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 <= (-8.8d-66)) then
tmp = (1.0d0 + sqrt(((2.0d0 * im) * 0.25d0))) + (-1.0d0)
else if (re <= 3.5d+54) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -8.8e-66) {
tmp = (1.0 + Math.sqrt(((2.0 * im) * 0.25))) + -1.0;
} else if (re <= 3.5e+54) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -8.8e-66: tmp = (1.0 + math.sqrt(((2.0 * im) * 0.25))) + -1.0 elif re <= 3.5e+54: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -8.8e-66) tmp = Float64(Float64(1.0 + sqrt(Float64(Float64(2.0 * im) * 0.25))) + -1.0); elseif (re <= 3.5e+54) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -8.8e-66) tmp = (1.0 + sqrt(((2.0 * im) * 0.25))) + -1.0; elseif (re <= 3.5e+54) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -8.8e-66], N[(N[(1.0 + N[Sqrt[N[(N[(2.0 * im), $MachinePrecision] * 0.25), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], If[LessEqual[re, 3.5e+54], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -8.8 \cdot 10^{-66}:\\
\;\;\;\;\left(1 + \sqrt{\left(2 \cdot im\right) \cdot 0.25}\right) + -1\\
\mathbf{elif}\;re \leq 3.5 \cdot 10^{+54}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -8.8000000000000004e-66Initial program 13.5%
sqr-neg13.5%
+-commutative13.5%
sqr-neg13.5%
+-commutative13.5%
distribute-rgt-in13.5%
cancel-sign-sub13.5%
distribute-rgt-out--13.5%
sub-neg13.5%
remove-double-neg13.5%
+-commutative13.5%
Simplified38.0%
Taylor expanded in re around 0 14.2%
associate-*r*14.2%
Simplified14.2%
expm1-log1p-u13.3%
associate-*l*13.3%
sqrt-unprod13.3%
*-commutative13.3%
Applied egg-rr13.3%
expm1-udef21.2%
log1p-udef21.2%
rem-exp-log22.3%
+-commutative22.3%
add-sqr-sqrt22.2%
sqrt-unprod22.3%
*-commutative22.3%
*-commutative22.3%
swap-sqr22.3%
add-sqr-sqrt22.3%
*-commutative22.3%
metadata-eval22.3%
Applied egg-rr22.3%
if -8.8000000000000004e-66 < re < 3.5000000000000001e54Initial program 55.6%
sqr-neg55.6%
+-commutative55.6%
sqr-neg55.6%
+-commutative55.6%
distribute-rgt-in55.6%
cancel-sign-sub55.6%
distribute-rgt-out--55.6%
sub-neg55.6%
remove-double-neg55.6%
+-commutative55.6%
Simplified91.5%
Taylor expanded in re around 0 39.1%
if 3.5000000000000001e54 < re Initial program 37.2%
sqr-neg37.2%
+-commutative37.2%
sqr-neg37.2%
+-commutative37.2%
distribute-rgt-in37.2%
cancel-sign-sub37.2%
distribute-rgt-out--37.2%
sub-neg37.2%
remove-double-neg37.2%
+-commutative37.2%
Simplified96.9%
Taylor expanded in im around 0 77.2%
*-commutative77.2%
unpow277.2%
rem-square-sqrt78.7%
associate-*r*78.7%
metadata-eval78.7%
*-lft-identity78.7%
Simplified78.7%
Final simplification43.1%
(FPCore (re im) :precision binary64 (if (<= re -2.1e+43) (* 0.5 (sqrt (* im (* im (/ -1.0 re))))) (if (<= re 3.3e+54) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -2.1e+43) {
tmp = 0.5 * sqrt((im * (im * (-1.0 / re))));
} else if (re <= 3.3e+54) {
tmp = 0.5 * sqrt((2.0 * (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 <= (-2.1d+43)) then
tmp = 0.5d0 * sqrt((im * (im * ((-1.0d0) / re))))
else if (re <= 3.3d+54) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.1e+43) {
tmp = 0.5 * Math.sqrt((im * (im * (-1.0 / re))));
} else if (re <= 3.3e+54) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.1e+43: tmp = 0.5 * math.sqrt((im * (im * (-1.0 / re)))) elif re <= 3.3e+54: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.1e+43) tmp = Float64(0.5 * sqrt(Float64(im * Float64(im * Float64(-1.0 / re))))); elseif (re <= 3.3e+54) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.1e+43) tmp = 0.5 * sqrt((im * (im * (-1.0 / re)))); elseif (re <= 3.3e+54) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.1e+43], N[(0.5 * N[Sqrt[N[(im * N[(im * N[(-1.0 / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.3e+54], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.1 \cdot 10^{+43}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \left(im \cdot \frac{-1}{re}\right)}\\
\mathbf{elif}\;re \leq 3.3 \cdot 10^{+54}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -2.10000000000000002e43Initial program 7.1%
sqr-neg7.1%
+-commutative7.1%
sqr-neg7.1%
+-commutative7.1%
distribute-rgt-in7.1%
cancel-sign-sub7.1%
distribute-rgt-out--7.1%
sub-neg7.1%
remove-double-neg7.1%
+-commutative7.1%
Simplified35.5%
Taylor expanded in re around -inf 55.7%
mul-1-neg55.7%
Simplified55.7%
div-inv55.6%
unpow255.6%
associate-*l*60.9%
Applied egg-rr60.9%
if -2.10000000000000002e43 < re < 3.3e54Initial program 50.6%
sqr-neg50.6%
+-commutative50.6%
sqr-neg50.6%
+-commutative50.6%
distribute-rgt-in50.6%
cancel-sign-sub50.6%
distribute-rgt-out--50.6%
sub-neg50.6%
remove-double-neg50.6%
+-commutative50.6%
Simplified82.8%
Taylor expanded in re around 0 36.4%
if 3.3e54 < re Initial program 37.2%
sqr-neg37.2%
+-commutative37.2%
sqr-neg37.2%
+-commutative37.2%
distribute-rgt-in37.2%
cancel-sign-sub37.2%
distribute-rgt-out--37.2%
sub-neg37.2%
remove-double-neg37.2%
+-commutative37.2%
Simplified96.9%
Taylor expanded in im around 0 77.2%
*-commutative77.2%
unpow277.2%
rem-square-sqrt78.7%
associate-*r*78.7%
metadata-eval78.7%
*-lft-identity78.7%
Simplified78.7%
Final simplification52.0%
(FPCore (re im) :precision binary64 (if (<= re -5e+41) (* 0.5 (sqrt (* im (/ (- im) re)))) (if (<= re 3.5e+54) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -5e+41) {
tmp = 0.5 * sqrt((im * (-im / re)));
} else if (re <= 3.5e+54) {
tmp = 0.5 * sqrt((2.0 * (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 <= (-5d+41)) then
tmp = 0.5d0 * sqrt((im * (-im / re)))
else if (re <= 3.5d+54) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -5e+41) {
tmp = 0.5 * Math.sqrt((im * (-im / re)));
} else if (re <= 3.5e+54) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -5e+41: tmp = 0.5 * math.sqrt((im * (-im / re))) elif re <= 3.5e+54: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -5e+41) tmp = Float64(0.5 * sqrt(Float64(im * Float64(Float64(-im) / re)))); elseif (re <= 3.5e+54) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -5e+41) tmp = 0.5 * sqrt((im * (-im / re))); elseif (re <= 3.5e+54) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -5e+41], N[(0.5 * N[Sqrt[N[(im * N[((-im) / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.5e+54], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -5 \cdot 10^{+41}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\
\mathbf{elif}\;re \leq 3.5 \cdot 10^{+54}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -5.00000000000000022e41Initial program 7.1%
sqr-neg7.1%
+-commutative7.1%
sqr-neg7.1%
+-commutative7.1%
distribute-rgt-in7.1%
cancel-sign-sub7.1%
distribute-rgt-out--7.1%
sub-neg7.1%
remove-double-neg7.1%
+-commutative7.1%
Simplified35.5%
Taylor expanded in re around -inf 55.7%
mul-1-neg55.7%
Simplified55.7%
unpow255.7%
*-un-lft-identity55.7%
times-frac61.0%
Applied egg-rr61.0%
if -5.00000000000000022e41 < re < 3.5000000000000001e54Initial program 50.6%
sqr-neg50.6%
+-commutative50.6%
sqr-neg50.6%
+-commutative50.6%
distribute-rgt-in50.6%
cancel-sign-sub50.6%
distribute-rgt-out--50.6%
sub-neg50.6%
remove-double-neg50.6%
+-commutative50.6%
Simplified82.8%
Taylor expanded in re around 0 36.4%
if 3.5000000000000001e54 < re Initial program 37.2%
sqr-neg37.2%
+-commutative37.2%
sqr-neg37.2%
+-commutative37.2%
distribute-rgt-in37.2%
cancel-sign-sub37.2%
distribute-rgt-out--37.2%
sub-neg37.2%
remove-double-neg37.2%
+-commutative37.2%
Simplified96.9%
Taylor expanded in im around 0 77.2%
*-commutative77.2%
unpow277.2%
rem-square-sqrt78.7%
associate-*r*78.7%
metadata-eval78.7%
*-lft-identity78.7%
Simplified78.7%
Final simplification52.0%
(FPCore (re im) :precision binary64 (if (<= re -9.4e+176) (* 0.5 (sqrt (* 2.0 (- re re)))) (if (<= re 5.4e+53) (sqrt (* im 0.5)) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -9.4e+176) {
tmp = 0.5 * sqrt((2.0 * (re - re)));
} else if (re <= 5.4e+53) {
tmp = sqrt((im * 0.5));
} 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 <= (-9.4d+176)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re - re)))
else if (re <= 5.4d+53) then
tmp = sqrt((im * 0.5d0))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -9.4e+176) {
tmp = 0.5 * Math.sqrt((2.0 * (re - re)));
} else if (re <= 5.4e+53) {
tmp = Math.sqrt((im * 0.5));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -9.4e+176: tmp = 0.5 * math.sqrt((2.0 * (re - re))) elif re <= 5.4e+53: tmp = math.sqrt((im * 0.5)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -9.4e+176) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - re)))); elseif (re <= 5.4e+53) tmp = sqrt(Float64(im * 0.5)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -9.4e+176) tmp = 0.5 * sqrt((2.0 * (re - re))); elseif (re <= 5.4e+53) tmp = sqrt((im * 0.5)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -9.4e+176], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5.4e+53], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -9.4 \cdot 10^{+176}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - re\right)}\\
\mathbf{elif}\;re \leq 5.4 \cdot 10^{+53}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -9.39999999999999962e176Initial program 2.4%
Taylor expanded in re around -inf 31.7%
mul-1-neg31.7%
Simplified31.7%
if -9.39999999999999962e176 < re < 5.40000000000000039e53Initial program 44.0%
sqr-neg44.0%
+-commutative44.0%
sqr-neg44.0%
+-commutative44.0%
distribute-rgt-in44.0%
cancel-sign-sub44.0%
distribute-rgt-out--44.0%
sub-neg44.0%
remove-double-neg44.0%
+-commutative44.0%
Simplified74.0%
Taylor expanded in re around 0 31.6%
associate-*r*31.6%
Simplified31.6%
add-sqr-sqrt31.5%
sqrt-unprod31.6%
*-commutative31.6%
*-commutative31.6%
swap-sqr31.5%
rem-square-sqrt31.7%
*-commutative31.7%
*-commutative31.7%
swap-sqr31.7%
add-sqr-sqrt31.8%
metadata-eval31.8%
Applied egg-rr31.8%
*-commutative31.8%
associate-*r*31.8%
metadata-eval31.8%
Simplified31.8%
if 5.40000000000000039e53 < re Initial program 37.7%
sqr-neg37.7%
+-commutative37.7%
sqr-neg37.7%
+-commutative37.7%
distribute-rgt-in37.7%
cancel-sign-sub37.7%
distribute-rgt-out--37.7%
sub-neg37.7%
remove-double-neg37.7%
+-commutative37.7%
Simplified97.0%
Taylor expanded in im around 0 76.5%
*-commutative76.5%
unpow276.5%
rem-square-sqrt77.9%
associate-*r*77.9%
metadata-eval77.9%
*-lft-identity77.9%
Simplified77.9%
Final simplification43.1%
(FPCore (re im) :precision binary64 (if (<= re 6.5e+53) (sqrt (* im 0.5)) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= 6.5e+53) {
tmp = sqrt((im * 0.5));
} 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 <= 6.5d+53) then
tmp = sqrt((im * 0.5d0))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 6.5e+53) {
tmp = Math.sqrt((im * 0.5));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 6.5e+53: tmp = math.sqrt((im * 0.5)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 6.5e+53) tmp = sqrt(Float64(im * 0.5)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 6.5e+53) tmp = sqrt((im * 0.5)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 6.5e+53], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 6.5 \cdot 10^{+53}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 6.50000000000000017e53Initial program 37.5%
sqr-neg37.5%
+-commutative37.5%
sqr-neg37.5%
+-commutative37.5%
distribute-rgt-in37.5%
cancel-sign-sub37.5%
distribute-rgt-out--37.5%
sub-neg37.5%
remove-double-neg37.5%
+-commutative37.5%
Simplified68.4%
Taylor expanded in re around 0 27.2%
associate-*r*27.2%
Simplified27.2%
add-sqr-sqrt27.0%
sqrt-unprod27.2%
*-commutative27.2%
*-commutative27.2%
swap-sqr27.1%
rem-square-sqrt27.2%
*-commutative27.2%
*-commutative27.2%
swap-sqr27.2%
add-sqr-sqrt27.3%
metadata-eval27.3%
Applied egg-rr27.3%
*-commutative27.3%
associate-*r*27.3%
metadata-eval27.3%
Simplified27.3%
if 6.50000000000000017e53 < re Initial program 37.7%
sqr-neg37.7%
+-commutative37.7%
sqr-neg37.7%
+-commutative37.7%
distribute-rgt-in37.7%
cancel-sign-sub37.7%
distribute-rgt-out--37.7%
sub-neg37.7%
remove-double-neg37.7%
+-commutative37.7%
Simplified97.0%
Taylor expanded in im around 0 76.5%
*-commutative76.5%
unpow276.5%
rem-square-sqrt77.9%
associate-*r*77.9%
metadata-eval77.9%
*-lft-identity77.9%
Simplified77.9%
Final simplification39.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 37.5%
sqr-neg37.5%
+-commutative37.5%
sqr-neg37.5%
+-commutative37.5%
distribute-rgt-in37.5%
cancel-sign-sub37.5%
distribute-rgt-out--37.5%
sub-neg37.5%
remove-double-neg37.5%
+-commutative37.5%
Simplified75.4%
Taylor expanded in im around 0 24.9%
*-commutative24.9%
unpow224.9%
rem-square-sqrt25.4%
associate-*r*25.4%
metadata-eval25.4%
*-lft-identity25.4%
Simplified25.4%
Final simplification25.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 2024027
(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)))))