
(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}
im_m = (fabs.f64 im)
(FPCore (re im_m)
:precision binary64
(if (<= re -2.5e+50)
(* (pow (exp (* 0.25 (+ (log (/ -1.0 re)) (log (* im im))))) 2.0) 0.5)
(if (<= re 1e-171)
(* 0.5 (sqrt (fma 2.0 (+ re im) (/ (* re re) im))))
(if (<= re 2e+81)
(* 0.5 (sqrt (* 2.0 (+ re (sqrt (fma im_m im_m (* re re)))))))
(sqrt re)))))im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= -2.5e+50) {
tmp = pow(exp((0.25 * (log((-1.0 / re)) + log((im * im))))), 2.0) * 0.5;
} else if (re <= 1e-171) {
tmp = 0.5 * sqrt(fma(2.0, (re + im), ((re * re) / im)));
} else if (re <= 2e+81) {
tmp = 0.5 * sqrt((2.0 * (re + sqrt(fma(im_m, im_m, (re * re))))));
} else {
tmp = sqrt(re);
}
return tmp;
}
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= -2.5e+50) tmp = Float64((exp(Float64(0.25 * Float64(log(Float64(-1.0 / re)) + log(Float64(im * im))))) ^ 2.0) * 0.5); elseif (re <= 1e-171) tmp = Float64(0.5 * sqrt(fma(2.0, Float64(re + im), Float64(Float64(re * re) / im)))); elseif (re <= 2e+81) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + sqrt(fma(im_m, im_m, Float64(re * re))))))); else tmp = sqrt(re); end return tmp end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, -2.5e+50], N[(N[Power[N[Exp[N[(0.25 * N[(N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision] + N[Log[N[(im * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 2.0], $MachinePrecision] * 0.5), $MachinePrecision], If[LessEqual[re, 1e-171], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision] + N[(N[(re * re), $MachinePrecision] / im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2e+81], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(im$95$m * im$95$m + N[(re * re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.5 \cdot 10^{+50}:\\
\;\;\;\;{\left(e^{0.25 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left(im \cdot im\right)\right)}\right)}^{2} \cdot 0.5\\
\mathbf{elif}\;re \leq 10^{-171}:\\
\;\;\;\;0.5 \cdot \sqrt{\mathsf{fma}\left(2, re + im, \frac{re \cdot re}{im}\right)}\\
\mathbf{elif}\;re \leq 2 \cdot 10^{+81}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\mathsf{fma}\left(im\_m, im\_m, re \cdot re\right)}\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -2.5e50Initial program 5.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f645.4
lift-+.f64N/A
+-commutativeN/A
lower-+.f645.4
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f645.4
Applied rewrites5.4%
Taylor expanded in re around 0
*-commutativeN/A
lower-*.f645.4
Applied rewrites5.4%
lift-sqrt.f64N/A
pow1/2N/A
sqr-powN/A
pow2N/A
lower-pow.f64N/A
Applied rewrites5.4%
Taylor expanded in re around -inf
lower-exp.f64N/A
lower-*.f64N/A
lower-+.f64N/A
lower-log.f64N/A
lower-/.f64N/A
lower-log.f64N/A
unpow2N/A
lower-*.f6471.9
Applied rewrites71.9%
if -2.5e50 < re < 9.9999999999999998e-172Initial program 43.0%
Taylor expanded in re around 0
distribute-rgt-inN/A
associate-+r+N/A
associate-*l/N/A
unpow2N/A
distribute-lft-outN/A
lower-fma.f64N/A
lower-+.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f6447.7
Applied rewrites47.7%
if 9.9999999999999998e-172 < re < 1.99999999999999984e81Initial program 74.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6474.4
lift-+.f64N/A
+-commutativeN/A
lower-+.f6474.4
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6474.4
Applied rewrites74.4%
if 1.99999999999999984e81 < re Initial program 27.7%
Taylor expanded in re around inf
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
associate-*r*N/A
metadata-evalN/A
*-lft-identityN/A
lower-sqrt.f6489.8
Applied rewrites89.8%
Final simplification65.0%
im_m = (fabs.f64 im)
(FPCore (re im_m)
:precision binary64
(if (<= re -2.5e+50)
(* 0.5 (sqrt (* (- im) (/ im re))))
(if (<= re 1e-171)
(* 0.5 (sqrt (fma 2.0 (+ re im) (/ (* re re) im))))
(if (<= re 2e+81)
(* 0.5 (sqrt (* 2.0 (+ re (sqrt (fma im_m im_m (* re re)))))))
(sqrt re)))))im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= -2.5e+50) {
tmp = 0.5 * sqrt((-im * (im / re)));
} else if (re <= 1e-171) {
tmp = 0.5 * sqrt(fma(2.0, (re + im), ((re * re) / im)));
} else if (re <= 2e+81) {
tmp = 0.5 * sqrt((2.0 * (re + sqrt(fma(im_m, im_m, (re * re))))));
} else {
tmp = sqrt(re);
}
return tmp;
}
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= -2.5e+50) tmp = Float64(0.5 * sqrt(Float64(Float64(-im) * Float64(im / re)))); elseif (re <= 1e-171) tmp = Float64(0.5 * sqrt(fma(2.0, Float64(re + im), Float64(Float64(re * re) / im)))); elseif (re <= 2e+81) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + sqrt(fma(im_m, im_m, Float64(re * re))))))); else tmp = sqrt(re); end return tmp end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, -2.5e+50], N[(0.5 * N[Sqrt[N[((-im) * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1e-171], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision] + N[(N[(re * re), $MachinePrecision] / im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2e+81], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(im$95$m * im$95$m + N[(re * re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.5 \cdot 10^{+50}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\
\mathbf{elif}\;re \leq 10^{-171}:\\
\;\;\;\;0.5 \cdot \sqrt{\mathsf{fma}\left(2, re + im, \frac{re \cdot re}{im}\right)}\\
\mathbf{elif}\;re \leq 2 \cdot 10^{+81}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\mathsf{fma}\left(im\_m, im\_m, re \cdot re\right)}\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -2.5e50Initial program 5.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f645.4
lift-+.f64N/A
+-commutativeN/A
lower-+.f645.4
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f645.4
Applied rewrites5.4%
Taylor expanded in re around -inf
associate-*r/N/A
lower-/.f64N/A
mul-1-negN/A
unpow2N/A
distribute-rgt-neg-inN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
lower-neg.f6465.2
Applied rewrites65.2%
Applied rewrites67.2%
if -2.5e50 < re < 9.9999999999999998e-172Initial program 43.0%
Taylor expanded in re around 0
distribute-rgt-inN/A
associate-+r+N/A
associate-*l/N/A
unpow2N/A
distribute-lft-outN/A
lower-fma.f64N/A
lower-+.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f6447.7
Applied rewrites47.7%
if 9.9999999999999998e-172 < re < 1.99999999999999984e81Initial program 74.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6474.4
lift-+.f64N/A
+-commutativeN/A
lower-+.f6474.4
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6474.4
Applied rewrites74.4%
if 1.99999999999999984e81 < re Initial program 27.7%
Taylor expanded in re around inf
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
associate-*r*N/A
metadata-evalN/A
*-lft-identityN/A
lower-sqrt.f6489.8
Applied rewrites89.8%
Final simplification64.0%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= re -2.5e+50) (* 0.5 (sqrt (* (- im) (/ im re)))) (if (<= re 5e+82) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re))))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= -2.5e+50) {
tmp = 0.5 * sqrt((-im * (im / re)));
} else if (re <= 5e+82) {
tmp = 0.5 * sqrt((2.0 * (re + im)));
} else {
tmp = sqrt(re);
}
return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (re <= (-2.5d+50)) then
tmp = 0.5d0 * sqrt((-im * (im / re)))
else if (re <= 5d+82) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = sqrt(re)
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= -2.5e+50) {
tmp = 0.5 * Math.sqrt((-im * (im / re)));
} else if (re <= 5e+82) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= -2.5e+50: tmp = 0.5 * math.sqrt((-im * (im / re))) elif re <= 5e+82: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = math.sqrt(re) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= -2.5e+50) tmp = Float64(0.5 * sqrt(Float64(Float64(-im) * Float64(im / re)))); elseif (re <= 5e+82) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = sqrt(re); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= -2.5e+50) tmp = 0.5 * sqrt((-im * (im / re))); elseif (re <= 5e+82) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, -2.5e+50], N[(0.5 * N[Sqrt[N[((-im) * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5e+82], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.5 \cdot 10^{+50}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\
\mathbf{elif}\;re \leq 5 \cdot 10^{+82}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -2.5e50Initial program 5.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f645.4
lift-+.f64N/A
+-commutativeN/A
lower-+.f645.4
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f645.4
Applied rewrites5.4%
Taylor expanded in re around -inf
associate-*r/N/A
lower-/.f64N/A
mul-1-negN/A
unpow2N/A
distribute-rgt-neg-inN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
lower-neg.f6465.2
Applied rewrites65.2%
Applied rewrites67.2%
if -2.5e50 < re < 5.00000000000000015e82Initial program 52.1%
Taylor expanded in re around 0
lower-+.f6442.5
Applied rewrites42.5%
if 5.00000000000000015e82 < re Initial program 26.5%
Taylor expanded in re around inf
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
associate-*r*N/A
metadata-evalN/A
*-lft-identityN/A
lower-sqrt.f6491.5
Applied rewrites91.5%
Final simplification56.0%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= re -2.5e+50) (* 0.5 (sqrt (/ (* im im) (- re)))) (if (<= re 5e+82) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re))))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= -2.5e+50) {
tmp = 0.5 * sqrt(((im * im) / -re));
} else if (re <= 5e+82) {
tmp = 0.5 * sqrt((2.0 * (re + im)));
} else {
tmp = sqrt(re);
}
return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (re <= (-2.5d+50)) then
tmp = 0.5d0 * sqrt(((im * im) / -re))
else if (re <= 5d+82) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = sqrt(re)
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= -2.5e+50) {
tmp = 0.5 * Math.sqrt(((im * im) / -re));
} else if (re <= 5e+82) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= -2.5e+50: tmp = 0.5 * math.sqrt(((im * im) / -re)) elif re <= 5e+82: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = math.sqrt(re) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= -2.5e+50) tmp = Float64(0.5 * sqrt(Float64(Float64(im * im) / Float64(-re)))); elseif (re <= 5e+82) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = sqrt(re); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= -2.5e+50) tmp = 0.5 * sqrt(((im * im) / -re)); elseif (re <= 5e+82) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, -2.5e+50], N[(0.5 * N[Sqrt[N[(N[(im * im), $MachinePrecision] / (-re)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5e+82], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.5 \cdot 10^{+50}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot im}{-re}}\\
\mathbf{elif}\;re \leq 5 \cdot 10^{+82}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -2.5e50Initial program 5.4%
Taylor expanded in re around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f6465.2
Applied rewrites65.2%
if -2.5e50 < re < 5.00000000000000015e82Initial program 52.1%
Taylor expanded in re around 0
lower-+.f6442.5
Applied rewrites42.5%
if 5.00000000000000015e82 < re Initial program 26.5%
Taylor expanded in re around inf
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
associate-*r*N/A
metadata-evalN/A
*-lft-identityN/A
lower-sqrt.f6491.5
Applied rewrites91.5%
Final simplification55.6%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= re -2e+223) (* 0.5 (sqrt 0.0)) (if (<= re 2e-131) (* 0.5 (sqrt (* 2.0 im))) (sqrt re))))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= -2e+223) {
tmp = 0.5 * sqrt(0.0);
} else if (re <= 2e-131) {
tmp = 0.5 * sqrt((2.0 * im));
} else {
tmp = sqrt(re);
}
return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (re <= (-2d+223)) then
tmp = 0.5d0 * sqrt(0.0d0)
else if (re <= 2d-131) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else
tmp = sqrt(re)
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= -2e+223) {
tmp = 0.5 * Math.sqrt(0.0);
} else if (re <= 2e-131) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= -2e+223: tmp = 0.5 * math.sqrt(0.0) elif re <= 2e-131: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = math.sqrt(re) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= -2e+223) tmp = Float64(0.5 * sqrt(0.0)); elseif (re <= 2e-131) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = sqrt(re); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= -2e+223) tmp = 0.5 * sqrt(0.0); elseif (re <= 2e-131) tmp = 0.5 * sqrt((2.0 * im)); else tmp = sqrt(re); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, -2e+223], N[(0.5 * N[Sqrt[0.0], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2e-131], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2 \cdot 10^{+223}:\\
\;\;\;\;0.5 \cdot \sqrt{0}\\
\mathbf{elif}\;re \leq 2 \cdot 10^{-131}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -2.00000000000000009e223Initial program 2.0%
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
flip-+N/A
associate-*l/N/A
associate-/l*N/A
lower-*.f64N/A
Applied rewrites0.0%
Taylor expanded in re around inf
*-commutativeN/A
unpow2N/A
times-fracN/A
distribute-rgt1-inN/A
metadata-evalN/A
mul0-lftN/A
times-fracN/A
mul0-lftN/A
mul0-lftN/A
mul0-lftN/A
metadata-evalN/A
distribute-lft1-inN/A
*-commutativeN/A
unpow2N/A
*-commutativeN/A
unpow2N/A
times-fracN/A
Applied rewrites48.7%
if -2.00000000000000009e223 < re < 2e-131Initial program 37.0%
Taylor expanded in re around 0
*-commutativeN/A
lower-*.f6435.5
Applied rewrites35.5%
if 2e-131 < re Initial program 46.8%
Taylor expanded in re around inf
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
associate-*r*N/A
metadata-evalN/A
*-lft-identityN/A
lower-sqrt.f6473.9
Applied rewrites73.9%
Final simplification48.5%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (if (<= re -2e-308) (* 0.5 (sqrt 0.0)) (sqrt re)))
im_m = fabs(im);
double code(double re, double im_m) {
double tmp;
if (re <= -2e-308) {
tmp = 0.5 * sqrt(0.0);
} else {
tmp = sqrt(re);
}
return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
real(8) :: tmp
if (re <= (-2d-308)) then
tmp = 0.5d0 * sqrt(0.0d0)
else
tmp = sqrt(re)
end if
code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
double tmp;
if (re <= -2e-308) {
tmp = 0.5 * Math.sqrt(0.0);
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
im_m = math.fabs(im) def code(re, im_m): tmp = 0 if re <= -2e-308: tmp = 0.5 * math.sqrt(0.0) else: tmp = math.sqrt(re) return tmp
im_m = abs(im) function code(re, im_m) tmp = 0.0 if (re <= -2e-308) tmp = Float64(0.5 * sqrt(0.0)); else tmp = sqrt(re); end return tmp end
im_m = abs(im); function tmp_2 = code(re, im_m) tmp = 0.0; if (re <= -2e-308) tmp = 0.5 * sqrt(0.0); else tmp = sqrt(re); end tmp_2 = tmp; end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := If[LessEqual[re, -2e-308], N[(0.5 * N[Sqrt[0.0], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2 \cdot 10^{-308}:\\
\;\;\;\;0.5 \cdot \sqrt{0}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -1.9999999999999998e-308Initial program 23.7%
lift-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
flip-+N/A
associate-*l/N/A
associate-/l*N/A
lower-*.f64N/A
Applied rewrites22.0%
Taylor expanded in re around inf
*-commutativeN/A
unpow2N/A
times-fracN/A
distribute-rgt1-inN/A
metadata-evalN/A
mul0-lftN/A
times-fracN/A
mul0-lftN/A
mul0-lftN/A
mul0-lftN/A
metadata-evalN/A
distribute-lft1-inN/A
*-commutativeN/A
unpow2N/A
*-commutativeN/A
unpow2N/A
times-fracN/A
Applied rewrites10.3%
if -1.9999999999999998e-308 < re Initial program 51.2%
Taylor expanded in re around inf
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
associate-*r*N/A
metadata-evalN/A
*-lft-identityN/A
lower-sqrt.f6451.6
Applied rewrites51.6%
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (sqrt re))
im_m = fabs(im);
double code(double re, double im_m) {
return sqrt(re);
}
im_m = abs(im)
real(8) function code(re, im_m)
real(8), intent (in) :: re
real(8), intent (in) :: im_m
code = sqrt(re)
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
return Math.sqrt(re);
}
im_m = math.fabs(im) def code(re, im_m): return math.sqrt(re)
im_m = abs(im) function code(re, im_m) return sqrt(re) end
im_m = abs(im); function tmp = code(re, im_m) tmp = sqrt(re); end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := N[Sqrt[re], $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|
\\
\sqrt{re}
\end{array}
Initial program 37.8%
Taylor expanded in re around inf
*-commutativeN/A
unpow2N/A
rem-square-sqrtN/A
associate-*r*N/A
metadata-evalN/A
*-lft-identityN/A
lower-sqrt.f6426.4
Applied rewrites26.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 2024214
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:alt
(! :herbie-platform default (if (< re 0) (* 1/2 (* (sqrt 2) (sqrt (/ (* im im) (- (modulus re im) re))))) (* 1/2 (sqrt (* 2 (+ (modulus re im) re))))))
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))