
(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 (exp (* 0.5 (+ (log (/ -1.0 re)) (log (pow im 2.0)))))) (* 0.5 (sqrt (* 2.0 (+ 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 * exp((0.5 * (log((-1.0 / re)) + log(pow(im, 2.0)))));
} else {
tmp = 0.5 * sqrt((2.0 * (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.exp((0.5 * (Math.log((-1.0 / re)) + Math.log(Math.pow(im, 2.0)))));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (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.exp((0.5 * (math.log((-1.0 / re)) + math.log(math.pow(im, 2.0))))) else: tmp = 0.5 * math.sqrt((2.0 * (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 * exp(Float64(0.5 * Float64(log(Float64(-1.0 / re)) + log((im ^ 2.0)))))); 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 (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) tmp = 0.5 * exp((0.5 * (log((-1.0 / re)) + log((im ^ 2.0))))); else tmp = 0.5 * sqrt((2.0 * (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[Exp[N[(0.5 * N[(N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision] + N[Log[N[Power[im, 2.0], $MachinePrecision]], $MachinePrecision]), $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}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot e^{0.5 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left({im}^{2}\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \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.8%
sqr-neg8.8%
+-commutative8.8%
sqr-neg8.8%
+-commutative8.8%
distribute-rgt-in8.8%
cancel-sign-sub8.8%
distribute-rgt-out--8.8%
sub-neg8.8%
remove-double-neg8.8%
+-commutative8.8%
hypot-define8.8%
Simplified8.8%
pow1/28.8%
hypot-define8.8%
+-commutative8.8%
pow-to-exp8.8%
+-commutative8.8%
hypot-define8.8%
Applied egg-rr8.8%
Taylor expanded in re around -inf 63.0%
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 42.3%
sqr-neg42.3%
+-commutative42.3%
sqr-neg42.3%
+-commutative42.3%
distribute-rgt-in42.3%
cancel-sign-sub42.3%
distribute-rgt-out--42.3%
sub-neg42.3%
remove-double-neg42.3%
+-commutative42.3%
hypot-define88.8%
Simplified88.8%
Final simplification86.4%
(FPCore (re im)
:precision binary64
(if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0)
(*
0.5
(sqrt (- (* (/ (/ (pow im 4.0) (pow re 2.0)) re) 0.25) (* im (/ im re)))))
(* 0.5 (sqrt (* 2.0 (+ 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(((((pow(im, 4.0) / pow(re, 2.0)) / re) * 0.25) - (im * (im / re))));
} else {
tmp = 0.5 * sqrt((2.0 * (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(((((Math.pow(im, 4.0) / Math.pow(re, 2.0)) / re) * 0.25) - (im * (im / re))));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (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(((((math.pow(im, 4.0) / math.pow(re, 2.0)) / re) * 0.25) - (im * (im / re)))) else: tmp = 0.5 * math.sqrt((2.0 * (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(Float64(Float64(Float64((im ^ 4.0) / (re ^ 2.0)) / re) * 0.25) - Float64(im * Float64(im / re))))); 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 (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) tmp = 0.5 * sqrt((((((im ^ 4.0) / (re ^ 2.0)) / re) * 0.25) - (im * (im / re)))); else tmp = 0.5 * sqrt((2.0 * (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[(N[(N[(N[(N[Power[im, 4.0], $MachinePrecision] / N[Power[re, 2.0], $MachinePrecision]), $MachinePrecision] / re), $MachinePrecision] * 0.25), $MachinePrecision] - N[(im * N[(im / re), $MachinePrecision]), $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}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{\frac{{im}^{4}}{{re}^{2}}}{re} \cdot 0.25 - im \cdot \frac{im}{re}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \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.8%
sqr-neg8.8%
+-commutative8.8%
sqr-neg8.8%
+-commutative8.8%
distribute-rgt-in8.8%
cancel-sign-sub8.8%
distribute-rgt-out--8.8%
sub-neg8.8%
remove-double-neg8.8%
+-commutative8.8%
hypot-define8.8%
Simplified8.8%
Taylor expanded in re around -inf 52.2%
+-commutative52.2%
mul-1-neg52.2%
unsub-neg52.2%
*-commutative52.2%
Simplified52.2%
unpow252.2%
associate-/l*56.2%
Applied egg-rr56.2%
*-un-lft-identity56.2%
cube-mult56.2%
times-frac57.8%
pow257.8%
Applied egg-rr57.8%
associate-*l/57.8%
*-lft-identity57.8%
Simplified57.8%
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 42.3%
sqr-neg42.3%
+-commutative42.3%
sqr-neg42.3%
+-commutative42.3%
distribute-rgt-in42.3%
cancel-sign-sub42.3%
distribute-rgt-out--42.3%
sub-neg42.3%
remove-double-neg42.3%
+-commutative42.3%
hypot-define88.8%
Simplified88.8%
Final simplification85.9%
(FPCore (re im) :precision binary64 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0) (* 0.5 (sqrt (/ (pow im 2.0) (- re)))) (* 0.5 (sqrt (* 2.0 (+ 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((pow(im, 2.0) / -re));
} else {
tmp = 0.5 * sqrt((2.0 * (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((Math.pow(im, 2.0) / -re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (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((math.pow(im, 2.0) / -re)) else: tmp = 0.5 * math.sqrt((2.0 * (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 ^ 2.0) / Float64(-re)))); 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 (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) tmp = 0.5 * sqrt(((im ^ 2.0) / -re)); else tmp = 0.5 * sqrt((2.0 * (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[(N[Power[im, 2.0], $MachinePrecision] / (-re)), $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}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{{im}^{2}}{-re}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \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.8%
sqr-neg8.8%
+-commutative8.8%
sqr-neg8.8%
+-commutative8.8%
distribute-rgt-in8.8%
cancel-sign-sub8.8%
distribute-rgt-out--8.8%
sub-neg8.8%
remove-double-neg8.8%
+-commutative8.8%
hypot-define8.8%
Simplified8.8%
Taylor expanded in re around -inf 56.6%
mul-1-neg56.6%
distribute-neg-frac256.6%
Simplified56.6%
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 42.3%
sqr-neg42.3%
+-commutative42.3%
sqr-neg42.3%
+-commutative42.3%
distribute-rgt-in42.3%
cancel-sign-sub42.3%
distribute-rgt-out--42.3%
sub-neg42.3%
remove-double-neg42.3%
+-commutative42.3%
hypot-define88.8%
Simplified88.8%
Final simplification85.8%
(FPCore (re im) :precision binary64 (if (<= re -1.9e+124) (* 0.5 (sqrt (/ (pow im 2.0) (- re)))) (if (<= re 8e-45) (* 0.5 (sqrt (* 2.0 im))) (* 0.5 (* 2.0 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -1.9e+124) {
tmp = 0.5 * sqrt((pow(im, 2.0) / -re));
} else if (re <= 8e-45) {
tmp = 0.5 * sqrt((2.0 * 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.9d+124)) then
tmp = 0.5d0 * sqrt(((im ** 2.0d0) / -re))
else if (re <= 8d-45) then
tmp = 0.5d0 * sqrt((2.0d0 * 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.9e+124) {
tmp = 0.5 * Math.sqrt((Math.pow(im, 2.0) / -re));
} else if (re <= 8e-45) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.9e+124: tmp = 0.5 * math.sqrt((math.pow(im, 2.0) / -re)) elif re <= 8e-45: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.9e+124) tmp = Float64(0.5 * sqrt(Float64((im ^ 2.0) / Float64(-re)))); elseif (re <= 8e-45) tmp = Float64(0.5 * sqrt(Float64(2.0 * 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.9e+124) tmp = 0.5 * sqrt(((im ^ 2.0) / -re)); elseif (re <= 8e-45) tmp = 0.5 * sqrt((2.0 * im)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.9e+124], N[(0.5 * N[Sqrt[N[(N[Power[im, 2.0], $MachinePrecision] / (-re)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 8e-45], N[(0.5 * N[Sqrt[N[(2.0 * im), $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.9 \cdot 10^{+124}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{{im}^{2}}{-re}}\\
\mathbf{elif}\;re \leq 8 \cdot 10^{-45}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -1.8999999999999999e124Initial program 5.7%
sqr-neg5.7%
+-commutative5.7%
sqr-neg5.7%
+-commutative5.7%
distribute-rgt-in5.7%
cancel-sign-sub5.7%
distribute-rgt-out--5.7%
sub-neg5.7%
remove-double-neg5.7%
+-commutative5.7%
hypot-define38.9%
Simplified38.9%
Taylor expanded in re around -inf 55.8%
mul-1-neg55.8%
distribute-neg-frac255.8%
Simplified55.8%
if -1.8999999999999999e124 < re < 7.99999999999999987e-45Initial program 44.5%
sqr-neg44.5%
+-commutative44.5%
sqr-neg44.5%
+-commutative44.5%
distribute-rgt-in44.5%
cancel-sign-sub44.5%
distribute-rgt-out--44.5%
sub-neg44.5%
remove-double-neg44.5%
+-commutative44.5%
hypot-define81.5%
Simplified81.5%
Taylor expanded in re around 0 38.7%
*-commutative38.7%
Simplified38.7%
if 7.99999999999999987e-45 < re Initial program 45.0%
sqr-neg45.0%
+-commutative45.0%
sqr-neg45.0%
+-commutative45.0%
distribute-rgt-in45.0%
cancel-sign-sub45.0%
distribute-rgt-out--45.0%
sub-neg45.0%
remove-double-neg45.0%
+-commutative45.0%
hypot-define100.0%
Simplified100.0%
Taylor expanded in im around 0 70.6%
*-commutative70.6%
unpow270.6%
rem-square-sqrt71.9%
Simplified71.9%
Final simplification51.5%
(FPCore (re im) :precision binary64 (if (<= re -1.65e+235) (* 0.5 (sqrt 0.0)) (if (<= re 4.2e-46) (* 0.5 (sqrt (* 2.0 im))) (* 0.5 (* 2.0 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -1.65e+235) {
tmp = 0.5 * sqrt(0.0);
} else if (re <= 4.2e-46) {
tmp = 0.5 * sqrt((2.0 * 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.65d+235)) then
tmp = 0.5d0 * sqrt(0.0d0)
else if (re <= 4.2d-46) then
tmp = 0.5d0 * sqrt((2.0d0 * 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.65e+235) {
tmp = 0.5 * Math.sqrt(0.0);
} else if (re <= 4.2e-46) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.65e+235: tmp = 0.5 * math.sqrt(0.0) elif re <= 4.2e-46: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.65e+235) tmp = Float64(0.5 * sqrt(0.0)); elseif (re <= 4.2e-46) tmp = Float64(0.5 * sqrt(Float64(2.0 * 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.65e+235) tmp = 0.5 * sqrt(0.0); elseif (re <= 4.2e-46) tmp = 0.5 * sqrt((2.0 * im)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.65e+235], N[(0.5 * N[Sqrt[0.0], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 4.2e-46], N[(0.5 * N[Sqrt[N[(2.0 * im), $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.65 \cdot 10^{+235}:\\
\;\;\;\;0.5 \cdot \sqrt{0}\\
\mathbf{elif}\;re \leq 4.2 \cdot 10^{-46}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -1.65e235Initial program 2.4%
add-sqr-sqrt2.4%
pow22.4%
hypot-define25.2%
Applied egg-rr25.2%
Taylor expanded in re around -inf 31.0%
distribute-rgt1-in31.0%
metadata-eval31.0%
mul0-lft31.0%
Simplified31.0%
if -1.65e235 < re < 4.19999999999999975e-46Initial program 40.4%
sqr-neg40.4%
+-commutative40.4%
sqr-neg40.4%
+-commutative40.4%
distribute-rgt-in40.4%
cancel-sign-sub40.4%
distribute-rgt-out--40.4%
sub-neg40.4%
remove-double-neg40.4%
+-commutative40.4%
hypot-define77.5%
Simplified77.5%
Taylor expanded in re around 0 35.2%
*-commutative35.2%
Simplified35.2%
if 4.19999999999999975e-46 < re Initial program 45.0%
sqr-neg45.0%
+-commutative45.0%
sqr-neg45.0%
+-commutative45.0%
distribute-rgt-in45.0%
cancel-sign-sub45.0%
distribute-rgt-out--45.0%
sub-neg45.0%
remove-double-neg45.0%
+-commutative45.0%
hypot-define100.0%
Simplified100.0%
Taylor expanded in im around 0 70.6%
*-commutative70.6%
unpow270.6%
rem-square-sqrt71.9%
Simplified71.9%
Final simplification46.3%
(FPCore (re im) :precision binary64 (if (<= im 5.5e-205) (* 0.5 (sqrt 0.0)) (* 0.5 (sqrt (* 2.0 im)))))
double code(double re, double im) {
double tmp;
if (im <= 5.5e-205) {
tmp = 0.5 * sqrt(0.0);
} else {
tmp = 0.5 * sqrt((2.0 * im));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 5.5d-205) then
tmp = 0.5d0 * sqrt(0.0d0)
else
tmp = 0.5d0 * sqrt((2.0d0 * im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 5.5e-205) {
tmp = 0.5 * Math.sqrt(0.0);
} else {
tmp = 0.5 * Math.sqrt((2.0 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 5.5e-205: tmp = 0.5 * math.sqrt(0.0) else: tmp = 0.5 * math.sqrt((2.0 * im)) return tmp
function code(re, im) tmp = 0.0 if (im <= 5.5e-205) tmp = Float64(0.5 * sqrt(0.0)); else tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 5.5e-205) tmp = 0.5 * sqrt(0.0); else tmp = 0.5 * sqrt((2.0 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 5.5e-205], N[(0.5 * N[Sqrt[0.0], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 5.5 \cdot 10^{-205}:\\
\;\;\;\;0.5 \cdot \sqrt{0}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\end{array}
\end{array}
if im < 5.4999999999999996e-205Initial program 38.7%
add-sqr-sqrt37.9%
pow237.9%
hypot-define75.7%
Applied egg-rr75.7%
Taylor expanded in re around -inf 8.0%
distribute-rgt1-in8.0%
metadata-eval8.0%
mul0-lft8.0%
Simplified8.0%
if 5.4999999999999996e-205 < im Initial program 39.7%
sqr-neg39.7%
+-commutative39.7%
sqr-neg39.7%
+-commutative39.7%
distribute-rgt-in39.7%
cancel-sign-sub39.7%
distribute-rgt-out--39.7%
sub-neg39.7%
remove-double-neg39.7%
+-commutative39.7%
hypot-define85.3%
Simplified85.3%
Taylor expanded in re around 0 60.9%
*-commutative60.9%
Simplified60.9%
Final simplification31.2%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt 0.0)))
double code(double re, double im) {
return 0.5 * sqrt(0.0);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt(0.0d0)
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt(0.0);
}
def code(re, im): return 0.5 * math.sqrt(0.0)
function code(re, im) return Float64(0.5 * sqrt(0.0)) end
function tmp = code(re, im) tmp = 0.5 * sqrt(0.0); end
code[re_, im_] := N[(0.5 * N[Sqrt[0.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{0}
\end{array}
Initial program 39.2%
add-sqr-sqrt38.7%
pow238.7%
hypot-define79.8%
Applied egg-rr79.8%
Taylor expanded in re around -inf 5.8%
distribute-rgt1-in5.8%
metadata-eval5.8%
mul0-lft5.8%
Simplified5.8%
Final simplification5.8%
(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 2024062
(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)))))