
(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 4 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 -4.4e+160) (* 0.5 (sqrt (* (/ im -1.0) (/ im re)))) (sqrt (* 0.5 (+ re (hypot re im))))))
double code(double re, double im) {
double tmp;
if (re <= -4.4e+160) {
tmp = 0.5 * sqrt(((im / -1.0) * (im / re)));
} else {
tmp = sqrt((0.5 * (re + hypot(re, im))));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= -4.4e+160) {
tmp = 0.5 * Math.sqrt(((im / -1.0) * (im / re)));
} else {
tmp = Math.sqrt((0.5 * (re + Math.hypot(re, im))));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -4.4e+160: tmp = 0.5 * math.sqrt(((im / -1.0) * (im / re))) else: tmp = math.sqrt((0.5 * (re + math.hypot(re, im)))) return tmp
function code(re, im) tmp = 0.0 if (re <= -4.4e+160) tmp = Float64(0.5 * sqrt(Float64(Float64(im / -1.0) * Float64(im / 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 <= -4.4e+160) tmp = 0.5 * sqrt(((im / -1.0) * (im / re))); else tmp = sqrt((0.5 * (re + hypot(re, im)))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -4.4e+160], N[(0.5 * N[Sqrt[N[(N[(im / -1.0), $MachinePrecision] * N[(im / re), $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 -4.4 \cdot 10^{+160}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im}{-1} \cdot \frac{im}{re}}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\end{array}
\end{array}
if re < -4.39999999999999984e160Initial program 2.5%
sqr-neg2.5%
+-commutative2.5%
sqr-neg2.5%
+-commutative2.5%
distribute-rgt-in2.5%
cancel-sign-sub2.5%
distribute-rgt-out--2.5%
sub-neg2.5%
remove-double-neg2.5%
+-commutative2.5%
Simplified4.3%
Taylor expanded in re around -inf 70.1%
mul-1-neg70.1%
distribute-neg-frac270.1%
Simplified70.1%
unpow270.1%
neg-mul-170.1%
times-frac74.4%
Applied egg-rr74.4%
if -4.39999999999999984e160 < re Initial program 50.8%
sqr-neg50.8%
+-commutative50.8%
sqr-neg50.8%
+-commutative50.8%
distribute-rgt-in50.8%
cancel-sign-sub50.8%
distribute-rgt-out--50.8%
sub-neg50.8%
remove-double-neg50.8%
+-commutative50.8%
Simplified89.6%
*-commutative89.6%
hypot-define50.8%
+-commutative50.8%
*-commutative50.8%
add-sqr-sqrt50.4%
sqrt-unprod50.8%
*-commutative50.8%
*-commutative50.8%
swap-sqr50.8%
Applied egg-rr89.6%
*-commutative89.6%
associate-*r*89.6%
metadata-eval89.6%
Simplified89.6%
Final simplification88.3%
(FPCore (re im) :precision binary64 (if (<= re -3.4e+160) (* 0.5 (sqrt (* (/ im -1.0) (/ im re)))) (if (<= re 4.5e-82) (sqrt (* 0.5 im)) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= -3.4e+160) {
tmp = 0.5 * sqrt(((im / -1.0) * (im / re)));
} else if (re <= 4.5e-82) {
tmp = sqrt((0.5 * 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.4d+160)) then
tmp = 0.5d0 * sqrt(((im / (-1.0d0)) * (im / re)))
else if (re <= 4.5d-82) then
tmp = sqrt((0.5d0 * im))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3.4e+160) {
tmp = 0.5 * Math.sqrt(((im / -1.0) * (im / re)));
} else if (re <= 4.5e-82) {
tmp = Math.sqrt((0.5 * im));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.4e+160: tmp = 0.5 * math.sqrt(((im / -1.0) * (im / re))) elif re <= 4.5e-82: tmp = math.sqrt((0.5 * im)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.4e+160) tmp = Float64(0.5 * sqrt(Float64(Float64(im / -1.0) * Float64(im / re)))); elseif (re <= 4.5e-82) tmp = sqrt(Float64(0.5 * im)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.4e+160) tmp = 0.5 * sqrt(((im / -1.0) * (im / re))); elseif (re <= 4.5e-82) tmp = sqrt((0.5 * im)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.4e+160], N[(0.5 * N[Sqrt[N[(N[(im / -1.0), $MachinePrecision] * N[(im / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 4.5e-82], N[Sqrt[N[(0.5 * im), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.4 \cdot 10^{+160}:\\
\;\;\;\;0.5 \cdot \sqrt{\frac{im}{-1} \cdot \frac{im}{re}}\\
\mathbf{elif}\;re \leq 4.5 \cdot 10^{-82}:\\
\;\;\;\;\sqrt{0.5 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < -3.4000000000000003e160Initial program 2.5%
sqr-neg2.5%
+-commutative2.5%
sqr-neg2.5%
+-commutative2.5%
distribute-rgt-in2.5%
cancel-sign-sub2.5%
distribute-rgt-out--2.5%
sub-neg2.5%
remove-double-neg2.5%
+-commutative2.5%
Simplified4.3%
Taylor expanded in re around -inf 70.1%
mul-1-neg70.1%
distribute-neg-frac270.1%
Simplified70.1%
unpow270.1%
neg-mul-170.1%
times-frac74.4%
Applied egg-rr74.4%
if -3.4000000000000003e160 < re < 4.4999999999999998e-82Initial program 52.6%
sqr-neg52.6%
+-commutative52.6%
sqr-neg52.6%
+-commutative52.6%
distribute-rgt-in52.6%
cancel-sign-sub52.6%
distribute-rgt-out--52.6%
sub-neg52.6%
remove-double-neg52.6%
+-commutative52.6%
Simplified83.1%
*-commutative83.1%
hypot-define52.6%
+-commutative52.6%
*-commutative52.6%
add-sqr-sqrt52.3%
sqrt-unprod52.6%
*-commutative52.6%
*-commutative52.6%
swap-sqr52.6%
Applied egg-rr83.1%
*-commutative83.1%
associate-*r*83.1%
metadata-eval83.1%
Simplified83.1%
Taylor expanded in re around 0 36.7%
if 4.4999999999999998e-82 < re Initial program 47.8%
sqr-neg47.8%
+-commutative47.8%
sqr-neg47.8%
+-commutative47.8%
distribute-rgt-in47.8%
cancel-sign-sub47.8%
distribute-rgt-out--47.8%
sub-neg47.8%
remove-double-neg47.8%
+-commutative47.8%
Simplified100.0%
Taylor expanded in re around inf 82.1%
*-commutative82.1%
unpow282.1%
rem-square-sqrt83.7%
associate-*r*83.7%
metadata-eval83.7%
Simplified83.7%
Final simplification56.6%
(FPCore (re im) :precision binary64 (if (<= re 9.8e-79) (sqrt (* 0.5 im)) (sqrt re)))
double code(double re, double im) {
double tmp;
if (re <= 9.8e-79) {
tmp = sqrt((0.5 * 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 <= 9.8d-79) then
tmp = sqrt((0.5d0 * im))
else
tmp = sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 9.8e-79) {
tmp = Math.sqrt((0.5 * im));
} else {
tmp = Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 9.8e-79: tmp = math.sqrt((0.5 * im)) else: tmp = math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 9.8e-79) tmp = sqrt(Float64(0.5 * im)); else tmp = sqrt(re); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 9.8e-79) tmp = sqrt((0.5 * im)); else tmp = sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 9.8e-79], N[Sqrt[N[(0.5 * im), $MachinePrecision]], $MachinePrecision], N[Sqrt[re], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 9.8 \cdot 10^{-79}:\\
\;\;\;\;\sqrt{0.5 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{re}\\
\end{array}
\end{array}
if re < 9.8000000000000001e-79Initial program 45.7%
sqr-neg45.7%
+-commutative45.7%
sqr-neg45.7%
+-commutative45.7%
distribute-rgt-in45.7%
cancel-sign-sub45.7%
distribute-rgt-out--45.7%
sub-neg45.7%
remove-double-neg45.7%
+-commutative45.7%
Simplified72.2%
*-commutative72.2%
hypot-define45.7%
+-commutative45.7%
*-commutative45.7%
add-sqr-sqrt45.4%
sqrt-unprod45.7%
*-commutative45.7%
*-commutative45.7%
swap-sqr45.7%
Applied egg-rr72.2%
*-commutative72.2%
associate-*r*72.2%
metadata-eval72.2%
Simplified72.2%
Taylor expanded in re around 0 32.0%
if 9.8000000000000001e-79 < re Initial program 47.8%
sqr-neg47.8%
+-commutative47.8%
sqr-neg47.8%
+-commutative47.8%
distribute-rgt-in47.8%
cancel-sign-sub47.8%
distribute-rgt-out--47.8%
sub-neg47.8%
remove-double-neg47.8%
+-commutative47.8%
Simplified100.0%
Taylor expanded in re around inf 82.1%
*-commutative82.1%
unpow282.1%
rem-square-sqrt83.7%
associate-*r*83.7%
metadata-eval83.7%
Simplified83.7%
Final simplification50.2%
(FPCore (re im) :precision binary64 (sqrt (* 0.5 im)))
double code(double re, double im) {
return sqrt((0.5 * im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sqrt((0.5d0 * im))
end function
public static double code(double re, double im) {
return Math.sqrt((0.5 * im));
}
def code(re, im): return math.sqrt((0.5 * im))
function code(re, im) return sqrt(Float64(0.5 * im)) end
function tmp = code(re, im) tmp = sqrt((0.5 * im)); end
code[re_, im_] := N[Sqrt[N[(0.5 * im), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{0.5 \cdot im}
\end{array}
Initial program 46.4%
sqr-neg46.4%
+-commutative46.4%
sqr-neg46.4%
+-commutative46.4%
distribute-rgt-in46.4%
cancel-sign-sub46.4%
distribute-rgt-out--46.4%
sub-neg46.4%
remove-double-neg46.4%
+-commutative46.4%
Simplified82.0%
*-commutative82.0%
hypot-define46.4%
+-commutative46.4%
*-commutative46.4%
add-sqr-sqrt46.1%
sqrt-unprod46.4%
*-commutative46.4%
*-commutative46.4%
swap-sqr46.4%
Applied egg-rr82.0%
*-commutative82.0%
associate-*r*82.0%
metadata-eval82.0%
Simplified82.0%
Taylor expanded in re around 0 24.1%
Final simplification24.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 2024095
(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)))))