
(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}
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -1.75e-301) (* 0.5 (* (* im 2.0) (sqrt (/ 0.5 (- (hypot re im) re))))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -1.75e-301) {
tmp = 0.5 * ((im * 2.0) * sqrt((0.5 / (hypot(re, 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 <= -1.75e-301) {
tmp = 0.5 * ((im * 2.0) * Math.sqrt((0.5 / (Math.hypot(re, 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 <= -1.75e-301: tmp = 0.5 * ((im * 2.0) * math.sqrt((0.5 / (math.hypot(re, 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 (re <= -1.75e-301) tmp = Float64(0.5 * Float64(Float64(im * 2.0) * sqrt(Float64(0.5 / Float64(hypot(re, 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 <= -1.75e-301) tmp = 0.5 * ((im * 2.0) * sqrt((0.5 / (hypot(re, 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[re, -1.75e-301], N[(0.5 * N[(N[(im * 2.0), $MachinePrecision] * N[Sqrt[N[(0.5 / N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - 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}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.75 \cdot 10^{-301}:\\
\;\;\;\;0.5 \cdot \left(\left(im \cdot 2\right) \cdot \sqrt{\frac{0.5}{\mathsf{hypot}\left(re, im\right) - re}}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -1.74999999999999996e-301Initial program 27.5%
Simplified56.6%
+-commutative56.6%
distribute-rgt-in56.6%
flip-+27.1%
Applied egg-rr27.1%
Taylor expanded in re around 0 47.4%
*-commutative47.4%
Simplified47.4%
pow1/247.4%
div-inv47.4%
unpow-prod-down55.6%
pow1/255.6%
sqrt-prod55.6%
unpow255.6%
sqrt-prod50.7%
add-sqr-sqrt52.2%
metadata-eval52.2%
distribute-rgt-out--52.2%
Applied egg-rr52.2%
unpow1/252.2%
associate-/r*52.2%
metadata-eval52.2%
Simplified52.2%
if -1.74999999999999996e-301 < re Initial program 50.3%
Simplified100.0%
Final simplification76.6%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -7e-302) (* 0.5 (/ im (sqrt (* 0.5 (- (hypot re im) re))))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -7e-302) {
tmp = 0.5 * (im / sqrt((0.5 * (hypot(re, 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 <= -7e-302) {
tmp = 0.5 * (im / Math.sqrt((0.5 * (Math.hypot(re, 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 <= -7e-302: tmp = 0.5 * (im / math.sqrt((0.5 * (math.hypot(re, 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 (re <= -7e-302) tmp = Float64(0.5 * Float64(im / sqrt(Float64(0.5 * Float64(hypot(re, 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 <= -7e-302) tmp = 0.5 * (im / sqrt((0.5 * (hypot(re, 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[re, -7e-302], N[(0.5 * N[(im / N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - 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}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -7 \cdot 10^{-302}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -7.0000000000000003e-302Initial program 27.5%
Simplified56.6%
+-commutative56.6%
distribute-rgt-in56.6%
flip-+27.1%
Applied egg-rr27.1%
Taylor expanded in re around 0 47.4%
*-commutative47.4%
Simplified47.4%
expm1-log1p-u46.9%
expm1-udef20.8%
associate-/l*20.8%
sqrt-div20.8%
unpow220.8%
sqrt-prod26.1%
add-sqr-sqrt27.6%
div-inv27.6%
distribute-rgt-out--27.6%
metadata-eval27.6%
Applied egg-rr27.6%
expm1-def50.5%
expm1-log1p52.1%
*-commutative52.1%
associate-*r*52.1%
metadata-eval52.1%
Simplified52.1%
if -7.0000000000000003e-302 < re Initial program 50.3%
Simplified100.0%
Final simplification76.6%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -4.6e+75) (* 0.5 (sqrt (/ (- (pow im 2.0)) re))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -4.6e+75) {
tmp = 0.5 * sqrt((-pow(im, 2.0) / 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 <= -4.6e+75) {
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;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -4.6e+75: 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
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -4.6e+75) tmp = Float64(0.5 * sqrt(Float64(Float64(-(im ^ 2.0)) / 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 <= -4.6e+75) tmp = 0.5 * sqrt((-(im ^ 2.0) / 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[re, -4.6e+75], 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}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.6 \cdot 10^{+75}:\\
\;\;\;\;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 re < -4.5999999999999997e75Initial program 12.1%
Simplified35.8%
Taylor expanded in re around -inf 45.3%
mul-1-neg45.3%
distribute-neg-frac45.3%
Simplified45.3%
if -4.5999999999999997e75 < re Initial program 44.6%
Simplified87.5%
Final simplification80.4%
NOTE: im should be positive before calling this function
(FPCore (re im)
:precision binary64
(if (<= re -3e+21)
(* 0.5 (sqrt (/ (- (pow im 2.0)) re)))
(if (<= re 9e+148)
(* 0.5 (sqrt (* 2.0 (+ re im))))
(* 0.5 (* 2.0 (sqrt re))))))im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -3e+21) {
tmp = 0.5 * sqrt((-pow(im, 2.0) / re));
} else if (re <= 9e+148) {
tmp = 0.5 * sqrt((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 <= (-3d+21)) then
tmp = 0.5d0 * sqrt((-(im ** 2.0d0) / re))
else if (re <= 9d+148) then
tmp = 0.5d0 * sqrt((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 <= -3e+21) {
tmp = 0.5 * Math.sqrt((-Math.pow(im, 2.0) / re));
} else if (re <= 9e+148) {
tmp = 0.5 * Math.sqrt((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 <= -3e+21: tmp = 0.5 * math.sqrt((-math.pow(im, 2.0) / re)) elif re <= 9e+148: tmp = 0.5 * math.sqrt((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 <= -3e+21) tmp = Float64(0.5 * sqrt(Float64(Float64(-(im ^ 2.0)) / re))); elseif (re <= 9e+148) tmp = Float64(0.5 * sqrt(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 <= -3e+21) tmp = 0.5 * sqrt((-(im ^ 2.0) / re)); elseif (re <= 9e+148) tmp = 0.5 * sqrt((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, -3e+21], N[(0.5 * N[Sqrt[N[((-N[Power[im, 2.0], $MachinePrecision]) / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 9e+148], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $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 -3 \cdot 10^{+21}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{-{im}^{2}}{re}}\\
\mathbf{elif}\;re \leq 9 \cdot 10^{+148}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -3e21Initial program 12.7%
Simplified35.3%
Taylor expanded in re around -inf 41.5%
mul-1-neg41.5%
distribute-neg-frac41.5%
Simplified41.5%
if -3e21 < re < 8.99999999999999987e148Initial program 57.2%
Simplified91.1%
Taylor expanded in re around 0 36.8%
distribute-lft-out36.8%
*-commutative36.8%
Simplified36.8%
if 8.99999999999999987e148 < re Initial program 4.2%
Simplified100.0%
Taylor expanded in im around 0 75.1%
*-commutative75.1%
unpow275.1%
rem-square-sqrt76.3%
Simplified76.3%
Final simplification43.4%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= im 8.5e-103) (* 0.5 (* 2.0 (sqrt re))) (* 0.5 (sqrt (* 2.0 (+ re im))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (im <= 8.5e-103) {
tmp = 0.5 * (2.0 * sqrt(re));
} else {
tmp = 0.5 * sqrt((2.0 * (re + im)));
}
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 (im <= 8.5d-103) then
tmp = 0.5d0 * (2.0d0 * sqrt(re))
else
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (im <= 8.5e-103) {
tmp = 0.5 * (2.0 * Math.sqrt(re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if im <= 8.5e-103: tmp = 0.5 * (2.0 * math.sqrt(re)) else: tmp = 0.5 * math.sqrt((2.0 * (re + im))) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (im <= 8.5e-103) tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (im <= 8.5e-103) tmp = 0.5 * (2.0 * sqrt(re)); else tmp = 0.5 * sqrt((2.0 * (re + im))); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[im, 8.5e-103], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;im \leq 8.5 \cdot 10^{-103}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\end{array}
\end{array}
if im < 8.50000000000000032e-103Initial program 38.4%
Simplified75.6%
Taylor expanded in im around 0 27.7%
*-commutative27.7%
unpow227.7%
rem-square-sqrt28.1%
Simplified28.1%
if 8.50000000000000032e-103 < im Initial program 40.6%
Simplified84.8%
Taylor expanded in re around 0 72.9%
distribute-lft-out72.9%
*-commutative72.9%
Simplified72.9%
Final simplification43.7%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= im 4.1e-105) (* 0.5 (* 2.0 (sqrt re))) (* 0.5 (sqrt (* im 2.0)))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (im <= 4.1e-105) {
tmp = 0.5 * (2.0 * sqrt(re));
} else {
tmp = 0.5 * sqrt((im * 2.0));
}
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 (im <= 4.1d-105) then
tmp = 0.5d0 * (2.0d0 * sqrt(re))
else
tmp = 0.5d0 * sqrt((im * 2.0d0))
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (im <= 4.1e-105) {
tmp = 0.5 * (2.0 * Math.sqrt(re));
} else {
tmp = 0.5 * Math.sqrt((im * 2.0));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if im <= 4.1e-105: tmp = 0.5 * (2.0 * math.sqrt(re)) else: tmp = 0.5 * math.sqrt((im * 2.0)) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (im <= 4.1e-105) tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); else tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (im <= 4.1e-105) tmp = 0.5 * (2.0 * sqrt(re)); else tmp = 0.5 * sqrt((im * 2.0)); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[im, 4.1e-105], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;im \leq 4.1 \cdot 10^{-105}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\end{array}
\end{array}
if im < 4.1000000000000003e-105Initial program 38.4%
Simplified75.6%
Taylor expanded in im around 0 27.7%
*-commutative27.7%
unpow227.7%
rem-square-sqrt28.1%
Simplified28.1%
if 4.1000000000000003e-105 < im Initial program 40.6%
Simplified84.8%
Taylor expanded in re around 0 71.4%
*-commutative71.4%
Simplified71.4%
Final simplification43.2%
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.2%
Simplified78.8%
Taylor expanded in re around 0 27.1%
*-commutative27.1%
Simplified27.1%
Final simplification27.1%
(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 2023298
(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)))))