
(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 8 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}
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= (+ re (sqrt (+ (* re re) (* im im)))) 0.0) (* 0.5 (* (sqrt im) (sqrt (/ (- im) re)))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if ((re + sqrt(((re * re) + (im * im)))) <= 0.0) {
tmp = 0.5 * (sqrt(im) * sqrt((-im / re)));
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
return tmp;
}
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if ((re + Math.sqrt(((re * re) + (im * im)))) <= 0.0) {
tmp = 0.5 * (Math.sqrt(im) * Math.sqrt((-im / re)));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if (re + math.sqrt(((re * re) + (im * im)))) <= 0.0: tmp = 0.5 * (math.sqrt(im) * math.sqrt((-im / re))) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (Float64(re + sqrt(Float64(Float64(re * re) + Float64(im * im)))) <= 0.0) tmp = Float64(0.5 * Float64(sqrt(im) * sqrt(Float64(Float64(-im) / re)))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if ((re + sqrt(((re * re) + (im * im)))) <= 0.0) tmp = 0.5 * (sqrt(im) * sqrt((-im / re))); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 0.0], N[(0.5 * N[(N[Sqrt[im], $MachinePrecision] * N[Sqrt[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}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re + \sqrt{re \cdot re + im \cdot im} \leq 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{im} \cdot \sqrt{\frac{-im}{re}}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0Initial program 9.5%
+-commutative9.5%
hypot-def19.3%
Simplified19.3%
Taylor expanded in re around -inf 42.2%
*-commutative42.2%
unpow242.2%
associate-/l*52.4%
Simplified52.4%
expm1-log1p-u52.2%
expm1-udef14.3%
*-commutative14.3%
associate-*l*14.3%
associate-/r/14.3%
*-commutative14.3%
metadata-eval14.3%
Applied egg-rr14.3%
expm1-def52.3%
expm1-log1p52.5%
associate-*l*52.5%
associate-*l/52.5%
*-commutative52.5%
neg-mul-152.5%
Simplified52.5%
distribute-frac-neg52.5%
distribute-rgt-neg-out52.5%
mul-1-neg52.5%
*-commutative52.5%
associate-*l*52.5%
sqrt-prod50.2%
Applied egg-rr50.2%
*-commutative50.2%
associate-*r/50.2%
neg-mul-150.2%
Simplified50.2%
if 0.0 < (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 46.6%
+-commutative46.6%
hypot-def90.3%
Simplified90.3%
Final simplification82.6%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -2.8e+77) (* 0.5 (sqrt (/ (- im) (/ re im)))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -2.8e+77) {
tmp = 0.5 * sqrt((-im / (re / im)));
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
return tmp;
}
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -2.8e+77) {
tmp = 0.5 * Math.sqrt((-im / (re / im)));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -2.8e+77: tmp = 0.5 * math.sqrt((-im / (re / im))) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -2.8e+77) tmp = Float64(0.5 * sqrt(Float64(Float64(-im) / Float64(re / im)))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.8e+77) tmp = 0.5 * sqrt((-im / (re / im))); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -2.8e+77], N[(0.5 * N[Sqrt[N[((-im) / N[(re / im), $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}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.8 \cdot 10^{+77}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{-im}{\frac{re}{im}}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -2.8e77Initial program 8.9%
+-commutative8.9%
hypot-def35.6%
Simplified35.6%
Taylor expanded in re around -inf 59.2%
*-commutative59.2%
unpow259.2%
associate-/l*63.8%
Simplified63.8%
expm1-log1p-u63.3%
expm1-udef32.5%
*-commutative32.5%
associate-*l*32.5%
associate-/r/32.5%
*-commutative32.5%
metadata-eval32.5%
Applied egg-rr32.5%
expm1-def63.2%
expm1-log1p63.7%
associate-*l*63.7%
associate-*l/63.7%
*-commutative63.7%
neg-mul-163.7%
Simplified63.7%
Taylor expanded in im around 0 59.2%
mul-1-neg59.2%
unpow259.2%
associate-/l*63.8%
distribute-neg-frac63.8%
Simplified63.8%
if -2.8e77 < re Initial program 46.7%
+-commutative46.7%
hypot-def86.5%
Simplified86.5%
Final simplification82.1%
NOTE: im should be positive before calling this function
(FPCore (re im)
:precision binary64
(if (<= re -4.8e+53)
(* 0.5 (sqrt (/ (- im) (/ re im))))
(if (<= re 1.12e-44)
(* 0.5 (sqrt (+ (/ (* re re) im) (* 2.0 (+ re im)))))
(* 0.5 (* 2.0 (sqrt re))))))im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -4.8e+53) {
tmp = 0.5 * sqrt((-im / (re / im)));
} else if (re <= 1.12e-44) {
tmp = 0.5 * sqrt((((re * re) / im) + (2.0 * (re + im))));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-4.8d+53)) then
tmp = 0.5d0 * sqrt((-im / (re / im)))
else if (re <= 1.12d-44) then
tmp = 0.5d0 * sqrt((((re * re) / im) + (2.0d0 * (re + im))))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -4.8e+53) {
tmp = 0.5 * Math.sqrt((-im / (re / im)));
} else if (re <= 1.12e-44) {
tmp = 0.5 * Math.sqrt((((re * re) / im) + (2.0 * (re + im))));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -4.8e+53: tmp = 0.5 * math.sqrt((-im / (re / im))) elif re <= 1.12e-44: tmp = 0.5 * math.sqrt((((re * re) / im) + (2.0 * (re + im)))) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -4.8e+53) tmp = Float64(0.5 * sqrt(Float64(Float64(-im) / Float64(re / im)))); elseif (re <= 1.12e-44) tmp = Float64(0.5 * sqrt(Float64(Float64(Float64(re * re) / im) + Float64(2.0 * Float64(re + im))))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4.8e+53) tmp = 0.5 * sqrt((-im / (re / im))); elseif (re <= 1.12e-44) tmp = 0.5 * sqrt((((re * re) / im) + (2.0 * (re + im)))); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -4.8e+53], N[(0.5 * N[Sqrt[N[((-im) / N[(re / im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.12e-44], N[(0.5 * N[Sqrt[N[(N[(N[(re * re), $MachinePrecision] / im), $MachinePrecision] + N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.8 \cdot 10^{+53}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{-im}{\frac{re}{im}}}\\
\mathbf{elif}\;re \leq 1.12 \cdot 10^{-44}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{re \cdot re}{im} + 2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -4.8e53Initial program 12.0%
+-commutative12.0%
hypot-def38.1%
Simplified38.1%
Taylor expanded in re around -inf 57.8%
*-commutative57.8%
unpow257.8%
associate-/l*61.9%
Simplified61.9%
expm1-log1p-u61.4%
expm1-udef31.7%
*-commutative31.7%
associate-*l*31.7%
associate-/r/31.7%
*-commutative31.7%
metadata-eval31.7%
Applied egg-rr31.7%
expm1-def61.4%
expm1-log1p61.9%
associate-*l*61.9%
associate-*l/61.9%
*-commutative61.9%
neg-mul-161.9%
Simplified61.9%
Taylor expanded in im around 0 57.8%
mul-1-neg57.8%
unpow257.8%
associate-/l*61.9%
distribute-neg-frac61.9%
Simplified61.9%
if -4.8e53 < re < 1.1200000000000001e-44Initial program 49.5%
+-commutative49.5%
hypot-def80.6%
Simplified80.6%
Taylor expanded in re around 0 36.1%
unpow236.1%
distribute-lft-out36.1%
Simplified36.1%
if 1.1200000000000001e-44 < re Initial program 41.4%
+-commutative41.4%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 76.2%
unpow276.2%
rem-square-sqrt77.7%
Simplified77.7%
Final simplification52.4%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -4.2e+47) (* 0.5 (sqrt (* im (/ (- im) re)))) (if (<= re 4.1e-45) (* 0.5 (sqrt (* im 2.0))) (* 0.5 (* 2.0 (sqrt re))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -4.2e+47) {
tmp = 0.5 * sqrt((im * (-im / re)));
} else if (re <= 4.1e-45) {
tmp = 0.5 * sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-4.2d+47)) then
tmp = 0.5d0 * sqrt((im * (-im / re)))
else if (re <= 4.1d-45) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -4.2e+47) {
tmp = 0.5 * Math.sqrt((im * (-im / re)));
} else if (re <= 4.1e-45) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -4.2e+47: tmp = 0.5 * math.sqrt((im * (-im / re))) elif re <= 4.1e-45: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -4.2e+47) tmp = Float64(0.5 * sqrt(Float64(im * Float64(Float64(-im) / re)))); elseif (re <= 4.1e-45) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4.2e+47) tmp = 0.5 * sqrt((im * (-im / re))); elseif (re <= 4.1e-45) tmp = 0.5 * sqrt((im * 2.0)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -4.2e+47], N[(0.5 * N[Sqrt[N[(im * N[((-im) / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 4.1e-45], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.2 \cdot 10^{+47}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\
\mathbf{elif}\;re \leq 4.1 \cdot 10^{-45}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -4.2e47Initial program 12.0%
+-commutative12.0%
hypot-def38.1%
Simplified38.1%
Taylor expanded in re around -inf 57.8%
*-commutative57.8%
unpow257.8%
associate-/l*61.9%
Simplified61.9%
expm1-log1p-u61.4%
expm1-udef31.7%
*-commutative31.7%
associate-*l*31.7%
associate-/r/31.7%
*-commutative31.7%
metadata-eval31.7%
Applied egg-rr31.7%
expm1-def61.4%
expm1-log1p61.9%
associate-*l*61.9%
associate-*l/61.9%
*-commutative61.9%
neg-mul-161.9%
Simplified61.9%
if -4.2e47 < re < 4.0999999999999999e-45Initial program 49.5%
+-commutative49.5%
hypot-def80.6%
Simplified80.6%
Taylor expanded in re around 0 35.9%
*-commutative35.9%
Simplified35.9%
if 4.0999999999999999e-45 < re Initial program 41.4%
+-commutative41.4%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 76.2%
unpow276.2%
rem-square-sqrt77.7%
Simplified77.7%
Final simplification52.3%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -1.35e+39) (* 0.5 (sqrt (/ (- im) (/ re im)))) (if (<= re 6.6e-44) (* 0.5 (sqrt (* im 2.0))) (* 0.5 (* 2.0 (sqrt re))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -1.35e+39) {
tmp = 0.5 * sqrt((-im / (re / im)));
} else if (re <= 6.6e-44) {
tmp = 0.5 * sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-1.35d+39)) then
tmp = 0.5d0 * sqrt((-im / (re / im)))
else if (re <= 6.6d-44) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -1.35e+39) {
tmp = 0.5 * Math.sqrt((-im / (re / im)));
} else if (re <= 6.6e-44) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -1.35e+39: tmp = 0.5 * math.sqrt((-im / (re / im))) elif re <= 6.6e-44: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -1.35e+39) tmp = Float64(0.5 * sqrt(Float64(Float64(-im) / Float64(re / im)))); elseif (re <= 6.6e-44) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.35e+39) tmp = 0.5 * sqrt((-im / (re / im))); elseif (re <= 6.6e-44) tmp = 0.5 * sqrt((im * 2.0)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -1.35e+39], N[(0.5 * N[Sqrt[N[((-im) / N[(re / im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 6.6e-44], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.35 \cdot 10^{+39}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{-im}{\frac{re}{im}}}\\
\mathbf{elif}\;re \leq 6.6 \cdot 10^{-44}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -1.35000000000000002e39Initial program 12.0%
+-commutative12.0%
hypot-def38.1%
Simplified38.1%
Taylor expanded in re around -inf 57.8%
*-commutative57.8%
unpow257.8%
associate-/l*61.9%
Simplified61.9%
expm1-log1p-u61.4%
expm1-udef31.7%
*-commutative31.7%
associate-*l*31.7%
associate-/r/31.7%
*-commutative31.7%
metadata-eval31.7%
Applied egg-rr31.7%
expm1-def61.4%
expm1-log1p61.9%
associate-*l*61.9%
associate-*l/61.9%
*-commutative61.9%
neg-mul-161.9%
Simplified61.9%
Taylor expanded in im around 0 57.8%
mul-1-neg57.8%
unpow257.8%
associate-/l*61.9%
distribute-neg-frac61.9%
Simplified61.9%
if -1.35000000000000002e39 < re < 6.60000000000000011e-44Initial program 49.5%
+-commutative49.5%
hypot-def80.6%
Simplified80.6%
Taylor expanded in re around 0 35.9%
*-commutative35.9%
Simplified35.9%
if 6.60000000000000011e-44 < re Initial program 41.4%
+-commutative41.4%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 76.2%
unpow276.2%
rem-square-sqrt77.7%
Simplified77.7%
Final simplification52.3%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -1.65e+206) (* 0.5 (sqrt (/ (* im im) re))) (if (<= re 3.1e-45) (* 0.5 (sqrt (* im 2.0))) (* 0.5 (* 2.0 (sqrt re))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -1.65e+206) {
tmp = 0.5 * sqrt(((im * im) / re));
} else if (re <= 3.1e-45) {
tmp = 0.5 * sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-1.65d+206)) then
tmp = 0.5d0 * sqrt(((im * im) / re))
else if (re <= 3.1d-45) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -1.65e+206) {
tmp = 0.5 * Math.sqrt(((im * im) / re));
} else if (re <= 3.1e-45) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -1.65e+206: tmp = 0.5 * math.sqrt(((im * im) / re)) elif re <= 3.1e-45: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -1.65e+206) tmp = Float64(0.5 * sqrt(Float64(Float64(im * im) / re))); elseif (re <= 3.1e-45) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.65e+206) tmp = 0.5 * sqrt(((im * im) / re)); elseif (re <= 3.1e-45) tmp = 0.5 * sqrt((im * 2.0)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -1.65e+206], N[(0.5 * N[Sqrt[N[(N[(im * im), $MachinePrecision] / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.1e-45], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.65 \cdot 10^{+206}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im \cdot im}{re}}\\
\mathbf{elif}\;re \leq 3.1 \cdot 10^{-45}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -1.64999999999999992e206Initial program 2.2%
+-commutative2.2%
hypot-def38.5%
Simplified38.5%
Taylor expanded in re around -inf 48.0%
*-commutative48.0%
unpow248.0%
associate-/l*58.2%
Simplified58.2%
expm1-log1p-u57.6%
expm1-udef43.4%
*-commutative43.4%
associate-*l*43.4%
associate-/r/43.4%
*-commutative43.4%
metadata-eval43.4%
Applied egg-rr43.4%
expm1-def57.6%
expm1-log1p58.2%
associate-*l*58.2%
associate-*l/58.2%
*-commutative58.2%
neg-mul-158.2%
Simplified58.2%
pow1/258.2%
add-sqr-sqrt58.2%
sqrt-unprod38.6%
swap-sqr33.8%
distribute-frac-neg33.8%
distribute-frac-neg33.8%
sqr-neg33.8%
swap-sqr38.6%
sqrt-unprod32.6%
add-sqr-sqrt32.6%
clear-num32.6%
un-div-inv32.6%
Applied egg-rr32.6%
unpow1/232.6%
associate-/l*32.6%
Simplified32.6%
if -1.64999999999999992e206 < re < 3.1000000000000001e-45Initial program 43.2%
+-commutative43.2%
hypot-def72.0%
Simplified72.0%
Taylor expanded in re around 0 32.2%
*-commutative32.2%
Simplified32.2%
if 3.1000000000000001e-45 < re Initial program 41.4%
+-commutative41.4%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 76.2%
unpow276.2%
rem-square-sqrt77.7%
Simplified77.7%
Final simplification44.1%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re 2.12e-44) (* 0.5 (sqrt (* im 2.0))) (* 0.5 (* 2.0 (sqrt re)))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= 2.12e-44) {
tmp = 0.5 * sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= 2.12d-44) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= 2.12e-44) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= 2.12e-44: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= 2.12e-44) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= 2.12e-44) tmp = 0.5 * sqrt((im * 2.0)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, 2.12e-44], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq 2.12 \cdot 10^{-44}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < 2.1199999999999999e-44Initial program 38.8%
+-commutative38.8%
hypot-def68.5%
Simplified68.5%
Taylor expanded in re around 0 29.4%
*-commutative29.4%
Simplified29.4%
if 2.1199999999999999e-44 < re Initial program 41.4%
+-commutative41.4%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 76.2%
unpow276.2%
rem-square-sqrt77.7%
Simplified77.7%
Final simplification42.0%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (* 0.5 (sqrt (* im 2.0))))
im = abs(im);
double code(double re, double im) {
return 0.5 * sqrt((im * 2.0));
}
NOTE: im should be positive before calling this function
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((im * 2.0d0))
end function
im = Math.abs(im);
public static double code(double re, double im) {
return 0.5 * Math.sqrt((im * 2.0));
}
im = abs(im) def code(re, im): return 0.5 * math.sqrt((im * 2.0))
im = abs(im) function code(re, im) return Float64(0.5 * sqrt(Float64(im * 2.0))) end
im = abs(im) function tmp = code(re, im) tmp = 0.5 * sqrt((im * 2.0)); end
NOTE: im should be positive before calling this function code[re_, im_] := N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
0.5 \cdot \sqrt{im \cdot 2}
\end{array}
Initial program 39.5%
+-commutative39.5%
hypot-def76.7%
Simplified76.7%
Taylor expanded in re around 0 26.6%
*-commutative26.6%
Simplified26.6%
Final simplification26.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 2023278
(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)))))