
(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 -3.1e-17) (* 0.5 (sqrt (* re -4.0))) (if (<= re 5.3e+24) (* 0.5 (sqrt (* im 2.0))) (/ (* 0.5 im) (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= -3.1e-17) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 5.3e+24) {
tmp = 0.5 * sqrt((im * 2.0));
} 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 <= (-3.1d-17)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 5.3d+24) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
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 <= -3.1e-17) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 5.3e+24) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = (0.5 * im) / Math.sqrt(re);
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.1e-17: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 5.3e+24: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = (0.5 * im) / math.sqrt(re) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.1e-17) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 5.3e+24) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(Float64(0.5 * im) / sqrt(re)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.1e-17) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 5.3e+24) tmp = 0.5 * sqrt((im * 2.0)); else tmp = (0.5 * im) / sqrt(re); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.1e-17], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5.3e+24], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(0.5 * im), $MachinePrecision] / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.1 \cdot 10^{-17}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 5.3 \cdot 10^{+24}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -3.0999999999999998e-17Initial program 44.7%
Taylor expanded in re around -inf
*-commutativeN/A
lower-*.f6479.2
Simplified79.2%
if -3.0999999999999998e-17 < re < 5.2999999999999999e24Initial program 50.1%
Taylor expanded in re around 0
*-commutativeN/A
lower-*.f6477.3
Simplified77.3%
if 5.2999999999999999e24 < re Initial program 7.1%
Taylor expanded in re around inf
lower-/.f64N/A
unpow2N/A
lower-*.f6442.3
Simplified42.3%
associate-/l*N/A
sqrt-prodN/A
lift-sqrt.f64N/A
sqrt-divN/A
lift-sqrt.f64N/A
associate-/l*N/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrtN/A
associate-*r/N/A
lift-*.f64N/A
lower-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-sqrt.f6479.8
Applied egg-rr79.8%
Final simplification78.4%
(FPCore (re im) :precision binary64 (if (<= re -3.1e-17) (* 0.5 (sqrt (* re -4.0))) (if (<= re 5.3e+24) (* 0.5 (sqrt (* im 2.0))) (* im (/ 0.5 (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -3.1e-17) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 5.3e+24) {
tmp = 0.5 * sqrt((im * 2.0));
} 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.1d-17)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 5.3d+24) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
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.1e-17) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 5.3e+24) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = im * (0.5 / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.1e-17: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 5.3e+24: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = im * (0.5 / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.1e-17) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 5.3e+24) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); 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.1e-17) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 5.3e+24) tmp = 0.5 * sqrt((im * 2.0)); else tmp = im * (0.5 / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.1e-17], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 5.3e+24], N[(0.5 * N[Sqrt[N[(im * 2.0), $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.1 \cdot 10^{-17}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 5.3 \cdot 10^{+24}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;im \cdot \frac{0.5}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -3.0999999999999998e-17Initial program 44.7%
Taylor expanded in re around -inf
*-commutativeN/A
lower-*.f6479.2
Simplified79.2%
if -3.0999999999999998e-17 < re < 5.2999999999999999e24Initial program 50.1%
Taylor expanded in re around 0
*-commutativeN/A
lower-*.f6477.3
Simplified77.3%
if 5.2999999999999999e24 < re Initial program 7.1%
Taylor expanded in re around inf
lower-/.f64N/A
unpow2N/A
lower-*.f6442.3
Simplified42.3%
associate-/l*N/A
sqrt-prodN/A
lift-sqrt.f64N/A
sqrt-divN/A
lift-sqrt.f64N/A
associate-/l*N/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrtN/A
associate-*r/N/A
*-commutativeN/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f64N/A
lower-sqrt.f6479.6
Applied egg-rr79.6%
(FPCore (re im) :precision binary64 (if (<= re -3.1e-17) (* 0.5 (sqrt (* re -4.0))) (* 0.5 (sqrt (* im 2.0)))))
double code(double re, double im) {
double tmp;
if (re <= -3.1e-17) {
tmp = 0.5 * sqrt((re * -4.0));
} 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 (re <= (-3.1d-17)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
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 (re <= -3.1e-17) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else {
tmp = 0.5 * Math.sqrt((im * 2.0));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.1e-17: tmp = 0.5 * math.sqrt((re * -4.0)) else: tmp = 0.5 * math.sqrt((im * 2.0)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.1e-17) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); else tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.1e-17) tmp = 0.5 * sqrt((re * -4.0)); else tmp = 0.5 * sqrt((im * 2.0)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.1e-17], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.1 \cdot 10^{-17}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\end{array}
\end{array}
if re < -3.0999999999999998e-17Initial program 44.7%
Taylor expanded in re around -inf
*-commutativeN/A
lower-*.f6479.2
Simplified79.2%
if -3.0999999999999998e-17 < re Initial program 36.1%
Taylor expanded in re around 0
*-commutativeN/A
lower-*.f6460.5
Simplified60.5%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* im 2.0))))
double code(double re, double im) {
return 0.5 * sqrt((im * 2.0));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt((im * 2.0d0))
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt((im * 2.0));
}
def code(re, im): return 0.5 * math.sqrt((im * 2.0))
function code(re, im) return Float64(0.5 * sqrt(Float64(im * 2.0))) end
function tmp = code(re, im) tmp = 0.5 * sqrt((im * 2.0)); end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{im \cdot 2}
\end{array}
Initial program 38.5%
Taylor expanded in re around 0
*-commutativeN/A
lower-*.f6451.2
Simplified51.2%
(FPCore (re im) :precision binary64 (* 0.5 (sqrt im)))
double code(double re, double im) {
return 0.5 * sqrt(im);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * sqrt(im)
end function
public static double code(double re, double im) {
return 0.5 * Math.sqrt(im);
}
def code(re, im): return 0.5 * math.sqrt(im)
function code(re, im) return Float64(0.5 * sqrt(im)) end
function tmp = code(re, im) tmp = 0.5 * sqrt(im); end
code[re_, im_] := N[(0.5 * N[Sqrt[im], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \sqrt{im}
\end{array}
Initial program 38.5%
Taylor expanded in re around 0
*-commutativeN/A
lower-*.f6451.2
Simplified51.2%
Taylor expanded in im around -inf
mul-1-negN/A
lower-neg.f640.0
Simplified0.0%
lift-neg.f64N/A
pow1/2N/A
metadata-evalN/A
metadata-evalN/A
pow-sqrN/A
pow-prod-downN/A
lift-neg.f64N/A
distribute-lft-neg-inN/A
lift-neg.f64N/A
distribute-rgt-neg-inN/A
lift-*.f64N/A
remove-double-negN/A
lift-*.f64N/A
pow-prod-downN/A
pow-sqrN/A
metadata-evalN/A
metadata-evalN/A
pow1/2N/A
lift-sqrt.f64N/A
*-commutativeN/A
lower-*.f6413.1
Applied egg-rr13.1%
Final simplification13.1%
herbie shell --seed 2024214
(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)))))