
(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 (<= re -5e+164)
(*
0.5
(sqrt
(-
(* (* (* im (/ im re)) (* (/ im re) (/ im re))) 0.25)
(/ im (/ re im)))))
(sqrt (* 0.5 (+ re (hypot re im))))))
double code(double re, double im) {
double tmp;
if (re <= -5e+164) {
tmp = 0.5 * sqrt(((((im * (im / re)) * ((im / re) * (im / re))) * 0.25) - (im / (re / im))));
} else {
tmp = sqrt((0.5 * (re + hypot(re, im))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= -5e+164) {
tmp = 0.5 * Math.sqrt(((((im * (im / re)) * ((im / re) * (im / re))) * 0.25) - (im / (re / im))));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -5e+164: tmp = 0.5 * math.sqrt(((((im * (im / re)) * ((im / re) * (im / re))) * 0.25) - (im / (re / im)))) else: tmp = math.sqrt((0.5 * (re + math.hypot(re, im)))) return tmp
function code(re, im) tmp = 0.0 if (re <= -5e+164) tmp = Float64(0.5 * sqrt(Float64(Float64(Float64(Float64(im * Float64(im / re)) * Float64(Float64(im / re) * Float64(im / re))) * 0.25) - Float64(im / Float64(re / im))))); 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 (re <= -5e+164) tmp = 0.5 * sqrt(((((im * (im / re)) * ((im / re) * (im / re))) * 0.25) - (im / (re / im)))); else tmp = sqrt((0.5 * (re + hypot(re, im)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -5e+164], N[(0.5 * N[Sqrt[N[(N[(N[(N[(im * N[(im / re), $MachinePrecision]), $MachinePrecision] * N[(N[(im / re), $MachinePrecision] * N[(im / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * 0.25), $MachinePrecision] - N[(im / N[(re / im), $MachinePrecision]), $MachinePrecision]), $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}\;re \leq -5 \cdot 10^{+164}:\\
\;\;\;\;0.5 \cdot \sqrt{\left(\left(im \cdot \frac{im}{re}\right) \cdot \left(\frac{im}{re} \cdot \frac{im}{re}\right)\right) \cdot 0.25 - \frac{im}{\frac{re}{im}}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -4.9999999999999995e164Initial program 2.7%
+-commutative2.7%
hypot-def27.2%
Simplified27.2%
Taylor expanded in re around -inf 43.5%
+-commutative43.5%
mul-1-neg43.5%
unsub-neg43.5%
*-commutative43.5%
unpow243.5%
associate-/l*43.5%
Simplified43.5%
metadata-eval43.5%
pow-prod-up43.5%
pow-prod-down43.5%
pow243.5%
cube-mult43.5%
times-frac54.2%
associate-*l/54.2%
*-commutative54.2%
Applied egg-rr54.2%
times-frac88.5%
Simplified88.5%
if -4.9999999999999995e164 < re Initial program 48.8%
+-commutative48.8%
hypot-def86.5%
Simplified86.5%
add-sqr-sqrt85.8%
sqrt-unprod86.5%
*-commutative86.5%
*-commutative86.5%
swap-sqr86.5%
add-sqr-sqrt86.5%
*-commutative86.5%
metadata-eval86.5%
Applied egg-rr86.5%
associate-*l*86.5%
metadata-eval86.5%
Simplified86.5%
Final simplification86.7%
(FPCore (re im) :precision binary64 (if (<= im -3.9e-94) (* 0.5 (sqrt (* im -2.0))) (if (<= im 8.5e-137) (sqrt re) (* 0.5 (sqrt (* im 2.0))))))
double code(double re, double im) {
double tmp;
if (im <= -3.9e-94) {
tmp = 0.5 * sqrt((im * -2.0));
} else if (im <= 8.5e-137) {
tmp = sqrt(re);
} else {
tmp = 0.5 * sqrt((im * 2.0));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= (-3.9d-94)) then
tmp = 0.5d0 * sqrt((im * (-2.0d0)))
else if (im <= 8.5d-137) then
tmp = sqrt(re)
else
tmp = 0.5d0 * sqrt((im * 2.0d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -3.9e-94) {
tmp = 0.5 * Math.sqrt((im * -2.0));
} else if (im <= 8.5e-137) {
tmp = Math.sqrt(re);
} else {
tmp = 0.5 * Math.sqrt((im * 2.0));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -3.9e-94: tmp = 0.5 * math.sqrt((im * -2.0)) elif im <= 8.5e-137: tmp = math.sqrt(re) else: tmp = 0.5 * math.sqrt((im * 2.0)) return tmp
function code(re, im) tmp = 0.0 if (im <= -3.9e-94) tmp = Float64(0.5 * sqrt(Float64(im * -2.0))); elseif (im <= 8.5e-137) tmp = sqrt(re); else tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -3.9e-94) tmp = 0.5 * sqrt((im * -2.0)); elseif (im <= 8.5e-137) tmp = sqrt(re); else tmp = 0.5 * sqrt((im * 2.0)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -3.9e-94], N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 8.5e-137], N[Sqrt[re], $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -3.9 \cdot 10^{-94}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\
\mathbf{elif}\;im \leq 8.5 \cdot 10^{-137}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\end{array}
\end{array}
if im < -3.9000000000000002e-94Initial program 52.0%
+-commutative52.0%
hypot-def89.8%
Simplified89.8%
Taylor expanded in im around -inf 73.6%
*-commutative73.6%
Simplified73.6%
if -3.9000000000000002e-94 < im < 8.5000000000000001e-137Initial program 37.7%
+-commutative37.7%
hypot-def75.4%
Simplified75.4%
Taylor expanded in im around 0 48.5%
associate-*r*48.5%
unpow248.5%
rem-square-sqrt49.5%
metadata-eval49.5%
*-lft-identity49.5%
Simplified49.5%
if 8.5000000000000001e-137 < im Initial program 42.7%
+-commutative42.7%
hypot-def76.3%
Simplified76.3%
Taylor expanded in re around 0 60.7%
*-commutative60.7%
Simplified60.7%
Final simplification60.8%
(FPCore (re im) :precision binary64 (if (<= im -6.5e-224) (* 0.5 (sqrt (* 2.0 (- re im)))) (if (<= im 1.05e-137) (sqrt re) (* 0.5 (sqrt (* im 2.0))))))
double code(double re, double im) {
double tmp;
if (im <= -6.5e-224) {
tmp = 0.5 * sqrt((2.0 * (re - im)));
} else if (im <= 1.05e-137) {
tmp = sqrt(re);
} else {
tmp = 0.5 * sqrt((im * 2.0));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= (-6.5d-224)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re - im)))
else if (im <= 1.05d-137) then
tmp = sqrt(re)
else
tmp = 0.5d0 * sqrt((im * 2.0d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= -6.5e-224) {
tmp = 0.5 * Math.sqrt((2.0 * (re - im)));
} else if (im <= 1.05e-137) {
tmp = Math.sqrt(re);
} else {
tmp = 0.5 * Math.sqrt((im * 2.0));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= -6.5e-224: tmp = 0.5 * math.sqrt((2.0 * (re - im))) elif im <= 1.05e-137: tmp = math.sqrt(re) else: tmp = 0.5 * math.sqrt((im * 2.0)) return tmp
function code(re, im) tmp = 0.0 if (im <= -6.5e-224) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - im)))); elseif (im <= 1.05e-137) tmp = sqrt(re); else tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= -6.5e-224) tmp = 0.5 * sqrt((2.0 * (re - im))); elseif (im <= 1.05e-137) tmp = sqrt(re); else tmp = 0.5 * sqrt((im * 2.0)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, -6.5e-224], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.05e-137], N[Sqrt[re], $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq -6.5 \cdot 10^{-224}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\
\mathbf{elif}\;im \leq 1.05 \cdot 10^{-137}:\\
\;\;\;\;\sqrt{re}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\end{array}
\end{array}
if im < -6.5e-224Initial program 46.9%
+-commutative46.9%
hypot-def86.2%
Simplified86.2%
Taylor expanded in im around -inf 66.8%
mul-1-neg66.8%
sub-neg66.8%
Simplified66.8%
if -6.5e-224 < im < 1.04999999999999996e-137Initial program 40.5%
+-commutative40.5%
hypot-def75.6%
Simplified75.6%
Taylor expanded in im around 0 52.6%
associate-*r*52.6%
unpow252.6%
rem-square-sqrt53.7%
metadata-eval53.7%
*-lft-identity53.7%
Simplified53.7%
if 1.04999999999999996e-137 < im Initial program 42.7%
+-commutative42.7%
hypot-def76.3%
Simplified76.3%
Taylor expanded in re around 0 60.7%
*-commutative60.7%
Simplified60.7%
Final simplification61.4%
(FPCore (re im) :precision binary64 (if (<= re 4.5e-116) (* 0.5 (sqrt (* im -2.0))) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= 4.5e-116) {
tmp = 0.5 * sqrt((im * -2.0));
} 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 <= 4.5d-116) then
tmp = 0.5d0 * sqrt((im * (-2.0d0)))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 4.5e-116) {
tmp = 0.5 * Math.sqrt((im * -2.0));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 4.5e-116: tmp = 0.5 * math.sqrt((im * -2.0)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 4.5e-116) tmp = Float64(0.5 * sqrt(Float64(im * -2.0))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 4.5e-116) tmp = 0.5 * sqrt((im * -2.0)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 4.5e-116], N[(0.5 * N[Sqrt[N[(im * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 4.5 \cdot 10^{-116}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot -2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 4.50000000000000012e-116Initial program 34.1%
+-commutative34.1%
hypot-def69.2%
Simplified69.2%
Taylor expanded in im around -inf 30.6%
*-commutative30.6%
Simplified30.6%
if 4.50000000000000012e-116 < re Initial program 61.7%
+-commutative61.7%
hypot-def100.0%
Simplified100.0%
Taylor expanded in im around 0 72.1%
associate-*r*72.1%
unpow272.1%
rem-square-sqrt73.5%
metadata-eval73.5%
*-lft-identity73.5%
Simplified73.5%
Final simplification45.6%
(FPCore (re im) :precision binary64 (sqrt re))
double code(double re, double im) {
return sqrt(re);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt(re)
end function
public static double code(double re, double im) {
return Math.sqrt(re);
}
def code(re, im): return math.sqrt(re)
function code(re, im) return sqrt(re) end
function tmp = code(re, im) tmp = sqrt(re); end
code[re_, im_] := N[Sqrt[re], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{re}
\end{array}
Initial program 43.8%
+-commutative43.8%
hypot-def80.0%
Simplified80.0%
Taylor expanded in im around 0 28.4%
associate-*r*28.4%
unpow228.4%
rem-square-sqrt29.0%
metadata-eval29.0%
*-lft-identity29.0%
Simplified29.0%
Final simplification29.0%
(FPCore (re im)
:precision binary64
(let* ((t_0 (sqrt (+ (* re re) (* im im)))))
(if (< re 0.0)
(* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- t_0 re)))))
(* 0.5 (sqrt (* 2.0 (+ t_0 re)))))))
double code(double re, double im) {
double t_0 = sqrt(((re * re) + (im * im)));
double tmp;
if (re < 0.0) {
tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re))));
} else {
tmp = 0.5 * sqrt((2.0 * (t_0 + re)));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt(((re * re) + (im * im)))
if (re < 0.0d0) then
tmp = 0.5d0 * (sqrt(2.0d0) * sqrt(((im * im) / (t_0 - re))))
else
tmp = 0.5d0 * sqrt((2.0d0 * (t_0 + re)))
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = Math.sqrt(((re * re) + (im * im)));
double tmp;
if (re < 0.0) {
tmp = 0.5 * (Math.sqrt(2.0) * Math.sqrt(((im * im) / (t_0 - re))));
} else {
tmp = 0.5 * Math.sqrt((2.0 * (t_0 + re)));
}
return tmp;
}
def code(re, im): t_0 = math.sqrt(((re * re) + (im * im))) tmp = 0 if re < 0.0: tmp = 0.5 * (math.sqrt(2.0) * math.sqrt(((im * im) / (t_0 - re)))) else: tmp = 0.5 * math.sqrt((2.0 * (t_0 + re))) return tmp
function code(re, im) t_0 = sqrt(Float64(Float64(re * re) + Float64(im * im))) tmp = 0.0 if (re < 0.0) tmp = Float64(0.5 * Float64(sqrt(2.0) * sqrt(Float64(Float64(im * im) / Float64(t_0 - re))))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(t_0 + re)))); end return tmp end
function tmp_2 = code(re, im) t_0 = sqrt(((re * re) + (im * im))); tmp = 0.0; if (re < 0.0) tmp = 0.5 * (sqrt(2.0) * sqrt(((im * im) / (t_0 - re)))); else tmp = 0.5 * sqrt((2.0 * (t_0 + re))); end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[Less[re, 0.0], N[(0.5 * N[(N[Sqrt[2.0], $MachinePrecision] * N[Sqrt[N[(N[(im * im), $MachinePrecision] / N[(t$95$0 - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * N[(t$95$0 + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{re \cdot re + im \cdot im}\\
\mathbf{if}\;re < 0:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{t_0 - re}}\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(t_0 + re\right)}\\
\end{array}
\end{array}
herbie shell --seed 2023182
(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)))))