
(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 2.8e+73) (* 0.5 (sqrt (* 2.0 (- (hypot re im) re)))) (* im (/ 0.5 (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= 2.8e+73) {
tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
} else {
tmp = im * (0.5 / sqrt(re));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 2.8e+73) {
tmp = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
} else {
tmp = im * (0.5 / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 2.8e+73: tmp = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re))) else: tmp = im * (0.5 / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= 2.8e+73) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re)))); else tmp = Float64(im * Float64(0.5 / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 2.8e+73) tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re))); else tmp = im * (0.5 / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 2.8e+73], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(im * N[(0.5 / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 2.8 \cdot 10^{+73}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < 2.80000000000000008e73Initial program 48.2%
*-lowering-*.f64N/A
sqrt-lowering-sqrt.f64N/A
*-lowering-*.f64N/A
--lowering--.f64N/A
hypot-defineN/A
hypot-lowering-hypot.f6490.3%
Simplified90.3%
if 2.80000000000000008e73 < re Initial program 8.8%
Taylor expanded in re around inf
/-lowering-/.f64N/A
unpow2N/A
*-lowering-*.f6443.2%
Simplified43.2%
clear-numN/A
associate-/r/N/A
sqrt-prodN/A
sqrt-prodN/A
rem-square-sqrtN/A
associate-*r*N/A
*-lowering-*.f64N/A
sqrt-divN/A
metadata-evalN/A
un-div-invN/A
/-lowering-/.f64N/A
sqrt-lowering-sqrt.f6482.7%
Applied egg-rr82.7%
Final simplification88.7%
(FPCore (re im)
:precision binary64
(if (<= re -3.9e+90)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 3.9e+70)
(* 0.5 (sqrt (* 2.0 (- im re))))
(* im (/ 0.5 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -3.9e+90) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 3.9e+70) {
tmp = 0.5 * sqrt((2.0 * (im - re)));
} else {
tmp = im * (0.5 / 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.9d+90)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 3.9d+70) then
tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
else
tmp = im * (0.5d0 / sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3.9e+90) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 3.9e+70) {
tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
} else {
tmp = im * (0.5 / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.9e+90: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 3.9e+70: tmp = 0.5 * math.sqrt((2.0 * (im - re))) else: tmp = im * (0.5 / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.9e+90) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 3.9e+70) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re)))); else tmp = Float64(im * Float64(0.5 / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.9e+90) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 3.9e+70) tmp = 0.5 * sqrt((2.0 * (im - re))); else tmp = im * (0.5 / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.9e+90], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.9e+70], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(im * N[(0.5 / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.9 \cdot 10^{+90}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 3.9 \cdot 10^{+70}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -3.9000000000000002e90Initial program 19.4%
Taylor expanded in re around -inf
*-commutativeN/A
*-lowering-*.f6483.1%
Simplified83.1%
if -3.9000000000000002e90 < re < 3.89999999999999975e70Initial program 54.2%
Taylor expanded in re around 0
Simplified77.7%
if 3.89999999999999975e70 < re Initial program 12.1%
Taylor expanded in re around inf
/-lowering-/.f64N/A
unpow2N/A
*-lowering-*.f6443.6%
Simplified43.6%
clear-numN/A
associate-/r/N/A
sqrt-prodN/A
sqrt-prodN/A
rem-square-sqrtN/A
associate-*r*N/A
*-lowering-*.f64N/A
sqrt-divN/A
metadata-evalN/A
un-div-invN/A
/-lowering-/.f64N/A
sqrt-lowering-sqrt.f6481.7%
Applied egg-rr81.7%
Final simplification79.4%
(FPCore (re im) :precision binary64 (if (<= re -1.15e+89) (* 0.5 (sqrt (* re -4.0))) (if (<= re 3.2e+70) (* 0.5 (sqrt (* 2.0 im))) (* im (/ 0.5 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -1.15e+89) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 3.2e+70) {
tmp = 0.5 * sqrt((2.0 * im));
} else {
tmp = im * (0.5 / 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.15d+89)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 3.2d+70) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
else
tmp = im * (0.5d0 / sqrt(re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.15e+89) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 3.2e+70) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = im * (0.5 / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.15e+89: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 3.2e+70: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = im * (0.5 / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.15e+89) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 3.2e+70) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = Float64(im * Float64(0.5 / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.15e+89) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 3.2e+70) tmp = 0.5 * sqrt((2.0 * im)); else tmp = im * (0.5 / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.15e+89], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 3.2e+70], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(im * N[(0.5 / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.15 \cdot 10^{+89}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 3.2 \cdot 10^{+70}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -1.1499999999999999e89Initial program 19.4%
Taylor expanded in re around -inf
*-commutativeN/A
*-lowering-*.f6483.1%
Simplified83.1%
if -1.1499999999999999e89 < re < 3.2000000000000002e70Initial program 54.2%
Taylor expanded in re around 0
*-commutativeN/A
*-lowering-*.f6477.2%
Simplified77.2%
if 3.2000000000000002e70 < re Initial program 12.1%
Taylor expanded in re around inf
/-lowering-/.f64N/A
unpow2N/A
*-lowering-*.f6443.6%
Simplified43.6%
clear-numN/A
associate-/r/N/A
sqrt-prodN/A
sqrt-prodN/A
rem-square-sqrtN/A
associate-*r*N/A
*-lowering-*.f64N/A
sqrt-divN/A
metadata-evalN/A
un-div-invN/A
/-lowering-/.f64N/A
sqrt-lowering-sqrt.f6481.7%
Applied egg-rr81.7%
Final simplification79.0%
(FPCore (re im) :precision binary64 (if (<= re -1.02e+96) (* 0.5 (sqrt (* re -4.0))) (* 0.5 (sqrt (* 2.0 im)))))
double code(double re, double im) {
double tmp;
if (re <= -1.02e+96) {
tmp = 0.5 * sqrt((re * -4.0));
} else {
tmp = 0.5 * sqrt((2.0 * 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.02d+96)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else
tmp = 0.5d0 * sqrt((2.0d0 * im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.02e+96) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else {
tmp = 0.5 * Math.sqrt((2.0 * im));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.02e+96: tmp = 0.5 * math.sqrt((re * -4.0)) else: tmp = 0.5 * math.sqrt((2.0 * im)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.02e+96) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.02e+96) tmp = 0.5 * sqrt((re * -4.0)); else tmp = 0.5 * sqrt((2.0 * im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.02e+96], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.02 \cdot 10^{+96}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\end{array}
\end{array}
if re < -1.02000000000000001e96Initial program 19.4%
Taylor expanded in re around -inf
*-commutativeN/A
*-lowering-*.f6483.1%
Simplified83.1%
if -1.02000000000000001e96 < re Initial program 43.6%
Taylor expanded in re around 0
*-commutativeN/A
*-lowering-*.f6463.3%
Simplified63.3%
Final simplification66.2%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 im))))
double code(double re, double im) {
return 0.5 * sqrt((2.0 * im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((2.0d0 * im))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * im));
}
def code(re, im): return 0.5 * math.sqrt((2.0 * im))
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * im))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * im)); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{2 \cdot im}
\end{array}
Initial program 40.0%
Taylor expanded in re around 0
*-commutativeN/A
*-lowering-*.f6457.1%
Simplified57.1%
Final simplification57.1%
herbie shell --seed 2024163
(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)))))