
(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 5 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 (/ (- (pow im 2.0)) re))) (sqrt (* 0.5 (+ 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((-pow(im, 2.0) / re));
} else {
tmp = sqrt((0.5 * (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((-Math.pow(im, 2.0) / re));
} else {
tmp = Math.sqrt((0.5 * (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((-math.pow(im, 2.0) / re)) else: tmp = math.sqrt((0.5 * (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(Float64(-(im ^ 2.0)) / re))); else tmp = sqrt(Float64(0.5 * 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 ^ 2.0) / re)); else tmp = sqrt((0.5 * (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[((-N[Power[im, 2.0], $MachinePrecision]) / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $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{\frac{-{im}^{2}}{re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \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 10.7%
+-commutative10.7%
hypot-def10.7%
Simplified10.7%
Taylor expanded in re around -inf 61.6%
mul-1-neg61.6%
distribute-neg-frac61.6%
Simplified61.6%
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 46.5%
+-commutative46.5%
hypot-def89.4%
Simplified89.4%
sqrt-prod89.8%
+-commutative89.8%
hypot-def46.3%
sqrt-prod46.5%
add-sqr-sqrt46.2%
sqrt-unprod46.5%
*-commutative46.5%
*-commutative46.5%
swap-sqr46.5%
Applied egg-rr89.4%
*-commutative89.4%
associate-*r*90.3%
metadata-eval90.3%
Simplified90.3%
Final simplification86.0%
(FPCore (re im) :precision binary64 (sqrt (* 0.5 (+ re (hypot re im)))))
double code(double re, double im) {
return sqrt((0.5 * (re + hypot(re, im))));
}
public static double code(double re, double im) {
return Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
def code(re, im): return math.sqrt((0.5 * (re + math.hypot(re, im))))
function code(re, im) return sqrt(Float64(0.5 * Float64(re + hypot(re, im)))) end
function tmp = code(re, im) tmp = sqrt((0.5 * (re + hypot(re, im)))); end
code[re_, im_] := N[Sqrt[N[(0.5 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}
\end{array}
Initial program 41.2%
+-commutative41.2%
hypot-def77.7%
Simplified77.7%
sqrt-prod78.1%
+-commutative78.1%
hypot-def41.0%
sqrt-prod41.2%
add-sqr-sqrt40.9%
sqrt-unprod41.2%
*-commutative41.2%
*-commutative41.2%
swap-sqr41.2%
Applied egg-rr77.7%
*-commutative77.7%
associate-*r*78.5%
metadata-eval78.5%
Simplified78.5%
Final simplification78.5%
(FPCore (re im) :precision binary64 (if (<= re -1.36e+218) (* 0.5 (sqrt (* 2.0 (- re re)))) (if (<= re 3.5e+39) (sqrt (* im 0.5)) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -1.36e+218) {
tmp = 0.5 * sqrt((2.0 * (re - re)));
} else if (re <= 3.5e+39) {
tmp = sqrt((im * 0.5));
} else {
tmp = 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 <= (-1.36d+218)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re - re)))
else if (re <= 3.5d+39) then
tmp = sqrt((im * 0.5d0))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.36e+218) {
tmp = 0.5 * Math.sqrt((2.0 * (re - re)));
} else if (re <= 3.5e+39) {
tmp = Math.sqrt((im * 0.5));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.36e+218: tmp = 0.5 * math.sqrt((2.0 * (re - re))) elif re <= 3.5e+39: tmp = math.sqrt((im * 0.5)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.36e+218) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - re)))); elseif (re <= 3.5e+39) tmp = sqrt(Float64(im * 0.5)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.36e+218) tmp = 0.5 * sqrt((2.0 * (re - re))); elseif (re <= 3.5e+39) tmp = sqrt((im * 0.5)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.36e+218], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.5e+39], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.36 \cdot 10^{+218}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - re\right)}\\
\mathbf{elif}\;re \leq 3.5 \cdot 10^{+39}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -1.36e218Initial program 2.4%
Taylor expanded in re around -inf 37.8%
mul-1-neg37.8%
Simplified37.8%
if -1.36e218 < re < 3.5000000000000002e39Initial program 48.4%
+-commutative48.4%
hypot-def75.7%
Simplified75.7%
Taylor expanded in re around 0 28.3%
associate-*r*28.3%
Simplified28.3%
add-sqr-sqrt28.2%
sqrt-unprod28.3%
associate-*l*28.3%
associate-*l*28.3%
swap-sqr28.3%
metadata-eval28.3%
swap-sqr28.2%
add-sqr-sqrt28.1%
rem-square-sqrt28.4%
Applied egg-rr28.4%
*-commutative28.4%
associate-*l*28.4%
metadata-eval28.4%
Simplified28.4%
if 3.5000000000000002e39 < re Initial program 34.5%
+-commutative34.5%
hypot-def96.7%
Simplified96.7%
Taylor expanded in im around 0 75.2%
*-commutative75.2%
unpow275.2%
rem-square-sqrt76.6%
associate-*r*76.6%
metadata-eval76.6%
Simplified76.6%
Final simplification40.0%
(FPCore (re im) :precision binary64 (if (<= re 1.4e+41) (sqrt (* im 0.5)) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= 1.4e+41) {
tmp = sqrt((im * 0.5));
} else {
tmp = 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 <= 1.4d+41) then
tmp = sqrt((im * 0.5d0))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 1.4e+41) {
tmp = Math.sqrt((im * 0.5));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 1.4e+41: tmp = math.sqrt((im * 0.5)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 1.4e+41) tmp = sqrt(Float64(im * 0.5)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 1.4e+41) tmp = sqrt((im * 0.5)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 1.4e+41], N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 1.4 \cdot 10^{+41}:\\
\;\;\;\;\sqrt{im \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 1.4e41Initial program 43.1%
+-commutative43.1%
hypot-def72.3%
Simplified72.3%
Taylor expanded in re around 0 25.3%
associate-*r*25.3%
Simplified25.3%
add-sqr-sqrt25.3%
sqrt-unprod25.3%
associate-*l*25.3%
associate-*l*25.3%
swap-sqr25.3%
metadata-eval25.3%
swap-sqr25.3%
add-sqr-sqrt25.2%
rem-square-sqrt25.5%
Applied egg-rr25.5%
*-commutative25.5%
associate-*l*25.5%
metadata-eval25.5%
Simplified25.5%
if 1.4e41 < re Initial program 34.5%
+-commutative34.5%
hypot-def96.7%
Simplified96.7%
Taylor expanded in im around 0 75.2%
*-commutative75.2%
unpow275.2%
rem-square-sqrt76.6%
associate-*r*76.6%
metadata-eval76.6%
Simplified76.6%
Final simplification36.8%
(FPCore (re im) :precision binary64 (sqrt (* im 0.5)))
double code(double re, double im) {
return sqrt((im * 0.5));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt((im * 0.5d0))
end function
public static double code(double re, double im) {
return Math.sqrt((im * 0.5));
}
def code(re, im): return math.sqrt((im * 0.5))
function code(re, im) return sqrt(Float64(im * 0.5)) end
function tmp = code(re, im) tmp = sqrt((im * 0.5)); end
code[re_, im_] := N[Sqrt[N[(im * 0.5), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{im \cdot 0.5}
\end{array}
Initial program 41.2%
+-commutative41.2%
hypot-def77.7%
Simplified77.7%
Taylor expanded in re around 0 22.1%
associate-*r*22.1%
Simplified22.1%
add-sqr-sqrt22.1%
sqrt-unprod22.1%
associate-*l*22.1%
associate-*l*22.1%
swap-sqr22.1%
metadata-eval22.1%
swap-sqr22.1%
add-sqr-sqrt22.0%
rem-square-sqrt22.2%
Applied egg-rr22.2%
*-commutative22.2%
associate-*l*22.2%
metadata-eval22.2%
Simplified22.2%
Final simplification22.2%
(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 2023305
(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)))))