
(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 (<= re -8600000000000.0) (* 0.5 (pow (exp (* 0.25 (+ (log (/ -1.0 re)) (log (* im im))))) 2.0)) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
double code(double re, double im) {
double tmp;
if (re <= -8600000000000.0) {
tmp = 0.5 * pow(exp((0.25 * (log((-1.0 / re)) + log((im * 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 (re <= -8600000000000.0) {
tmp = 0.5 * Math.pow(Math.exp((0.25 * (Math.log((-1.0 / re)) + Math.log((im * 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 re <= -8600000000000.0: tmp = 0.5 * math.pow(math.exp((0.25 * (math.log((-1.0 / re)) + math.log((im * 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 (re <= -8600000000000.0) tmp = Float64(0.5 * (exp(Float64(0.25 * Float64(log(Float64(-1.0 / re)) + log(Float64(im * 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 (re <= -8600000000000.0) tmp = 0.5 * (exp((0.25 * (log((-1.0 / re)) + log((im * im))))) ^ 2.0); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -8600000000000.0], N[(0.5 * 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]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -8600000000000:\\
\;\;\;\;0.5 \cdot {\left(e^{0.25 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left(im \cdot im\right)\right)}\right)}^{2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -8.6e12Initial program 8.0%
Taylor expanded in re around 0
*-commutativeN/A
lower-*.f647.6
Applied rewrites7.6%
lift-sqrt.f64N/A
pow1/2N/A
sqr-powN/A
pow2N/A
lower-pow.f64N/A
lower-pow.f64N/A
metadata-evalN/A
Applied rewrites7.6%
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-*.f6460.2
Applied rewrites60.2%
if -8.6e12 < re Initial program 54.2%
lift-sqrt.f64N/A
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lower-hypot.f6494.6
Applied rewrites94.6%
Final simplification86.0%
(FPCore (re im) :precision binary64 (if (<= re -7000000000000.0) (* 0.5 (sqrt (* im (/ (- im) re)))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
double code(double re, double im) {
double tmp;
if (re <= -7000000000000.0) {
tmp = 0.5 * sqrt((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 (re <= -7000000000000.0) {
tmp = 0.5 * Math.sqrt((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 re <= -7000000000000.0: tmp = 0.5 * math.sqrt((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 (re <= -7000000000000.0) tmp = Float64(0.5 * sqrt(Float64(im * Float64(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 (re <= -7000000000000.0) tmp = 0.5 * sqrt((im * (-im / re))); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -7000000000000.0], N[(0.5 * N[Sqrt[N[(im * N[((-im) / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -7000000000000:\\
\;\;\;\;0.5 \cdot \sqrt{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 re < -7e12Initial program 8.0%
Taylor expanded in re around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f6447.7
Applied rewrites47.7%
Applied rewrites57.7%
if -7e12 < re Initial program 54.2%
lift-sqrt.f64N/A
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lower-hypot.f6494.6
Applied rewrites94.6%
Final simplification85.3%
(FPCore (re im)
:precision binary64
(if (<= re -7000000000000.0)
(* 0.5 (sqrt (* im (/ (- im) re))))
(if (<= re 1.4e-139)
(* 0.5 (sqrt (fma 2.0 (+ re im) (/ (* re re) im))))
(if (<= re 8.5e+93)
(* 0.5 (sqrt (* 2.0 (+ re (sqrt (fma im im (* re re)))))))
(sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= -7000000000000.0) {
tmp = 0.5 * sqrt((im * (-im / re)));
} else if (re <= 1.4e-139) {
tmp = 0.5 * sqrt(fma(2.0, (re + im), ((re * re) / im)));
} else if (re <= 8.5e+93) {
tmp = 0.5 * sqrt((2.0 * (re + sqrt(fma(im, im, (re * re))))));
} else {
tmp = sqrt(re);
}
return tmp;
}
function code(re, im) tmp = 0.0 if (re <= -7000000000000.0) tmp = Float64(0.5 * sqrt(Float64(im * Float64(Float64(-im) / re)))); elseif (re <= 1.4e-139) tmp = Float64(0.5 * sqrt(fma(2.0, Float64(re + im), Float64(Float64(re * re) / im)))); elseif (re <= 8.5e+93) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + sqrt(fma(im, im, Float64(re * re))))))); else tmp = sqrt(re); end return tmp end
code[re_, im_] := If[LessEqual[re, -7000000000000.0], N[(0.5 * N[Sqrt[N[(im * N[((-im) / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.4e-139], 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, 8.5e+93], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(im * im + N[(re * re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -7000000000000:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\
\mathbf{elif}\;re \leq 1.4 \cdot 10^{-139}:\\
\;\;\;\;0.5 \cdot \sqrt{\mathsf{fma}\left(2, re + im, \frac{re \cdot re}{im}\right)}\\
\mathbf{elif}\;re \leq 8.5 \cdot 10^{+93}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\mathsf{fma}\left(im, im, re \cdot re\right)}\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -7e12Initial program 8.0%
Taylor expanded in re around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f6447.7
Applied rewrites47.7%
Applied rewrites57.7%
if -7e12 < re < 1.3999999999999999e-139Initial program 54.1%
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-*.f6445.6
Applied rewrites45.6%
if 1.3999999999999999e-139 < re < 8.5000000000000005e93Initial program 88.0%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6488.0
lift-+.f64N/A
+-commutativeN/A
lower-+.f6488.0
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6488.0
Applied rewrites88.0%
if 8.5000000000000005e93 < re Initial program 13.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.f6484.6
Applied rewrites84.6%
Final simplification62.7%
(FPCore (re im) :precision binary64 (if (<= re -7000000000000.0) (* 0.5 (sqrt (* im (/ (- im) re)))) (if (<= re 2.3e-44) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -7000000000000.0) {
tmp = 0.5 * sqrt((im * (-im / re)));
} else if (re <= 2.3e-44) {
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 <= (-7000000000000.0d0)) then
tmp = 0.5d0 * sqrt((im * (-im / re)))
else if (re <= 2.3d-44) 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 <= -7000000000000.0) {
tmp = 0.5 * Math.sqrt((im * (-im / re)));
} else if (re <= 2.3e-44) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -7000000000000.0: tmp = 0.5 * math.sqrt((im * (-im / re))) elif re <= 2.3e-44: 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 <= -7000000000000.0) tmp = Float64(0.5 * sqrt(Float64(im * Float64(Float64(-im) / re)))); elseif (re <= 2.3e-44) 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 <= -7000000000000.0) tmp = 0.5 * sqrt((im * (-im / re))); elseif (re <= 2.3e-44) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -7000000000000.0], N[(0.5 * N[Sqrt[N[(im * N[((-im) / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.3e-44], 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 -7000000000000:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\
\mathbf{elif}\;re \leq 2.3 \cdot 10^{-44}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -7e12Initial program 8.0%
Taylor expanded in re around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f6447.7
Applied rewrites47.7%
Applied rewrites57.7%
if -7e12 < re < 2.29999999999999998e-44Initial program 59.7%
Taylor expanded in re around 0
lower-+.f6445.3
Applied rewrites45.3%
if 2.29999999999999998e-44 < re Initial program 43.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.f6479.0
Applied rewrites79.0%
Final simplification56.8%
(FPCore (re im) :precision binary64 (if (<= re -7000000000000.0) (* 0.5 (sqrt (- (/ (* im im) re)))) (if (<= re 2.3e-44) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -7000000000000.0) {
tmp = 0.5 * sqrt(-((im * im) / re));
} else if (re <= 2.3e-44) {
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 <= (-7000000000000.0d0)) then
tmp = 0.5d0 * sqrt(-((im * im) / re))
else if (re <= 2.3d-44) 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 <= -7000000000000.0) {
tmp = 0.5 * Math.sqrt(-((im * im) / re));
} else if (re <= 2.3e-44) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -7000000000000.0: tmp = 0.5 * math.sqrt(-((im * im) / re)) elif re <= 2.3e-44: 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 <= -7000000000000.0) tmp = Float64(0.5 * sqrt(Float64(-Float64(Float64(im * im) / re)))); elseif (re <= 2.3e-44) 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 <= -7000000000000.0) tmp = 0.5 * sqrt(-((im * im) / re)); elseif (re <= 2.3e-44) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -7000000000000.0], N[(0.5 * N[Sqrt[(-N[(N[(im * im), $MachinePrecision] / re), $MachinePrecision])], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.3e-44], 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 -7000000000000:\\
\;\;\;\;0.5 \cdot \sqrt{-\frac{im \cdot im}{re}}\\
\mathbf{elif}\;re \leq 2.3 \cdot 10^{-44}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -7e12Initial program 8.0%
Taylor expanded in re around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f6447.7
Applied rewrites47.7%
if -7e12 < re < 2.29999999999999998e-44Initial program 59.7%
Taylor expanded in re around 0
lower-+.f6445.3
Applied rewrites45.3%
if 2.29999999999999998e-44 < re Initial program 43.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.f6479.0
Applied rewrites79.0%
Final simplification54.3%
(FPCore (re im) :precision binary64 (if (<= re 1e-52) (* 0.5 (sqrt (* 2.0 im))) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= 1e-52) {
tmp = 0.5 * sqrt((2.0 * 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 <= 1d-52) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 1e-52) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 1e-52: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 1e-52) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 1e-52) tmp = 0.5 * sqrt((2.0 * im)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 1e-52], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 10^{-52}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 1e-52Initial program 41.9%
Taylor expanded in re around 0
*-commutativeN/A
lower-*.f6432.0
Applied rewrites32.0%
if 1e-52 < re Initial program 44.9%
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.f6478.2
Applied rewrites78.2%
Final simplification43.9%
(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 42.6%
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.6
Applied rewrites26.6%
(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 2024234
(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)))))