
(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}
(FPCore (re im) :precision binary64 (if (<= (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))) 0.0) (* 0.5 (sqrt (* 2.0 (* (/ im (/ re im)) -0.5)))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re 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((2.0 * ((im / (re / im)) * -0.5)));
} else {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
}
return tmp;
}
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((2.0 * ((im / (re / im)) * -0.5)));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
}
return tmp;
}
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((2.0 * ((im / (re / im)) * -0.5))) else: tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) return tmp
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 * sqrt(Float64(2.0 * Float64(Float64(im / Float64(re / im)) * -0.5)))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (sqrt((2.0 * (re + sqrt(((re * re) + (im * im)))))) <= 0.0) tmp = 0.5 * sqrt((2.0 * ((im / (re / im)) * -0.5))); else tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); end tmp_2 = tmp; end
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[Sqrt[N[(2.0 * N[(N[(im / N[(re / im), $MachinePrecision]), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im}{\frac{re}{im}} \cdot -0.5\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 4.9%
+-commutative4.9%
hypot-def4.9%
Simplified4.9%
Taylor expanded in re around -inf 62.6%
*-commutative62.6%
unpow262.6%
associate-/l*65.5%
Simplified65.5%
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 43.7%
+-commutative43.7%
hypot-def90.7%
Simplified90.7%
Final simplification87.5%
(FPCore (re im)
:precision binary64
(let* ((t_0 (* 2.0 (+ re im))) (t_1 (* 0.5 (* 2.0 (sqrt re)))))
(if (<= im -1.08e-111)
(* 0.5 (sqrt (* im -2.0)))
(if (<= im 1.65e-60)
t_1
(if (<= im 0.045)
(* 0.5 (sqrt (+ (/ (* re re) im) t_0)))
(if (<= im 7.5e+43) t_1 (* 0.5 (sqrt t_0))))))))
double code(double re, double im) {
double t_0 = 2.0 * (re + im);
double t_1 = 0.5 * (2.0 * sqrt(re));
double tmp;
if (im <= -1.08e-111) {
tmp = 0.5 * sqrt((im * -2.0));
} else if (im <= 1.65e-60) {
tmp = t_1;
} else if (im <= 0.045) {
tmp = 0.5 * sqrt((((re * re) / im) + t_0));
} else if (im <= 7.5e+43) {
tmp = t_1;
} else {
tmp = 0.5 * sqrt(t_0);
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = 2.0d0 * (re + im)
t_1 = 0.5d0 * (2.0d0 * sqrt(re))
if (im <= (-1.08d-111)) then
tmp = 0.5d0 * sqrt((im * (-2.0d0)))
else if (im <= 1.65d-60) then
tmp = t_1
else if (im <= 0.045d0) then
tmp = 0.5d0 * sqrt((((re * re) / im) + t_0))
else if (im <= 7.5d+43) then
tmp = t_1
else
tmp = 0.5d0 * sqrt(t_0)
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = 2.0 * (re + im);
double t_1 = 0.5 * (2.0 * Math.sqrt(re));
double tmp;
if (im <= -1.08e-111) {
tmp = 0.5 * Math.sqrt((im * -2.0));
} else if (im <= 1.65e-60) {
tmp = t_1;
} else if (im <= 0.045) {
tmp = 0.5 * Math.sqrt((((re * re) / im) + t_0));
} else if (im <= 7.5e+43) {
tmp = t_1;
} else {
tmp = 0.5 * Math.sqrt(t_0);
}
return tmp;
}
def code(re, im): t_0 = 2.0 * (re + im) t_1 = 0.5 * (2.0 * math.sqrt(re)) tmp = 0 if im <= -1.08e-111: tmp = 0.5 * math.sqrt((im * -2.0)) elif im <= 1.65e-60: tmp = t_1 elif im <= 0.045: tmp = 0.5 * math.sqrt((((re * re) / im) + t_0)) elif im <= 7.5e+43: tmp = t_1 else: tmp = 0.5 * math.sqrt(t_0) return tmp
function code(re, im) t_0 = Float64(2.0 * Float64(re + im)) t_1 = Float64(0.5 * Float64(2.0 * sqrt(re))) tmp = 0.0 if (im <= -1.08e-111) tmp = Float64(0.5 * sqrt(Float64(im * -2.0))); elseif (im <= 1.65e-60) tmp = t_1; elseif (im <= 0.045) tmp = Float64(0.5 * sqrt(Float64(Float64(Float64(re * re) / im) + t_0))); elseif (im <= 7.5e+43) tmp = t_1; else tmp = Float64(0.5 * sqrt(t_0)); end return tmp end
function tmp_2 = code(re, im) t_0 = 2.0 * (re + im); t_1 = 0.5 * (2.0 * sqrt(re)); tmp = 0.0; if (im <= -1.08e-111) tmp = 0.5 * sqrt((im * -2.0)); elseif (im <= 1.65e-60) tmp = t_1; elseif (im <= 0.045) tmp = 0.5 * sqrt((((re * re) / im) + t_0)); elseif (im <= 7.5e+43) tmp = t_1; else tmp = 0.5 * sqrt(t_0); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[im, -1.08e-111], N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.65e-60], t$95$1, If[LessEqual[im, 0.045], N[(0.5 * N[Sqrt[N[(N[(N[(re * re), $MachinePrecision] / im), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 7.5e+43], t$95$1, N[(0.5 * N[Sqrt[t$95$0], $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 2 \cdot \left(re + im\right)\\
t_1 := 0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{if}\;im \leq -1.08 \cdot 10^{-111}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\
\mathbf{elif}\;im \leq 1.65 \cdot 10^{-60}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;im \leq 0.045:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{re \cdot re}{im} + t_0}\\
\mathbf{elif}\;im \leq 7.5 \cdot 10^{+43}:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{t_0}\\
\end{array}
\end{array}
if im < -1.08e-111Initial program 39.4%
+-commutative39.4%
hypot-def77.8%
Simplified77.8%
Taylor expanded in im around -inf 66.1%
*-commutative66.1%
Simplified66.1%
if -1.08e-111 < im < 1.6499999999999999e-60 or 0.044999999999999998 < im < 7.49999999999999967e43Initial program 45.7%
+-commutative45.7%
hypot-def71.8%
Simplified71.8%
Taylor expanded in im around 0 51.9%
unpow251.9%
rem-square-sqrt53.0%
Simplified53.0%
if 1.6499999999999999e-60 < im < 0.044999999999999998Initial program 54.7%
+-commutative54.7%
hypot-def61.7%
Simplified61.7%
Taylor expanded in re around 0 52.5%
unpow252.5%
distribute-lft-out52.5%
Simplified52.5%
if 7.49999999999999967e43 < im Initial program 24.6%
+-commutative24.6%
hypot-def97.0%
Simplified97.0%
Taylor expanded in re around 0 79.4%
distribute-lft-out79.4%
+-commutative79.4%
*-commutative79.4%
+-commutative79.4%
Simplified79.4%
Final simplification64.0%
(FPCore (re im)
:precision binary64
(if (<= im -7.2e-112)
(* 0.5 (sqrt (* im -2.0)))
(if (or (<= im 8.2e-62) (and (not (<= im 0.065)) (<= im 9.5e+43)))
(* 0.5 (* 2.0 (sqrt re)))
(* 0.5 (sqrt (* 2.0 (+ re im)))))))
double code(double re, double im) {
double tmp;
if (im <= -7.2e-112) {
tmp = 0.5 * sqrt((im * -2.0));
} else if ((im <= 8.2e-62) || (!(im <= 0.065) && (im <= 9.5e+43))) {
tmp = 0.5 * (2.0 * sqrt(re));
} else {
tmp = 0.5 * sqrt((2.0 * (re + im)));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= (-7.2d-112)) then
tmp = 0.5d0 * sqrt((im * (-2.0d0)))
else if ((im <= 8.2d-62) .or. (.not. (im <= 0.065d0)) .and. (im <= 9.5d+43)) then
tmp = 0.5d0 * (2.0d0 * sqrt(re))
else
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -7.2e-112) {
tmp = 0.5 * Math.sqrt((im * -2.0));
} else if ((im <= 8.2e-62) || (!(im <= 0.065) && (im <= 9.5e+43))) {
tmp = 0.5 * (2.0 * Math.sqrt(re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -7.2e-112: tmp = 0.5 * math.sqrt((im * -2.0)) elif (im <= 8.2e-62) or (not (im <= 0.065) and (im <= 9.5e+43)): tmp = 0.5 * (2.0 * math.sqrt(re)) else: tmp = 0.5 * math.sqrt((2.0 * (re + im))) return tmp
function code(re, im) tmp = 0.0 if (im <= -7.2e-112) tmp = Float64(0.5 * sqrt(Float64(im * -2.0))); elseif ((im <= 8.2e-62) || (!(im <= 0.065) && (im <= 9.5e+43))) 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
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -7.2e-112) tmp = 0.5 * sqrt((im * -2.0)); elseif ((im <= 8.2e-62) || (~((im <= 0.065)) && (im <= 9.5e+43))) tmp = 0.5 * (2.0 * sqrt(re)); else tmp = 0.5 * sqrt((2.0 * (re + im))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -7.2e-112], N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[im, 8.2e-62], And[N[Not[LessEqual[im, 0.065]], $MachinePrecision], LessEqual[im, 9.5e+43]]], 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}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -7.2 \cdot 10^{-112}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\
\mathbf{elif}\;im \leq 8.2 \cdot 10^{-62} \lor \neg \left(im \leq 0.065\right) \land im \leq 9.5 \cdot 10^{+43}:\\
\;\;\;\;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 < -7.2000000000000002e-112Initial program 39.4%
+-commutative39.4%
hypot-def77.8%
Simplified77.8%
Taylor expanded in im around -inf 66.1%
*-commutative66.1%
Simplified66.1%
if -7.2000000000000002e-112 < im < 8.2e-62 or 0.065000000000000002 < im < 9.5000000000000004e43Initial program 45.7%
+-commutative45.7%
hypot-def71.8%
Simplified71.8%
Taylor expanded in im around 0 51.9%
unpow251.9%
rem-square-sqrt53.0%
Simplified53.0%
if 8.2e-62 < im < 0.065000000000000002 or 9.5000000000000004e43 < im Initial program 30.1%
+-commutative30.1%
hypot-def90.5%
Simplified90.5%
Taylor expanded in re around 0 73.9%
distribute-lft-out73.9%
+-commutative73.9%
*-commutative73.9%
+-commutative73.9%
Simplified73.9%
Final simplification63.9%
(FPCore (re im)
:precision binary64
(if (<= im -5.4e-112)
(* 0.5 (sqrt (* im -2.0)))
(if (or (<= im 3.3e-61) (and (not (<= im 0.034)) (<= im 7e+43)))
(* 0.5 (* 2.0 (sqrt re)))
(* 0.5 (sqrt (* 2.0 im))))))
double code(double re, double im) {
double tmp;
if (im <= -5.4e-112) {
tmp = 0.5 * sqrt((im * -2.0));
} else if ((im <= 3.3e-61) || (!(im <= 0.034) && (im <= 7e+43))) {
tmp = 0.5 * (2.0 * sqrt(re));
} else {
tmp = 0.5 * sqrt((2.0 * im));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= (-5.4d-112)) then
tmp = 0.5d0 * sqrt((im * (-2.0d0)))
else if ((im <= 3.3d-61) .or. (.not. (im <= 0.034d0)) .and. (im <= 7d+43)) then
tmp = 0.5d0 * (2.0d0 * sqrt(re))
else
tmp = 0.5d0 * sqrt((2.0d0 * im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -5.4e-112) {
tmp = 0.5 * Math.sqrt((im * -2.0));
} else if ((im <= 3.3e-61) || (!(im <= 0.034) && (im <= 7e+43))) {
tmp = 0.5 * (2.0 * Math.sqrt(re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -5.4e-112: tmp = 0.5 * math.sqrt((im * -2.0)) elif (im <= 3.3e-61) or (not (im <= 0.034) and (im <= 7e+43)): tmp = 0.5 * (2.0 * math.sqrt(re)) else: tmp = 0.5 * math.sqrt((2.0 * im)) return tmp
function code(re, im) tmp = 0.0 if (im <= -5.4e-112) tmp = Float64(0.5 * sqrt(Float64(im * -2.0))); elseif ((im <= 3.3e-61) || (!(im <= 0.034) && (im <= 7e+43))) tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -5.4e-112) tmp = 0.5 * sqrt((im * -2.0)); elseif ((im <= 3.3e-61) || (~((im <= 0.034)) && (im <= 7e+43))) tmp = 0.5 * (2.0 * sqrt(re)); else tmp = 0.5 * sqrt((2.0 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -5.4e-112], N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[im, 3.3e-61], And[N[Not[LessEqual[im, 0.034]], $MachinePrecision], LessEqual[im, 7e+43]]], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -5.4 \cdot 10^{-112}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\
\mathbf{elif}\;im \leq 3.3 \cdot 10^{-61} \lor \neg \left(im \leq 0.034\right) \land im \leq 7 \cdot 10^{+43}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\end{array}
\end{array}
if im < -5.4000000000000001e-112Initial program 39.4%
+-commutative39.4%
hypot-def77.8%
Simplified77.8%
Taylor expanded in im around -inf 66.1%
*-commutative66.1%
Simplified66.1%
if -5.4000000000000001e-112 < im < 3.29999999999999996e-61 or 0.034000000000000002 < im < 7.0000000000000002e43Initial program 45.7%
+-commutative45.7%
hypot-def71.8%
Simplified71.8%
Taylor expanded in im around 0 51.9%
unpow251.9%
rem-square-sqrt53.0%
Simplified53.0%
if 3.29999999999999996e-61 < im < 0.034000000000000002 or 7.0000000000000002e43 < im Initial program 30.1%
+-commutative30.1%
hypot-def90.5%
Simplified90.5%
Taylor expanded in re around 0 71.6%
*-commutative71.6%
Simplified71.6%
Final simplification63.2%
(FPCore (re im) :precision binary64 (if (<= im -2e-310) (* 0.5 (sqrt (* im -2.0))) (* 0.5 (sqrt (* 2.0 im)))))
double code(double re, double im) {
double tmp;
if (im <= -2e-310) {
tmp = 0.5 * sqrt((im * -2.0));
} else {
tmp = 0.5 * sqrt((2.0 * im));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= (-2d-310)) then
tmp = 0.5d0 * sqrt((im * (-2.0d0)))
else
tmp = 0.5d0 * sqrt((2.0d0 * im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -2e-310) {
tmp = 0.5 * Math.sqrt((im * -2.0));
} else {
tmp = 0.5 * Math.sqrt((2.0 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -2e-310: tmp = 0.5 * math.sqrt((im * -2.0)) else: tmp = 0.5 * math.sqrt((2.0 * im)) return tmp
function code(re, im) tmp = 0.0 if (im <= -2e-310) tmp = Float64(0.5 * sqrt(Float64(im * -2.0))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -2e-310) tmp = 0.5 * sqrt((im * -2.0)); else tmp = 0.5 * sqrt((2.0 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -2e-310], N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -2 \cdot 10^{-310}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\end{array}
\end{array}
if im < -1.999999999999994e-310Initial program 40.6%
+-commutative40.6%
hypot-def76.0%
Simplified76.0%
Taylor expanded in im around -inf 52.8%
*-commutative52.8%
Simplified52.8%
if -1.999999999999994e-310 < im Initial program 37.3%
+-commutative37.3%
hypot-def82.6%
Simplified82.6%
Taylor expanded in re around 0 47.3%
*-commutative47.3%
Simplified47.3%
Final simplification49.7%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* im -2.0))))
double code(double re, double im) {
return 0.5 * sqrt((im * -2.0));
}
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
public static double code(double re, double im) {
return 0.5 * Math.sqrt((im * -2.0));
}
def code(re, im): return 0.5 * math.sqrt((im * -2.0))
function code(re, im) return Float64(0.5 * sqrt(Float64(im * -2.0))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((im * -2.0)); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{im \cdot -2}
\end{array}
Initial program 38.7%
+-commutative38.7%
hypot-def79.7%
Simplified79.7%
Taylor expanded in im around -inf 23.5%
*-commutative23.5%
Simplified23.5%
Final simplification23.5%
(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 2023181
(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)))))