
(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 -1.15e+25) (* 0.5 (sqrt (/ (pow im 2.0) (- re)))) (sqrt (* 0.5 (+ re (hypot re im))))))
double code(double re, double im) {
double tmp;
if (re <= -1.15e+25) {
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 (re <= -1.15e+25) {
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 re <= -1.15e+25: 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 (re <= -1.15e+25) tmp = Float64(0.5 * sqrt(Float64((im ^ 2.0) / Float64(-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 (re <= -1.15e+25) 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[re, -1.15e+25], 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}\;re \leq -1.15 \cdot 10^{+25}:\\
\;\;\;\;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 re < -1.1499999999999999e25Initial program 7.0%
sqr-neg7.0%
+-commutative7.0%
sqr-neg7.0%
+-commutative7.0%
distribute-rgt-in7.0%
cancel-sign-sub7.0%
distribute-rgt-out--7.0%
sub-neg7.0%
remove-double-neg7.0%
+-commutative7.0%
Simplified25.9%
Taylor expanded in re around -inf 58.9%
mul-1-neg58.9%
distribute-neg-frac258.9%
Simplified58.9%
if -1.1499999999999999e25 < re Initial program 49.3%
sqr-neg49.3%
+-commutative49.3%
sqr-neg49.3%
+-commutative49.3%
distribute-rgt-in49.3%
cancel-sign-sub49.3%
distribute-rgt-out--49.3%
sub-neg49.3%
remove-double-neg49.3%
+-commutative49.3%
Simplified90.3%
*-commutative90.3%
hypot-define49.3%
+-commutative49.3%
*-commutative49.3%
add-sqr-sqrt49.0%
sqrt-unprod49.3%
*-commutative49.3%
*-commutative49.3%
swap-sqr49.3%
Applied egg-rr90.3%
*-commutative90.3%
associate-*r*91.2%
metadata-eval91.2%
Simplified91.2%
Final simplification83.5%
(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 39.2%
sqr-neg39.2%
+-commutative39.2%
sqr-neg39.2%
+-commutative39.2%
distribute-rgt-in39.2%
cancel-sign-sub39.2%
distribute-rgt-out--39.2%
sub-neg39.2%
remove-double-neg39.2%
+-commutative39.2%
Simplified74.9%
*-commutative74.9%
hypot-define39.2%
+-commutative39.2%
*-commutative39.2%
add-sqr-sqrt39.0%
sqrt-unprod39.2%
*-commutative39.2%
*-commutative39.2%
swap-sqr39.2%
Applied egg-rr74.9%
*-commutative74.9%
associate-*r*75.7%
metadata-eval75.7%
Simplified75.7%
Final simplification75.7%
(FPCore (re im) :precision binary64 (if (<= re -3.7e+45) (* 0.5 (sqrt (* 2.0 (- re re)))) (if (<= re 4.9e+102) (* 0.5 (sqrt (* 2.0 (+ re im)))) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -3.7e+45) {
tmp = 0.5 * sqrt((2.0 * (re - re)));
} else if (re <= 4.9e+102) {
tmp = 0.5 * sqrt((2.0 * (re + im)));
} 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 <= (-3.7d+45)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re - re)))
else if (re <= 4.9d+102) then
tmp = 0.5d0 * sqrt((2.0d0 * (re + im)))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3.7e+45) {
tmp = 0.5 * Math.sqrt((2.0 * (re - re)));
} else if (re <= 4.9e+102) {
tmp = 0.5 * Math.sqrt((2.0 * (re + im)));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.7e+45: tmp = 0.5 * math.sqrt((2.0 * (re - re))) elif re <= 4.9e+102: tmp = 0.5 * math.sqrt((2.0 * (re + im))) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.7e+45) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re - re)))); elseif (re <= 4.9e+102) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + im)))); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.7e+45) tmp = 0.5 * sqrt((2.0 * (re - re))); elseif (re <= 4.9e+102) tmp = 0.5 * sqrt((2.0 * (re + im))); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.7e+45], N[(0.5 * N[Sqrt[N[(2.0 * N[(re - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 4.9e+102], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.7 \cdot 10^{+45}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - re\right)}\\
\mathbf{elif}\;re \leq 4.9 \cdot 10^{+102}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -3.69999999999999977e45Initial program 6.9%
Taylor expanded in re around -inf 20.2%
mul-1-neg20.2%
Simplified20.2%
if -3.69999999999999977e45 < re < 4.90000000000000045e102Initial program 53.7%
sqr-neg53.7%
+-commutative53.7%
sqr-neg53.7%
+-commutative53.7%
distribute-rgt-in53.7%
cancel-sign-sub53.7%
distribute-rgt-out--53.7%
sub-neg53.7%
remove-double-neg53.7%
+-commutative53.7%
Simplified86.6%
Taylor expanded in re around 0 38.2%
if 4.90000000000000045e102 < re Initial program 29.8%
sqr-neg29.8%
+-commutative29.8%
sqr-neg29.8%
+-commutative29.8%
distribute-rgt-in29.8%
cancel-sign-sub29.8%
distribute-rgt-out--29.8%
sub-neg29.8%
remove-double-neg29.8%
+-commutative29.8%
Simplified95.8%
Taylor expanded in re around inf 85.6%
*-commutative85.6%
unpow285.6%
rem-square-sqrt87.2%
associate-*r*87.2%
metadata-eval87.2%
Simplified87.2%
Final simplification42.9%
(FPCore (re im) :precision binary64 (if (<= re 2.6e+103) (* 0.5 (sqrt (* im 2.0))) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= 2.6e+103) {
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 <= 2.6d+103) 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 <= 2.6e+103) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 2.6e+103: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 2.6e+103) 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 <= 2.6e+103) tmp = 0.5 * sqrt((im * 2.0)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 2.6e+103], 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 2.6 \cdot 10^{+103}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 2.6000000000000002e103Initial program 41.3%
sqr-neg41.3%
+-commutative41.3%
sqr-neg41.3%
+-commutative41.3%
distribute-rgt-in41.3%
cancel-sign-sub41.3%
distribute-rgt-out--41.3%
sub-neg41.3%
remove-double-neg41.3%
+-commutative41.3%
Simplified70.5%
Taylor expanded in re around 0 28.2%
*-commutative28.2%
Simplified28.2%
if 2.6000000000000002e103 < re Initial program 29.8%
sqr-neg29.8%
+-commutative29.8%
sqr-neg29.8%
+-commutative29.8%
distribute-rgt-in29.8%
cancel-sign-sub29.8%
distribute-rgt-out--29.8%
sub-neg29.8%
remove-double-neg29.8%
+-commutative29.8%
Simplified95.8%
Taylor expanded in re around inf 85.6%
*-commutative85.6%
unpow285.6%
rem-square-sqrt87.2%
associate-*r*87.2%
metadata-eval87.2%
Simplified87.2%
Final simplification38.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 39.2%
sqr-neg39.2%
+-commutative39.2%
sqr-neg39.2%
+-commutative39.2%
distribute-rgt-in39.2%
cancel-sign-sub39.2%
distribute-rgt-out--39.2%
sub-neg39.2%
remove-double-neg39.2%
+-commutative39.2%
Simplified74.9%
Taylor expanded in re around inf 24.6%
*-commutative24.6%
unpow224.6%
rem-square-sqrt25.1%
associate-*r*25.1%
metadata-eval25.1%
Simplified25.1%
Final simplification25.1%
(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 2024131
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:alt
(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)))))