
(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 (* im (/ (- im) re)))) (* 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((im * (-im / re)));
} 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((im * (-im / re)));
} 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((im * (-im / re))) 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(im * Float64(Float64(-im) / re)))); 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((im * (-im / re))); 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[(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}
\\
\begin{array}{l}
\mathbf{if}\;\sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)} \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{im \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 (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 8.9%
+-commutative8.9%
hypot-def8.9%
Simplified8.9%
Taylor expanded in re around -inf 55.1%
*-commutative55.1%
unpow255.1%
associate-/l*63.5%
Simplified63.5%
expm1-log1p-u63.2%
expm1-udef14.9%
*-commutative14.9%
associate-*l*14.9%
associate-/r/14.9%
*-commutative14.9%
metadata-eval14.9%
Applied egg-rr14.9%
expm1-def63.6%
expm1-log1p63.9%
*-commutative63.9%
neg-mul-163.9%
distribute-rgt-neg-in63.9%
distribute-frac-neg63.9%
Simplified63.9%
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-def91.8%
Simplified91.8%
Final simplification88.5%
(FPCore (re im)
:precision binary64
(let* ((t_0 (* 0.5 (sqrt (* im (/ (- im) re))))))
(if (<= re -1e+100)
t_0
(if (<= re -2.5e-7)
(* 0.5 (sqrt (* 2.0 im)))
(if (<= re -5.3e-40)
t_0
(if (<= re 4e-85)
(* 0.5 (sqrt (+ (/ (* re re) im) (* 2.0 (+ re im)))))
(* 0.5 (* 2.0 (sqrt re)))))))))
double code(double re, double im) {
double t_0 = 0.5 * sqrt((im * (-im / re)));
double tmp;
if (re <= -1e+100) {
tmp = t_0;
} else if (re <= -2.5e-7) {
tmp = 0.5 * sqrt((2.0 * im));
} else if (re <= -5.3e-40) {
tmp = t_0;
} else if (re <= 4e-85) {
tmp = 0.5 * sqrt((((re * re) / im) + (2.0 * (re + im))));
} else {
tmp = 0.5 * (2.0 * sqrt(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 = 0.5d0 * sqrt((im * (-im / re)))
if (re <= (-1d+100)) then
tmp = t_0
else if (re <= (-2.5d-7)) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else if (re <= (-5.3d-40)) then
tmp = t_0
else if (re <= 4d-85) then
tmp = 0.5d0 * sqrt((((re * re) / im) + (2.0d0 * (re + im))))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = 0.5 * Math.sqrt((im * (-im / re)));
double tmp;
if (re <= -1e+100) {
tmp = t_0;
} else if (re <= -2.5e-7) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else if (re <= -5.3e-40) {
tmp = t_0;
} else if (re <= 4e-85) {
tmp = 0.5 * Math.sqrt((((re * re) / im) + (2.0 * (re + im))));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): t_0 = 0.5 * math.sqrt((im * (-im / re))) tmp = 0 if re <= -1e+100: tmp = t_0 elif re <= -2.5e-7: tmp = 0.5 * math.sqrt((2.0 * im)) elif re <= -5.3e-40: tmp = t_0 elif re <= 4e-85: tmp = 0.5 * math.sqrt((((re * re) / im) + (2.0 * (re + im)))) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) t_0 = Float64(0.5 * sqrt(Float64(im * Float64(Float64(-im) / re)))) tmp = 0.0 if (re <= -1e+100) tmp = t_0; elseif (re <= -2.5e-7) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); elseif (re <= -5.3e-40) tmp = t_0; elseif (re <= 4e-85) tmp = Float64(0.5 * sqrt(Float64(Float64(Float64(re * re) / im) + Float64(2.0 * Float64(re + im))))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
function tmp_2 = code(re, im) t_0 = 0.5 * sqrt((im * (-im / re))); tmp = 0.0; if (re <= -1e+100) tmp = t_0; elseif (re <= -2.5e-7) tmp = 0.5 * sqrt((2.0 * im)); elseif (re <= -5.3e-40) tmp = t_0; elseif (re <= 4e-85) tmp = 0.5 * sqrt((((re * re) / im) + (2.0 * (re + im)))); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(im * N[((-im) / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -1e+100], t$95$0, If[LessEqual[re, -2.5e-7], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, -5.3e-40], t$95$0, If[LessEqual[re, 4e-85], N[(0.5 * N[Sqrt[N[(N[(N[(re * re), $MachinePrecision] / im), $MachinePrecision] + N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(2.0 * N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\
\mathbf{if}\;re \leq -1 \cdot 10^{+100}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;re \leq -2.5 \cdot 10^{-7}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{elif}\;re \leq -5.3 \cdot 10^{-40}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;re \leq 4 \cdot 10^{-85}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{re \cdot re}{im} + 2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < -1.00000000000000002e100 or -2.49999999999999989e-7 < re < -5.3000000000000002e-40Initial program 7.8%
+-commutative7.8%
hypot-def39.7%
Simplified39.7%
Taylor expanded in re around -inf 51.5%
*-commutative51.5%
unpow251.5%
associate-/l*62.3%
Simplified62.3%
expm1-log1p-u61.4%
expm1-udef38.9%
*-commutative38.9%
associate-*l*38.9%
associate-/r/38.9%
*-commutative38.9%
metadata-eval38.9%
Applied egg-rr38.9%
expm1-def61.5%
expm1-log1p62.4%
*-commutative62.4%
neg-mul-162.4%
distribute-rgt-neg-in62.4%
distribute-frac-neg62.4%
Simplified62.4%
if -1.00000000000000002e100 < re < -2.49999999999999989e-7Initial program 28.7%
+-commutative28.7%
hypot-def68.5%
Simplified68.5%
Taylor expanded in re around 0 26.4%
*-commutative26.4%
Simplified26.4%
if -5.3000000000000002e-40 < re < 3.9999999999999999e-85Initial program 50.5%
+-commutative50.5%
hypot-def89.9%
Simplified89.9%
Taylor expanded in re around 0 36.5%
unpow236.5%
distribute-lft-out36.5%
Simplified36.5%
if 3.9999999999999999e-85 < re Initial program 50.3%
+-commutative50.3%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 67.4%
unpow267.4%
rem-square-sqrt68.5%
Simplified68.5%
Final simplification51.2%
(FPCore (re im)
:precision binary64
(let* ((t_0 (* 0.5 (sqrt (* im (/ (- im) re))))))
(if (<= re -2.25e+101)
t_0
(if (<= re -1.4e-7)
(* 0.5 (sqrt (* 2.0 im)))
(if (<= re -2.35e-40)
t_0
(if (<= re 1.8e-85)
(* 0.5 (sqrt (* 2.0 (+ re im))))
(* 0.5 (* 2.0 (sqrt re)))))))))
double code(double re, double im) {
double t_0 = 0.5 * sqrt((im * (-im / re)));
double tmp;
if (re <= -2.25e+101) {
tmp = t_0;
} else if (re <= -1.4e-7) {
tmp = 0.5 * sqrt((2.0 * im));
} else if (re <= -2.35e-40) {
tmp = t_0;
} else if (re <= 1.8e-85) {
tmp = 0.5 * sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * sqrt(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 = 0.5d0 * sqrt((im * (-im / re)))
if (re <= (-2.25d+101)) then
tmp = t_0
else if (re <= (-1.4d-7)) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else if (re <= (-2.35d-40)) then
tmp = t_0
else if (re <= 1.8d-85) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = 0.5 * Math.sqrt((im * (-im / re)));
double tmp;
if (re <= -2.25e+101) {
tmp = t_0;
} else if (re <= -1.4e-7) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else if (re <= -2.35e-40) {
tmp = t_0;
} else if (re <= 1.8e-85) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): t_0 = 0.5 * math.sqrt((im * (-im / re))) tmp = 0 if re <= -2.25e+101: tmp = t_0 elif re <= -1.4e-7: tmp = 0.5 * math.sqrt((2.0 * im)) elif re <= -2.35e-40: tmp = t_0 elif re <= 1.8e-85: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) t_0 = Float64(0.5 * sqrt(Float64(im * Float64(Float64(-im) / re)))) tmp = 0.0 if (re <= -2.25e+101) tmp = t_0; elseif (re <= -1.4e-7) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); elseif (re <= -2.35e-40) tmp = t_0; elseif (re <= 1.8e-85) 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
function tmp_2 = code(re, im) t_0 = 0.5 * sqrt((im * (-im / re))); tmp = 0.0; if (re <= -2.25e+101) tmp = t_0; elseif (re <= -1.4e-7) tmp = 0.5 * sqrt((2.0 * im)); elseif (re <= -2.35e-40) tmp = t_0; elseif (re <= 1.8e-85) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Sqrt[N[(im * N[((-im) / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -2.25e+101], t$95$0, If[LessEqual[re, -1.4e-7], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, -2.35e-40], t$95$0, If[LessEqual[re, 1.8e-85], 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}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\
\mathbf{if}\;re \leq -2.25 \cdot 10^{+101}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;re \leq -1.4 \cdot 10^{-7}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{elif}\;re \leq -2.35 \cdot 10^{-40}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;re \leq 1.8 \cdot 10^{-85}:\\
\;\;\;\;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.2500000000000001e101 or -1.4000000000000001e-7 < re < -2.35e-40Initial program 7.8%
+-commutative7.8%
hypot-def39.7%
Simplified39.7%
Taylor expanded in re around -inf 51.5%
*-commutative51.5%
unpow251.5%
associate-/l*62.3%
Simplified62.3%
expm1-log1p-u61.4%
expm1-udef38.9%
*-commutative38.9%
associate-*l*38.9%
associate-/r/38.9%
*-commutative38.9%
metadata-eval38.9%
Applied egg-rr38.9%
expm1-def61.5%
expm1-log1p62.4%
*-commutative62.4%
neg-mul-162.4%
distribute-rgt-neg-in62.4%
distribute-frac-neg62.4%
Simplified62.4%
if -2.2500000000000001e101 < re < -1.4000000000000001e-7Initial program 28.7%
+-commutative28.7%
hypot-def68.5%
Simplified68.5%
Taylor expanded in re around 0 26.4%
*-commutative26.4%
Simplified26.4%
if -2.35e-40 < re < 1.7999999999999999e-85Initial program 50.5%
+-commutative50.5%
hypot-def89.9%
Simplified89.9%
Taylor expanded in re around 0 36.7%
distribute-lft-out36.7%
+-commutative36.7%
*-commutative36.7%
+-commutative36.7%
Simplified36.7%
if 1.7999999999999999e-85 < re Initial program 50.3%
+-commutative50.3%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 67.4%
unpow267.4%
rem-square-sqrt68.5%
Simplified68.5%
Final simplification51.2%
(FPCore (re im) :precision binary64 (if (<= re -2.5e+219) (* 0.5 (sqrt (* im (/ im re)))) (if (<= re 4e-85) (* 0.5 (sqrt (* 2.0 im))) (* 0.5 (* 2.0 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -2.5e+219) {
tmp = 0.5 * sqrt((im * (im / re)));
} else if (re <= 4e-85) {
tmp = 0.5 * sqrt((2.0 * im));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-2.5d+219)) then
tmp = 0.5d0 * sqrt((im * (im / re)))
else if (re <= 4d-85) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -2.5e+219) {
tmp = 0.5 * Math.sqrt((im * (im / re)));
} else if (re <= 4e-85) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -2.5e+219: tmp = 0.5 * math.sqrt((im * (im / re))) elif re <= 4e-85: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -2.5e+219) tmp = Float64(0.5 * sqrt(Float64(im * Float64(im / re)))); elseif (re <= 4e-85) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.5e+219) tmp = 0.5 * sqrt((im * (im / re))); elseif (re <= 4e-85) tmp = 0.5 * sqrt((2.0 * im)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -2.5e+219], N[(0.5 * N[Sqrt[N[(im * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 4e-85], 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}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -2.5 \cdot 10^{+219}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{im}{re}}\\
\mathbf{elif}\;re \leq 4 \cdot 10^{-85}:\\
\;\;\;\;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.5e219Initial program 2.4%
+-commutative2.4%
hypot-def53.2%
Simplified53.2%
Taylor expanded in re around -inf 59.1%
*-commutative59.1%
unpow259.1%
associate-/l*82.2%
Simplified82.2%
expm1-log1p-u80.5%
expm1-udef74.9%
*-commutative74.9%
associate-*l*74.9%
associate-/r/74.9%
*-commutative74.9%
metadata-eval74.9%
Applied egg-rr74.9%
expm1-def80.5%
expm1-log1p82.1%
rem-square-sqrt82.1%
fabs-sqr82.1%
rem-square-sqrt82.1%
fabs-mul82.1%
rem-square-sqrt46.2%
fabs-sqr46.2%
metadata-eval46.2%
rem-square-sqrt46.2%
*-rgt-identity46.2%
Simplified46.2%
if -2.5e219 < re < 3.9999999999999999e-85Initial program 40.0%
+-commutative40.0%
hypot-def75.9%
Simplified75.9%
Taylor expanded in re around 0 30.6%
*-commutative30.6%
Simplified30.6%
if 3.9999999999999999e-85 < re Initial program 50.3%
+-commutative50.3%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 67.4%
unpow267.4%
rem-square-sqrt68.5%
Simplified68.5%
Final simplification43.3%
(FPCore (re im) :precision binary64 (if (<= re 3.3e-85) (* 0.5 (sqrt (* 2.0 im))) (* 0.5 (* 2.0 (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= 3.3e-85) {
tmp = 0.5 * sqrt((2.0 * im));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= 3.3d-85) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else
tmp = 0.5d0 * (2.0d0 * sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 3.3e-85) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = 0.5 * (2.0 * Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 3.3e-85: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = 0.5 * (2.0 * math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= 3.3e-85) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = Float64(0.5 * Float64(2.0 * sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 3.3e-85) tmp = 0.5 * sqrt((2.0 * im)); else tmp = 0.5 * (2.0 * sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 3.3e-85], 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}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 3.3 \cdot 10^{-85}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\end{array}
\end{array}
if re < 3.29999999999999973e-85Initial program 36.4%
+-commutative36.4%
hypot-def73.7%
Simplified73.7%
Taylor expanded in re around 0 28.4%
*-commutative28.4%
Simplified28.4%
if 3.29999999999999973e-85 < re Initial program 50.3%
+-commutative50.3%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 67.4%
unpow267.4%
rem-square-sqrt68.5%
Simplified68.5%
Final simplification40.8%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 im))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * im));
}
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
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * im));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * im))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * im))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * im)); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Initial program 40.7%
+-commutative40.7%
hypot-def81.8%
Simplified81.8%
Taylor expanded in re around 0 26.5%
*-commutative26.5%
Simplified26.5%
Final simplification26.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 2023200
(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)))))