
(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 (<= re 115000000.0) (sqrt (* 0.5 (- (hypot im re) re))) (/ (* 0.5 im) (sqrt re))))
double code(double re, double im) {
double tmp;
if (re <= 115000000.0) {
tmp = sqrt((0.5 * (hypot(im, re) - re)));
} else {
tmp = (0.5 * im) / sqrt(re);
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 115000000.0) {
tmp = Math.sqrt((0.5 * (Math.hypot(im, re) - re)));
} else {
tmp = (0.5 * im) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 115000000.0: tmp = math.sqrt((0.5 * (math.hypot(im, re) - re))) else: tmp = (0.5 * im) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= 115000000.0) tmp = sqrt(Float64(0.5 * Float64(hypot(im, re) - re))); else tmp = Float64(Float64(0.5 * im) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 115000000.0) tmp = sqrt((0.5 * (hypot(im, re) - re))); else tmp = (0.5 * im) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 115000000.0], N[Sqrt[N[(0.5 * N[(N[Sqrt[im ^ 2 + re ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 115000000:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(im, re\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < 1.15e8Initial program 49.3%
pow149.3%
Applied egg-rr92.5%
unpow192.5%
*-commutative92.5%
associate-*r*92.5%
metadata-eval92.5%
hypot-undefine49.3%
unpow249.3%
unpow249.3%
+-commutative49.3%
unpow249.3%
unpow249.3%
hypot-undefine92.5%
Simplified92.5%
if 1.15e8 < re Initial program 8.4%
Taylor expanded in re around inf 50.0%
sqrt-div59.8%
sqrt-pow181.2%
metadata-eval81.2%
pow181.2%
associate-*r/81.2%
Applied egg-rr81.2%
(FPCore (re im) :precision binary64 (if (<= re -9e+39) (sqrt (- re)) (if (<= re 470.0) (sqrt (* 0.5 (- im re))) (/ (* 0.5 im) (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= -9e+39) {
tmp = sqrt(-re);
} else if (re <= 470.0) {
tmp = sqrt((0.5 * (im - re)));
} else {
tmp = (0.5 * im) / 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 <= (-9d+39)) then
tmp = sqrt(-re)
else if (re <= 470.0d0) then
tmp = sqrt((0.5d0 * (im - re)))
else
tmp = (0.5d0 * im) / sqrt(re)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -9e+39) {
tmp = Math.sqrt(-re);
} else if (re <= 470.0) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = (0.5 * im) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -9e+39: tmp = math.sqrt(-re) elif re <= 470.0: tmp = math.sqrt((0.5 * (im - re))) else: tmp = (0.5 * im) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -9e+39) tmp = sqrt(Float64(-re)); elseif (re <= 470.0) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64(Float64(0.5 * im) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -9e+39) tmp = sqrt(-re); elseif (re <= 470.0) tmp = sqrt((0.5 * (im - re))); else tmp = (0.5 * im) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -9e+39], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 470.0], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -9 \cdot 10^{+39}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 470:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -8.99999999999999991e39Initial program 41.9%
pow141.9%
Applied egg-rr100.0%
unpow1100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine41.9%
unpow241.9%
unpow241.9%
+-commutative41.9%
unpow241.9%
unpow241.9%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around -inf 84.9%
neg-mul-184.9%
Simplified84.9%
if -8.99999999999999991e39 < re < 470Initial program 52.4%
pow152.4%
Applied egg-rr89.4%
unpow189.4%
*-commutative89.4%
associate-*r*89.4%
metadata-eval89.4%
hypot-undefine52.4%
unpow252.4%
unpow252.4%
+-commutative52.4%
unpow252.4%
unpow252.4%
hypot-undefine89.4%
Simplified89.4%
Taylor expanded in re around 0 79.5%
neg-mul-179.5%
unsub-neg79.5%
Simplified79.5%
if 470 < re Initial program 8.4%
Taylor expanded in re around inf 50.0%
sqrt-div59.8%
sqrt-pow181.2%
metadata-eval81.2%
pow181.2%
associate-*r/81.2%
Applied egg-rr81.2%
(FPCore (re im) :precision binary64 (if (<= re -3.1e+29) (sqrt (- re)) (if (<= re 4.2) (sqrt (* 0.5 (- im re))) (* im (sqrt (/ 0.25 re))))))
double code(double re, double im) {
double tmp;
if (re <= -3.1e+29) {
tmp = sqrt(-re);
} else if (re <= 4.2) {
tmp = sqrt((0.5 * (im - re)));
} else {
tmp = im * sqrt((0.25 / 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.1d+29)) then
tmp = sqrt(-re)
else if (re <= 4.2d0) then
tmp = sqrt((0.5d0 * (im - re)))
else
tmp = im * sqrt((0.25d0 / re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3.1e+29) {
tmp = Math.sqrt(-re);
} else if (re <= 4.2) {
tmp = Math.sqrt((0.5 * (im - re)));
} else {
tmp = im * Math.sqrt((0.25 / re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.1e+29: tmp = math.sqrt(-re) elif re <= 4.2: tmp = math.sqrt((0.5 * (im - re))) else: tmp = im * math.sqrt((0.25 / re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.1e+29) tmp = sqrt(Float64(-re)); elseif (re <= 4.2) tmp = sqrt(Float64(0.5 * Float64(im - re))); else tmp = Float64(im * sqrt(Float64(0.25 / re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.1e+29) tmp = sqrt(-re); elseif (re <= 4.2) tmp = sqrt((0.5 * (im - re))); else tmp = im * sqrt((0.25 / re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.1e+29], N[Sqrt[(-re)], $MachinePrecision], If[LessEqual[re, 4.2], N[Sqrt[N[(0.5 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(im * N[Sqrt[N[(0.25 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.1 \cdot 10^{+29}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{elif}\;re \leq 4.2:\\
\;\;\;\;\sqrt{0.5 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \sqrt{\frac{0.25}{re}}\\
\end{array}
\end{array}
if re < -3.0999999999999999e29Initial program 41.9%
pow141.9%
Applied egg-rr100.0%
unpow1100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine41.9%
unpow241.9%
unpow241.9%
+-commutative41.9%
unpow241.9%
unpow241.9%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around -inf 84.9%
neg-mul-184.9%
Simplified84.9%
if -3.0999999999999999e29 < re < 4.20000000000000018Initial program 52.4%
pow152.4%
Applied egg-rr89.4%
unpow189.4%
*-commutative89.4%
associate-*r*89.4%
metadata-eval89.4%
hypot-undefine52.4%
unpow252.4%
unpow252.4%
+-commutative52.4%
unpow252.4%
unpow252.4%
hypot-undefine89.4%
Simplified89.4%
Taylor expanded in re around 0 79.5%
neg-mul-179.5%
unsub-neg79.5%
Simplified79.5%
if 4.20000000000000018 < re Initial program 8.4%
pow18.4%
Applied egg-rr35.4%
unpow135.4%
*-commutative35.4%
associate-*r*35.4%
metadata-eval35.4%
hypot-undefine8.4%
unpow28.4%
unpow28.4%
+-commutative8.4%
unpow28.4%
unpow28.4%
hypot-undefine35.4%
Simplified35.4%
Taylor expanded in im around 0 80.3%
associate-*l*80.4%
unpow280.4%
rem-square-sqrt81.2%
Simplified81.2%
add-sqr-sqrt80.9%
sqrt-unprod81.2%
*-commutative81.2%
*-commutative81.2%
swap-sqr81.2%
add-sqr-sqrt81.2%
metadata-eval81.2%
Applied egg-rr81.2%
associate-*l/81.2%
metadata-eval81.2%
Simplified81.2%
(FPCore (re im) :precision binary64 (if (<= re -1.16e+33) (sqrt (- re)) (sqrt (* 0.5 im))))
double code(double re, double im) {
double tmp;
if (re <= -1.16e+33) {
tmp = sqrt(-re);
} else {
tmp = sqrt((0.5 * im));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-1.16d+33)) then
tmp = sqrt(-re)
else
tmp = sqrt((0.5d0 * im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.16e+33) {
tmp = Math.sqrt(-re);
} else {
tmp = Math.sqrt((0.5 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.16e+33: tmp = math.sqrt(-re) else: tmp = math.sqrt((0.5 * im)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.16e+33) tmp = sqrt(Float64(-re)); else tmp = sqrt(Float64(0.5 * im)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.16e+33) tmp = sqrt(-re); else tmp = sqrt((0.5 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.16e+33], N[Sqrt[(-re)], $MachinePrecision], N[Sqrt[N[(0.5 * im), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.16 \cdot 10^{+33}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{0.5 \cdot im}\\
\end{array}
\end{array}
if re < -1.16000000000000001e33Initial program 41.9%
pow141.9%
Applied egg-rr100.0%
unpow1100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine41.9%
unpow241.9%
unpow241.9%
+-commutative41.9%
unpow241.9%
unpow241.9%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around -inf 84.9%
neg-mul-184.9%
Simplified84.9%
if -1.16000000000000001e33 < re Initial program 37.6%
pow137.6%
Applied egg-rr71.3%
unpow171.3%
*-commutative71.3%
associate-*r*71.3%
metadata-eval71.3%
hypot-undefine37.6%
unpow237.6%
unpow237.6%
+-commutative37.6%
unpow237.6%
unpow237.6%
hypot-undefine71.3%
Simplified71.3%
Taylor expanded in im around inf 60.2%
*-commutative60.2%
Simplified60.2%
Final simplification65.6%
(FPCore (re im) :precision binary64 (if (<= re -5e-310) (sqrt (- re)) 0.0))
double code(double re, double im) {
double tmp;
if (re <= -5e-310) {
tmp = sqrt(-re);
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-5d-310)) then
tmp = sqrt(-re)
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -5e-310) {
tmp = Math.sqrt(-re);
} else {
tmp = 0.0;
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -5e-310: tmp = math.sqrt(-re) else: tmp = 0.0 return tmp
function code(re, im) tmp = 0.0 if (re <= -5e-310) tmp = sqrt(Float64(-re)); else tmp = 0.0; end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -5e-310) tmp = sqrt(-re); else tmp = 0.0; end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -5e-310], N[Sqrt[(-re)], $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\sqrt{-re}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if re < -4.999999999999985e-310Initial program 55.6%
pow155.6%
Applied egg-rr100.0%
unpow1100.0%
*-commutative100.0%
associate-*r*100.0%
metadata-eval100.0%
hypot-undefine55.6%
unpow255.6%
unpow255.6%
+-commutative55.6%
unpow255.6%
unpow255.6%
hypot-undefine100.0%
Simplified100.0%
Taylor expanded in re around -inf 53.2%
neg-mul-153.2%
Simplified53.2%
if -4.999999999999985e-310 < re Initial program 22.8%
Taylor expanded in re around inf 9.6%
Taylor expanded in re around 0 9.6%
(FPCore (re im) :precision binary64 0.0)
double code(double re, double im) {
return 0.0;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.0d0
end function
public static double code(double re, double im) {
return 0.0;
}
def code(re, im): return 0.0
function code(re, im) return 0.0 end
function tmp = code(re, im) tmp = 0.0; end
code[re_, im_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 38.6%
Taylor expanded in re around inf 6.4%
Taylor expanded in re around 0 6.4%
herbie shell --seed 2024130
(FPCore (re im)
:name "math.sqrt on complex, imaginary part, im greater than 0 branch"
:precision binary64
:pre (> im 0.0)
(* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))