
(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 (<= (sqrt (* 2.0 (+ 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 (sqrt((2.0 * (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 (Math.sqrt((2.0 * (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 math.sqrt((2.0 * (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 (sqrt(Float64(2.0 * 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 (sqrt((2.0 * (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[Sqrt[N[(2.0 * N[(re + N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $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}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \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 (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 12.1%
+-commutative12.1%
hypot-def12.1%
Simplified12.1%
Taylor expanded in re around -inf 60.1%
+-commutative60.1%
mul-1-neg60.1%
unsub-neg60.1%
*-commutative60.1%
unpow260.1%
associate-/l*61.7%
Simplified61.7%
Taylor expanded in im around 0 63.4%
mul-1-neg63.4%
unpow263.4%
associate-*r/64.8%
distribute-rgt-neg-in64.8%
distribute-neg-frac64.8%
Simplified64.8%
sqrt-prod55.3%
Applied egg-rr55.3%
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 45.1%
+-commutative45.1%
hypot-def90.7%
Simplified90.7%
Final simplification86.6%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re -1.56e+65) (* 0.5 (sqrt (* (- im) (/ 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.56e+65) {
tmp = 0.5 * sqrt((-im * (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.56e+65) {
tmp = 0.5 * Math.sqrt((-im * (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.56e+65: tmp = 0.5 * math.sqrt((-im * (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.56e+65) tmp = Float64(0.5 * sqrt(Float64(Float64(-im) * 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 <= -1.56e+65) tmp = 0.5 * sqrt((-im * (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.56e+65], 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}
im = |im|\\
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.56 \cdot 10^{+65}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \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 < -1.5599999999999999e65Initial program 10.9%
+-commutative10.9%
hypot-def29.8%
Simplified29.8%
Taylor expanded in re around -inf 44.3%
+-commutative44.3%
mul-1-neg44.3%
unsub-neg44.3%
*-commutative44.3%
unpow244.3%
associate-/l*44.2%
Simplified44.2%
Taylor expanded in im around 0 56.9%
mul-1-neg56.9%
unpow256.9%
associate-*r/61.7%
distribute-rgt-neg-in61.7%
distribute-neg-frac61.7%
Simplified61.7%
if -1.5599999999999999e65 < re Initial program 47.4%
+-commutative47.4%
hypot-def92.0%
Simplified92.0%
Final simplification86.9%
NOTE: im should be positive before calling this function
(FPCore (re im)
:precision binary64
(let* ((t_0 (* 2.0 (+ re im))))
(if (<= re -3.6e+62)
(* 0.5 (sqrt (* (- im) (/ im re))))
(if (<= re 3.55e-9)
(* 0.5 (sqrt (+ (* re (/ re im)) t_0)))
(if (or (<= re 5e+43) (not (<= re 1.12e+85)))
(* 0.5 (* 2.0 (sqrt re)))
(* 0.5 (sqrt t_0)))))))im = abs(im);
double code(double re, double im) {
double t_0 = 2.0 * (re + im);
double tmp;
if (re <= -3.6e+62) {
tmp = 0.5 * sqrt((-im * (im / re)));
} else if (re <= 3.55e-9) {
tmp = 0.5 * sqrt(((re * (re / im)) + t_0));
} else if ((re <= 5e+43) || !(re <= 1.12e+85)) {
tmp = 0.5 * (2.0 * sqrt(re));
} else {
tmp = 0.5 * sqrt(t_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) :: t_0
real(8) :: tmp
t_0 = 2.0d0 * (re + im)
if (re <= (-3.6d+62)) then
tmp = 0.5d0 * sqrt((-im * (im / re)))
else if (re <= 3.55d-9) then
tmp = 0.5d0 * sqrt(((re * (re / im)) + t_0))
else if ((re <= 5d+43) .or. (.not. (re <= 1.12d+85))) then
tmp = 0.5d0 * (2.0d0 * sqrt(re))
else
tmp = 0.5d0 * sqrt(t_0)
end if
code = tmp
end function
im = Math.abs(im);
public static double code(double re, double im) {
double t_0 = 2.0 * (re + im);
double tmp;
if (re <= -3.6e+62) {
tmp = 0.5 * Math.sqrt((-im * (im / re)));
} else if (re <= 3.55e-9) {
tmp = 0.5 * Math.sqrt(((re * (re / im)) + t_0));
} else if ((re <= 5e+43) || !(re <= 1.12e+85)) {
tmp = 0.5 * (2.0 * Math.sqrt(re));
} else {
tmp = 0.5 * Math.sqrt(t_0);
}
return tmp;
}
im = abs(im) def code(re, im): t_0 = 2.0 * (re + im) tmp = 0 if re <= -3.6e+62: tmp = 0.5 * math.sqrt((-im * (im / re))) elif re <= 3.55e-9: tmp = 0.5 * math.sqrt(((re * (re / im)) + t_0)) elif (re <= 5e+43) or not (re <= 1.12e+85): tmp = 0.5 * (2.0 * math.sqrt(re)) else: tmp = 0.5 * math.sqrt(t_0) return tmp
im = abs(im) function code(re, im) t_0 = Float64(2.0 * Float64(re + im)) tmp = 0.0 if (re <= -3.6e+62) tmp = Float64(0.5 * sqrt(Float64(Float64(-im) * Float64(im / re)))); elseif (re <= 3.55e-9) tmp = Float64(0.5 * sqrt(Float64(Float64(re * Float64(re / im)) + t_0))); elseif ((re <= 5e+43) || !(re <= 1.12e+85)) tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); else tmp = Float64(0.5 * sqrt(t_0)); end return tmp end
im = abs(im) function tmp_2 = code(re, im) t_0 = 2.0 * (re + im); tmp = 0.0; if (re <= -3.6e+62) tmp = 0.5 * sqrt((-im * (im / re))); elseif (re <= 3.55e-9) tmp = 0.5 * sqrt(((re * (re / im)) + t_0)); elseif ((re <= 5e+43) || ~((re <= 1.12e+85))) tmp = 0.5 * (2.0 * sqrt(re)); else tmp = 0.5 * sqrt(t_0); end tmp_2 = tmp; end
NOTE: im should be positive before calling this function
code[re_, im_] := Block[{t$95$0 = N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -3.6e+62], N[(0.5 * N[Sqrt[N[((-im) * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.55e-9], N[(0.5 * N[Sqrt[N[(N[(re * N[(re / im), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 5e+43], N[Not[LessEqual[re, 1.12e+85]], $MachinePrecision]], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[t$95$0], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
im = |im|\\
\\
\begin{array}{l}
t_0 := 2 \cdot \left(re + im\right)\\
\mathbf{if}\;re \leq -3.6 \cdot 10^{+62}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\
\mathbf{elif}\;re \leq 3.55 \cdot 10^{-9}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot \frac{re}{im} + t_0}\\
\mathbf{elif}\;re \leq 5 \cdot 10^{+43} \lor \neg \left(re \leq 1.12 \cdot 10^{+85}\right):\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{t_0}\\
\end{array}
\end{array}
if re < -3.6e62Initial program 10.8%
+-commutative10.8%
hypot-def29.3%
Simplified29.3%
Taylor expanded in re around -inf 43.4%
+-commutative43.4%
mul-1-neg43.4%
unsub-neg43.4%
*-commutative43.4%
unpow243.4%
associate-/l*43.4%
Simplified43.4%
Taylor expanded in im around 0 55.8%
mul-1-neg55.8%
unpow255.8%
associate-*r/60.5%
distribute-rgt-neg-in60.5%
distribute-neg-frac60.5%
Simplified60.5%
if -3.6e62 < re < 3.54999999999999994e-9Initial program 49.2%
+-commutative49.2%
hypot-def88.5%
Simplified88.5%
Taylor expanded in re around 0 44.6%
unpow244.6%
distribute-lft-out44.6%
Simplified44.6%
associate-/l*44.5%
associate-/r/44.5%
Applied egg-rr44.5%
if 3.54999999999999994e-9 < re < 5.0000000000000004e43 or 1.11999999999999993e85 < re Initial program 42.5%
+-commutative42.5%
hypot-def98.6%
Simplified98.6%
Taylor expanded in im around 0 84.3%
unpow284.3%
rem-square-sqrt86.0%
Simplified86.0%
if 5.0000000000000004e43 < re < 1.11999999999999993e85Initial program 61.6%
+-commutative61.6%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around 0 46.1%
distribute-lft-out46.1%
+-commutative46.1%
*-commutative46.1%
+-commutative46.1%
Simplified46.1%
Final simplification58.7%
NOTE: im should be positive before calling this function
(FPCore (re im)
:precision binary64
(if (<= re -2.35e+63)
(* 0.5 (sqrt (/ (- im) (/ re im))))
(if (or (<= re 3.3e-9) (and (not (<= re 1.7e+47)) (<= re 1.4e+84)))
(* 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 <= -2.35e+63) {
tmp = 0.5 * sqrt((-im / (re / im)));
} else if ((re <= 3.3e-9) || (!(re <= 1.7e+47) && (re <= 1.4e+84))) {
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 <= (-2.35d+63)) then
tmp = 0.5d0 * sqrt((-im / (re / im)))
else if ((re <= 3.3d-9) .or. (.not. (re <= 1.7d+47)) .and. (re <= 1.4d+84)) 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 <= -2.35e+63) {
tmp = 0.5 * Math.sqrt((-im / (re / im)));
} else if ((re <= 3.3e-9) || (!(re <= 1.7e+47) && (re <= 1.4e+84))) {
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 <= -2.35e+63: tmp = 0.5 * math.sqrt((-im / (re / im))) elif (re <= 3.3e-9) or (not (re <= 1.7e+47) and (re <= 1.4e+84)): 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 <= -2.35e+63) tmp = Float64(0.5 * sqrt(Float64(Float64(-im) / Float64(re / im)))); elseif ((re <= 3.3e-9) || (!(re <= 1.7e+47) && (re <= 1.4e+84))) 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 <= -2.35e+63) tmp = 0.5 * sqrt((-im / (re / im))); elseif ((re <= 3.3e-9) || (~((re <= 1.7e+47)) && (re <= 1.4e+84))) 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, -2.35e+63], N[(0.5 * N[Sqrt[N[((-im) / N[(re / im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 3.3e-9], And[N[Not[LessEqual[re, 1.7e+47]], $MachinePrecision], LessEqual[re, 1.4e+84]]], 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 -2.35 \cdot 10^{+63}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{-im}{\frac{re}{im}}}\\
\mathbf{elif}\;re \leq 3.3 \cdot 10^{-9} \lor \neg \left(re \leq 1.7 \cdot 10^{+47}\right) \land re \leq 1.4 \cdot 10^{+84}:\\
\;\;\;\;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 < -2.3500000000000001e63Initial program 10.8%
+-commutative10.8%
hypot-def29.3%
Simplified29.3%
Taylor expanded in re around -inf 43.4%
+-commutative43.4%
mul-1-neg43.4%
unsub-neg43.4%
*-commutative43.4%
unpow243.4%
associate-/l*43.4%
Simplified43.4%
Taylor expanded in im around 0 55.8%
mul-1-neg55.8%
unpow255.8%
associate-*r/60.5%
distribute-rgt-neg-in60.5%
distribute-neg-frac60.5%
Simplified60.5%
Taylor expanded in im around 0 55.8%
mul-1-neg55.8%
distribute-frac-neg55.8%
unpow255.8%
distribute-lft-neg-in55.8%
associate-/l*60.4%
distribute-neg-frac60.4%
Simplified60.4%
if -2.3500000000000001e63 < re < 3.30000000000000018e-9 or 1.6999999999999999e47 < re < 1.39999999999999991e84Initial program 50.1%
+-commutative50.1%
hypot-def89.3%
Simplified89.3%
Taylor expanded in re around 0 45.3%
distribute-lft-out45.3%
+-commutative45.3%
*-commutative45.3%
+-commutative45.3%
Simplified45.3%
if 3.30000000000000018e-9 < re < 1.6999999999999999e47 or 1.39999999999999991e84 < re Initial program 42.5%
+-commutative42.5%
hypot-def98.6%
Simplified98.6%
Taylor expanded in im around 0 84.3%
unpow284.3%
rem-square-sqrt86.0%
Simplified86.0%
Final simplification59.0%
NOTE: im should be positive before calling this function
(FPCore (re im)
:precision binary64
(if (<= re -3.4e+62)
(* 0.5 (sqrt (* (- im) (/ im re))))
(if (or (<= re 3.75e-9) (and (not (<= re 1.9e+46)) (<= re 1.25e+84)))
(* 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 <= -3.4e+62) {
tmp = 0.5 * sqrt((-im * (im / re)));
} else if ((re <= 3.75e-9) || (!(re <= 1.9e+46) && (re <= 1.25e+84))) {
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 <= (-3.4d+62)) then
tmp = 0.5d0 * sqrt((-im * (im / re)))
else if ((re <= 3.75d-9) .or. (.not. (re <= 1.9d+46)) .and. (re <= 1.25d+84)) 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 <= -3.4e+62) {
tmp = 0.5 * Math.sqrt((-im * (im / re)));
} else if ((re <= 3.75e-9) || (!(re <= 1.9e+46) && (re <= 1.25e+84))) {
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 <= -3.4e+62: tmp = 0.5 * math.sqrt((-im * (im / re))) elif (re <= 3.75e-9) or (not (re <= 1.9e+46) and (re <= 1.25e+84)): 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 <= -3.4e+62) tmp = Float64(0.5 * sqrt(Float64(Float64(-im) * Float64(im / re)))); elseif ((re <= 3.75e-9) || (!(re <= 1.9e+46) && (re <= 1.25e+84))) 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 <= -3.4e+62) tmp = 0.5 * sqrt((-im * (im / re))); elseif ((re <= 3.75e-9) || (~((re <= 1.9e+46)) && (re <= 1.25e+84))) 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, -3.4e+62], N[(0.5 * N[Sqrt[N[((-im) * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 3.75e-9], And[N[Not[LessEqual[re, 1.9e+46]], $MachinePrecision], LessEqual[re, 1.25e+84]]], 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.4 \cdot 10^{+62}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(-im\right) \cdot \frac{im}{re}}\\
\mathbf{elif}\;re \leq 3.75 \cdot 10^{-9} \lor \neg \left(re \leq 1.9 \cdot 10^{+46}\right) \land re \leq 1.25 \cdot 10^{+84}:\\
\;\;\;\;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 < -3.40000000000000014e62Initial program 10.8%
+-commutative10.8%
hypot-def29.3%
Simplified29.3%
Taylor expanded in re around -inf 43.4%
+-commutative43.4%
mul-1-neg43.4%
unsub-neg43.4%
*-commutative43.4%
unpow243.4%
associate-/l*43.4%
Simplified43.4%
Taylor expanded in im around 0 55.8%
mul-1-neg55.8%
unpow255.8%
associate-*r/60.5%
distribute-rgt-neg-in60.5%
distribute-neg-frac60.5%
Simplified60.5%
if -3.40000000000000014e62 < re < 3.74999999999999966e-9 or 1.9e46 < re < 1.25e84Initial program 50.1%
+-commutative50.1%
hypot-def89.3%
Simplified89.3%
Taylor expanded in re around 0 45.3%
distribute-lft-out45.3%
+-commutative45.3%
*-commutative45.3%
+-commutative45.3%
Simplified45.3%
if 3.74999999999999966e-9 < re < 1.9e46 or 1.25e84 < re Initial program 42.5%
+-commutative42.5%
hypot-def98.6%
Simplified98.6%
Taylor expanded in im around 0 84.3%
unpow284.3%
rem-square-sqrt86.0%
Simplified86.0%
Final simplification59.0%
NOTE: im should be positive before calling this function
(FPCore (re im)
:precision binary64
(if (<= re 3.7e-9)
(* 0.5 (sqrt (* 2.0 im)))
(if (or (<= re 1.4e+47) (not (<= re 1.6e+84)))
(* 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 (re <= 3.7e-9) {
tmp = 0.5 * sqrt((2.0 * im));
} else if ((re <= 1.4e+47) || !(re <= 1.6e+84)) {
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 (re <= 3.7d-9) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else if ((re <= 1.4d+47) .or. (.not. (re <= 1.6d+84))) 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 (re <= 3.7e-9) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else if ((re <= 1.4e+47) || !(re <= 1.6e+84)) {
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 re <= 3.7e-9: tmp = 0.5 * math.sqrt((2.0 * im)) elif (re <= 1.4e+47) or not (re <= 1.6e+84): 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 (re <= 3.7e-9) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); elseif ((re <= 1.4e+47) || !(re <= 1.6e+84)) 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 (re <= 3.7e-9) tmp = 0.5 * sqrt((2.0 * im)); elseif ((re <= 1.4e+47) || ~((re <= 1.6e+84))) 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[re, 3.7e-9], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 1.4e+47], N[Not[LessEqual[re, 1.6e+84]], $MachinePrecision]], 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}\;re \leq 3.7 \cdot 10^{-9}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{elif}\;re \leq 1.4 \cdot 10^{+47} \lor \neg \left(re \leq 1.6 \cdot 10^{+84}\right):\\
\;\;\;\;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 re < 3.7e-9Initial program 39.6%
+-commutative39.6%
hypot-def73.7%
Simplified73.7%
Taylor expanded in re around 0 35.5%
*-commutative35.5%
Simplified35.5%
if 3.7e-9 < re < 1.39999999999999994e47 or 1.60000000000000005e84 < re Initial program 42.5%
+-commutative42.5%
hypot-def98.6%
Simplified98.6%
Taylor expanded in im around 0 84.3%
unpow284.3%
rem-square-sqrt86.0%
Simplified86.0%
if 1.39999999999999994e47 < re < 1.60000000000000005e84Initial program 61.6%
+-commutative61.6%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around 0 46.1%
distribute-lft-out46.1%
+-commutative46.1%
*-commutative46.1%
+-commutative46.1%
Simplified46.1%
Final simplification49.7%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (if (<= re 2.65e-9) (* 0.5 (sqrt (* 2.0 im))) (* 0.5 (* 2.0 (sqrt re)))))
im = abs(im);
double code(double re, double im) {
double tmp;
if (re <= 2.65e-9) {
tmp = 0.5 * sqrt((2.0 * 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 <= 2.65d-9) then
tmp = 0.5d0 * sqrt((2.0d0 * 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 <= 2.65e-9) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
im = abs(im) def code(re, im): tmp = 0 if re <= 2.65e-9: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
im = abs(im) function code(re, im) tmp = 0.0 if (re <= 2.65e-9) tmp = Float64(0.5 * sqrt(Float64(2.0 * 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 <= 2.65e-9) tmp = 0.5 * sqrt((2.0 * 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, 2.65e-9], N[(0.5 * N[Sqrt[N[(2.0 * im), $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.65 \cdot 10^{-9}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < 2.65000000000000015e-9Initial program 39.6%
+-commutative39.6%
hypot-def73.7%
Simplified73.7%
Taylor expanded in re around 0 35.5%
*-commutative35.5%
Simplified35.5%
if 2.65000000000000015e-9 < re Initial program 44.9%
+-commutative44.9%
hypot-def98.8%
Simplified98.8%
Taylor expanded in im around 0 78.2%
unpow278.2%
rem-square-sqrt79.7%
Simplified79.7%
Final simplification49.3%
NOTE: im should be positive before calling this function (FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 im))))
im = abs(im);
double code(double re, double im) {
return 0.5 * sqrt((2.0 * im));
}
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((2.0d0 * im))
end function
im = Math.abs(im);
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * im));
}
im = abs(im) def code(re, im): return 0.5 * math.sqrt((2.0 * im))
im = abs(im) function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * im))) end
im = abs(im) function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * im)); end
NOTE: im should be positive before calling this function code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im = |im|\\
\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Initial program 41.3%
+-commutative41.3%
hypot-def81.5%
Simplified81.5%
Taylor expanded in re around 0 28.4%
*-commutative28.4%
Simplified28.4%
Final simplification28.4%
(FPCore (re im)
:precision binary64
(let* ((t_0 (sqrt (+ (* re re) (* im im)))))
(if (< re 0.0)
(* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- t_0 re)))))
(* 0.5 (sqrt (* 2.0 (+ t_0 re)))))))
double code(double re, double im) {
double t_0 = sqrt(((re * re) + (im * im)));
double tmp;
if (re < 0.0) {
tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
} else {
tmp = 0.5 * sqrt((2.0 * (t_0 + re)));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt(((re * re) + (im * im)))
if (re < 0.0d0) then
tmp = 0.5d0 * (sqrt(2.0d0) * sqrt(((im * im) / (t_0 - re))))
else
tmp = 0.5d0 * sqrt((2.0d0 * (t_0 + re)))
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = Math.sqrt(((re * re) + (im * im)));
double tmp;
if (re < 0.0) {
tmp = 0.5 * (Math.sqrt(2.0) * Math.sqrt(((im * im) / (t_0 - re))));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (t_0 + re)));
}
return tmp;
}
def code(re, im): t_0 = math.sqrt(((re * re) + (im * im))) tmp = 0 if re < 0.0: tmp = 0.5 * (math.sqrt(2.0) * math.sqrt(((im * im) / (t_0 - re)))) else: tmp = 0.5 * math.sqrt((2.0 * (t_0 + re))) return tmp
function code(re, im) t_0 = sqrt(Float64(Float64(re * re) + Float64(im * im))) tmp = 0.0 if (re < 0.0) tmp = Float64(0.5 * Float64(sqrt(2.0) * sqrt(Float64(Float64(im * im) / Float64(t_0 - re))))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(t_0 + re)))); end return tmp end
function tmp_2 = code(re, im) t_0 = sqrt(((re * re) + (im * im))); tmp = 0.0; if (re < 0.0) tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re)))); else tmp = 0.5 * sqrt((2.0 * (t_0 + re))); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[Less[re, 0.0], N[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[N[(N[(im * im), $MachinePrecision] / N[(t$95$0 - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(t$95$0 + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{re \cdot re + im \cdot im}\\
\mathbf{if}\;re < 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t_0 - re}}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t_0 + re\right)}\\
\end{array}
\end{array}
herbie shell --seed 2023189
(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)))))