
(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 6 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 -880.0) (* 0.5 (/ im (sqrt (- re)))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -880.0) {
tmp = 0.5 * (im / sqrt(-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 <= -880.0) {
tmp = 0.5 * (im / Math.sqrt(-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 <= -880.0: tmp = 0.5 * (im / math.sqrt(-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 <= -880.0) tmp = Float64(0.5 * Float64(im / sqrt(Float64(-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 <= -880.0) tmp = 0.5 * (im / sqrt(-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, -880.0], N[(0.5 * N[(im / N[Sqrt[(-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 -880:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -880Initial program 18.5%
sqr-neg18.5%
+-commutative18.5%
sqr-neg18.5%
distribute-rgt-in18.5%
cancel-sign-sub18.5%
distribute-rgt-out--18.5%
sub-neg18.5%
remove-double-neg18.5%
hypot-def30.9%
Simplified30.9%
Taylor expanded in re around -inf 41.7%
associate-*r/41.7%
neg-mul-141.7%
unpow241.7%
distribute-rgt-neg-in41.7%
Simplified41.7%
frac-2neg41.7%
sqrt-div52.9%
distribute-rgt-neg-out52.9%
remove-double-neg52.9%
sqrt-unprod43.1%
add-sqr-sqrt46.5%
Applied egg-rr46.5%
if -880 < re Initial program 48.4%
sqr-neg48.4%
+-commutative48.4%
sqr-neg48.4%
distribute-rgt-in48.4%
cancel-sign-sub48.4%
distribute-rgt-out--48.4%
sub-neg48.4%
remove-double-neg48.4%
hypot-def92.7%
Simplified92.7%
Final simplification85.1%
NOTE: im should be positive before calling this function
(FPCore (re im)
:precision binary64
(if (<= re -19000000000.0)
(* 0.5 (/ im (sqrt (- re))))
(if (<= re 6.8e-10)
(* 0.5 (sqrt (* 2.0 (+ re im))))
(* 0.5 (sqrt (* 2.0 (+ re (+ re (* 0.5 (* im (/ im re)))))))))))im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= -19000000000.0) {
tmp = 0.5 * (im / sqrt(-re));
} else if (re <= 6.8e-10) {
tmp = 0.5 * sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * sqrt((2.0 * (re + (re + (0.5 * (im * (im / 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 <= (-19000000000.0d0)) then
tmp = 0.5d0 * (im / sqrt(-re))
else if (re <= 6.8d-10) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = 0.5d0 * sqrt((2.0d0 * (re + (re + (0.5d0 * (im * (im / re)))))))
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double tmp;
if (re <= -19000000000.0) {
tmp = 0.5 * (im / Math.sqrt(-re));
} else if (re <= 6.8e-10) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + (re + (0.5 * (im * (im / re)))))));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= -19000000000.0: tmp = 0.5 * (im / math.sqrt(-re)) elif re <= 6.8e-10: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = 0.5 * math.sqrt((2.0 * (re + (re + (0.5 * (im * (im / re))))))) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= -19000000000.0) tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re)))); elseif (re <= 6.8e-10) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + Float64(re + Float64(0.5 * Float64(im * Float64(im / re)))))))); end return tmp end
im = abs(im) function tmp_2 = code(re, im) tmp = 0.0; if (re <= -19000000000.0) tmp = 0.5 * (im / sqrt(-re)); elseif (re <= 6.8e-10) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = 0.5 * sqrt((2.0 * (re + (re + (0.5 * (im * (im / re))))))); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function code[re_, im_] := If[LessEqual[re, -19000000000.0], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 6.8e-10], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[(re + N[(0.5 * N[(im * N[(im / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -19000000000:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\
\mathbf{elif}\;re \leq 6.8 \cdot 10^{-10}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \left(re + 0.5 \cdot \left(im \cdot \frac{im}{re}\right)\right)\right)}\\
\end{array}
\end{array}
if re < -1.9e10Initial program 18.5%
sqr-neg18.5%
+-commutative18.5%
sqr-neg18.5%
distribute-rgt-in18.5%
cancel-sign-sub18.5%
distribute-rgt-out--18.5%
sub-neg18.5%
remove-double-neg18.5%
hypot-def30.9%
Simplified30.9%
Taylor expanded in re around -inf 41.7%
associate-*r/41.7%
neg-mul-141.7%
unpow241.7%
distribute-rgt-neg-in41.7%
Simplified41.7%
frac-2neg41.7%
sqrt-div52.9%
distribute-rgt-neg-out52.9%
remove-double-neg52.9%
sqrt-unprod43.1%
add-sqr-sqrt46.5%
Applied egg-rr46.5%
if -1.9e10 < re < 6.8000000000000003e-10Initial program 54.4%
sqr-neg54.4%
+-commutative54.4%
sqr-neg54.4%
distribute-rgt-in54.4%
cancel-sign-sub54.4%
distribute-rgt-out--54.4%
sub-neg54.4%
remove-double-neg54.4%
hypot-def88.3%
Simplified88.3%
Taylor expanded in re around 0 44.2%
distribute-lft-out44.2%
*-commutative44.2%
Simplified44.2%
if 6.8000000000000003e-10 < re Initial program 38.6%
hypot-udef100.0%
add-sqr-sqrt99.8%
pow299.8%
Applied egg-rr99.8%
Taylor expanded in im around 0 68.2%
unpow268.2%
associate-*r/76.7%
Simplified76.7%
Final simplification54.9%
NOTE: im should be positive before calling this function
(FPCore (re im)
:precision binary64
(if (<= re -29500.0)
(* 0.5 (/ im (sqrt (- re))))
(if (<= re 1.22e-14)
(* 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 <= -29500.0) {
tmp = 0.5 * (im / sqrt(-re));
} else if (re <= 1.22e-14) {
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 <= (-29500.0d0)) then
tmp = 0.5d0 * (im / sqrt(-re))
else if (re <= 1.22d-14) 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 <= -29500.0) {
tmp = 0.5 * (im / Math.sqrt(-re));
} else if (re <= 1.22e-14) {
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 <= -29500.0: tmp = 0.5 * (im / math.sqrt(-re)) elif re <= 1.22e-14: 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 <= -29500.0) tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re)))); elseif (re <= 1.22e-14) 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 <= -29500.0) tmp = 0.5 * (im / sqrt(-re)); elseif (re <= 1.22e-14) 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, -29500.0], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.22e-14], 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 -29500:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\
\mathbf{elif}\;re \leq 1.22 \cdot 10^{-14}:\\
\;\;\;\;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 < -29500Initial program 18.5%
sqr-neg18.5%
+-commutative18.5%
sqr-neg18.5%
distribute-rgt-in18.5%
cancel-sign-sub18.5%
distribute-rgt-out--18.5%
sub-neg18.5%
remove-double-neg18.5%
hypot-def30.9%
Simplified30.9%
Taylor expanded in re around -inf 41.7%
associate-*r/41.7%
neg-mul-141.7%
unpow241.7%
distribute-rgt-neg-in41.7%
Simplified41.7%
frac-2neg41.7%
sqrt-div52.9%
distribute-rgt-neg-out52.9%
remove-double-neg52.9%
sqrt-unprod43.1%
add-sqr-sqrt46.5%
Applied egg-rr46.5%
if -29500 < re < 1.21999999999999994e-14Initial program 54.4%
sqr-neg54.4%
+-commutative54.4%
sqr-neg54.4%
distribute-rgt-in54.4%
cancel-sign-sub54.4%
distribute-rgt-out--54.4%
sub-neg54.4%
remove-double-neg54.4%
hypot-def88.3%
Simplified88.3%
Taylor expanded in re around 0 44.2%
distribute-lft-out44.2%
*-commutative44.2%
Simplified44.2%
if 1.21999999999999994e-14 < re Initial program 38.6%
sqr-neg38.6%
+-commutative38.6%
sqr-neg38.6%
distribute-rgt-in38.6%
cancel-sign-sub38.6%
distribute-rgt-out--38.6%
sub-neg38.6%
remove-double-neg38.6%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 74.7%
*-commutative74.7%
unpow274.7%
rem-square-sqrt76.2%
Simplified76.2%
Final simplification54.7%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -16500000000.0) (* 0.5 (/ im (sqrt (- re)))) (if (<= re 1.36e-12) (* 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 <= -16500000000.0) {
tmp = 0.5 * (im / sqrt(-re));
} else if (re <= 1.36e-12) {
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 <= (-16500000000.0d0)) then
tmp = 0.5d0 * (im / sqrt(-re))
else if (re <= 1.36d-12) 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 <= -16500000000.0) {
tmp = 0.5 * (im / Math.sqrt(-re));
} else if (re <= 1.36e-12) {
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 <= -16500000000.0: tmp = 0.5 * (im / math.sqrt(-re)) elif re <= 1.36e-12: 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 <= -16500000000.0) tmp = Float64(0.5 * Float64(im / sqrt(Float64(-re)))); elseif (re <= 1.36e-12) 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 <= -16500000000.0) tmp = 0.5 * (im / sqrt(-re)); elseif (re <= 1.36e-12) 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, -16500000000.0], N[(0.5 * N[(im / N[Sqrt[(-re)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.36e-12], 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 -16500000000:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{-re}}\\
\mathbf{elif}\;re \leq 1.36 \cdot 10^{-12}:\\
\;\;\;\;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.65e10Initial program 18.5%
sqr-neg18.5%
+-commutative18.5%
sqr-neg18.5%
distribute-rgt-in18.5%
cancel-sign-sub18.5%
distribute-rgt-out--18.5%
sub-neg18.5%
remove-double-neg18.5%
hypot-def30.9%
Simplified30.9%
Taylor expanded in re around -inf 41.7%
associate-*r/41.7%
neg-mul-141.7%
unpow241.7%
distribute-rgt-neg-in41.7%
Simplified41.7%
frac-2neg41.7%
sqrt-div52.9%
distribute-rgt-neg-out52.9%
remove-double-neg52.9%
sqrt-unprod43.1%
add-sqr-sqrt46.5%
Applied egg-rr46.5%
if -1.65e10 < re < 1.36000000000000006e-12Initial program 54.4%
sqr-neg54.4%
+-commutative54.4%
sqr-neg54.4%
distribute-rgt-in54.4%
cancel-sign-sub54.4%
distribute-rgt-out--54.4%
sub-neg54.4%
remove-double-neg54.4%
hypot-def88.3%
Simplified88.3%
Taylor expanded in re around 0 43.7%
*-commutative43.7%
Simplified43.7%
if 1.36000000000000006e-12 < re Initial program 38.6%
sqr-neg38.6%
+-commutative38.6%
sqr-neg38.6%
distribute-rgt-in38.6%
cancel-sign-sub38.6%
distribute-rgt-out--38.6%
sub-neg38.6%
remove-double-neg38.6%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 74.7%
*-commutative74.7%
unpow274.7%
rem-square-sqrt76.2%
Simplified76.2%
Final simplification54.4%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re 2.6e-16) (* 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.6e-16) {
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.6d-16) 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.6e-16) {
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.6e-16: 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.6e-16) 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.6e-16) 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.6e-16], 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.6 \cdot 10^{-16}:\\
\;\;\;\;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.5999999999999998e-16Initial program 45.8%
sqr-neg45.8%
+-commutative45.8%
sqr-neg45.8%
distribute-rgt-in45.8%
cancel-sign-sub45.8%
distribute-rgt-out--45.8%
sub-neg45.8%
remove-double-neg45.8%
hypot-def74.5%
Simplified74.5%
Taylor expanded in re around 0 35.4%
*-commutative35.4%
Simplified35.4%
if 2.5999999999999998e-16 < re Initial program 38.6%
sqr-neg38.6%
+-commutative38.6%
sqr-neg38.6%
distribute-rgt-in38.6%
cancel-sign-sub38.6%
distribute-rgt-out--38.6%
sub-neg38.6%
remove-double-neg38.6%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 74.7%
*-commutative74.7%
unpow274.7%
rem-square-sqrt76.2%
Simplified76.2%
Final simplification48.3%
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 43.5%
sqr-neg43.5%
+-commutative43.5%
sqr-neg43.5%
distribute-rgt-in43.5%
cancel-sign-sub43.5%
distribute-rgt-out--43.5%
sub-neg43.5%
remove-double-neg43.5%
hypot-def82.6%
Simplified82.6%
Taylor expanded in re around 0 29.0%
*-commutative29.0%
Simplified29.0%
Final simplification29.0%
(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 2023283
(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)))))