
(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.05e-10) (* 0.5 (sqrt (* 2.0 (- (hypot re im) re)))) (* 0.5 (/ im (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= 2.05e-10) {
tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
} else {
tmp = 0.5 * (im / sqrt(re));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 2.05e-10) {
tmp = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
} else {
tmp = 0.5 * (im / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 2.05e-10: tmp = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re))) else: tmp = 0.5 * (im / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= 2.05e-10) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re)))); else tmp = Float64(0.5 * Float64(im / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 2.05e-10) tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re))); else tmp = 0.5 * (im / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 2.05e-10], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 2.05 \cdot 10^{-10}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < 2.0499999999999999e-10Initial program 51.3%
hypot-def98.5%
Simplified98.5%
if 2.0499999999999999e-10 < re Initial program 6.8%
Taylor expanded in re around inf 52.1%
unpow252.1%
Simplified52.1%
expm1-log1p-u52.1%
expm1-udef13.1%
associate-*r*13.1%
metadata-eval13.1%
*-un-lft-identity13.1%
sqrt-div13.1%
sqrt-prod16.6%
add-sqr-sqrt16.6%
Applied egg-rr16.6%
expm1-def78.9%
expm1-log1p79.2%
Simplified79.2%
Final simplification94.1%
(FPCore (re im)
:precision binary64
(if (<= re -4.4e-6)
(* 0.5 (sqrt (* 2.0 (* re -2.0))))
(if (<= re 0.0085)
(* 0.5 (sqrt (* 2.0 (- im re))))
(* 0.5 (/ im (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -4.4e-6) {
tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
} else if (re <= 0.0085) {
tmp = 0.5 * sqrt((2.0 * (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 <= (-4.4d-6)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
else if (re <= 0.0085d0) then
tmp = 0.5d0 * sqrt((2.0d0 * (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 <= -4.4e-6) {
tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
} else if (re <= 0.0085) {
tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
} else {
tmp = 0.5 * (im / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -4.4e-6: tmp = 0.5 * math.sqrt((2.0 * (re * -2.0))) elif re <= 0.0085: tmp = 0.5 * math.sqrt((2.0 * (im - re))) else: tmp = 0.5 * (im / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -4.4e-6) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))); elseif (re <= 0.0085) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re)))); else tmp = Float64(0.5 * Float64(im / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4.4e-6) tmp = 0.5 * sqrt((2.0 * (re * -2.0))); elseif (re <= 0.0085) tmp = 0.5 * sqrt((2.0 * (im - re))); else tmp = 0.5 * (im / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -4.4e-6], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 0.0085], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.4 \cdot 10^{-6}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{elif}\;re \leq 0.0085:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -4.4000000000000002e-6Initial program 37.0%
Taylor expanded in re around -inf 79.4%
*-commutative79.4%
Simplified79.4%
if -4.4000000000000002e-6 < re < 0.0085000000000000006Initial program 60.3%
Taylor expanded in re around 0 87.5%
if 0.0085000000000000006 < re Initial program 6.8%
Taylor expanded in re around inf 52.1%
unpow252.1%
Simplified52.1%
expm1-log1p-u52.1%
expm1-udef13.1%
associate-*r*13.1%
metadata-eval13.1%
*-un-lft-identity13.1%
sqrt-div13.1%
sqrt-prod16.6%
add-sqr-sqrt16.6%
Applied egg-rr16.6%
expm1-def78.9%
expm1-log1p79.2%
Simplified79.2%
Final simplification83.2%
(FPCore (re im) :precision binary64 (if (<= re -4.4e-6) (* 0.5 (sqrt (* 2.0 (* re -2.0)))) (if (<= re 14.2) (* 0.5 (sqrt (* 2.0 im))) (* 0.5 (/ im (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -4.4e-6) {
tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
} else if (re <= 14.2) {
tmp = 0.5 * sqrt((2.0 * im));
} 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 <= (-4.4d-6)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
else if (re <= 14.2d0) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
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 <= -4.4e-6) {
tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
} else if (re <= 14.2) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = 0.5 * (im / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -4.4e-6: tmp = 0.5 * math.sqrt((2.0 * (re * -2.0))) elif re <= 14.2: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = 0.5 * (im / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= -4.4e-6) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))); elseif (re <= 14.2) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = Float64(0.5 * Float64(im / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4.4e-6) tmp = 0.5 * sqrt((2.0 * (re * -2.0))); elseif (re <= 14.2) tmp = 0.5 * sqrt((2.0 * im)); else tmp = 0.5 * (im / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -4.4e-6], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 14.2], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.4 \cdot 10^{-6}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{elif}\;re \leq 14.2:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -4.4000000000000002e-6Initial program 37.0%
Taylor expanded in re around -inf 79.4%
*-commutative79.4%
Simplified79.4%
if -4.4000000000000002e-6 < re < 14.199999999999999Initial program 59.4%
Taylor expanded in re around 0 83.7%
expm1-log1p-u79.9%
expm1-udef59.4%
sqrt-unprod59.4%
Applied egg-rr59.4%
expm1-def80.1%
expm1-log1p84.3%
*-commutative84.3%
Simplified84.3%
if 14.199999999999999 < re Initial program 6.9%
Taylor expanded in re around inf 52.1%
unpow252.1%
Simplified52.1%
expm1-log1p-u52.1%
expm1-udef13.4%
associate-*r*13.4%
metadata-eval13.4%
*-un-lft-identity13.4%
sqrt-div13.4%
sqrt-prod17.0%
add-sqr-sqrt17.0%
Applied egg-rr17.0%
expm1-def79.8%
expm1-log1p80.1%
Simplified80.1%
Final simplification81.9%
(FPCore (re im) :precision binary64 (if (<= re 23.0) (* 0.5 (sqrt (* 2.0 im))) (* 0.5 (/ im (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= 23.0) {
tmp = 0.5 * sqrt((2.0 * im));
} 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 <= 23.0d0) then
tmp = 0.5d0 * sqrt((2.0d0 * im))
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 <= 23.0) {
tmp = 0.5 * Math.sqrt((2.0 * im));
} else {
tmp = 0.5 * (im / Math.sqrt(re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 23.0: tmp = 0.5 * math.sqrt((2.0 * im)) else: tmp = 0.5 * (im / math.sqrt(re)) return tmp
function code(re, im) tmp = 0.0 if (re <= 23.0) tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); else tmp = Float64(0.5 * Float64(im / sqrt(re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 23.0) tmp = 0.5 * sqrt((2.0 * im)); else tmp = 0.5 * (im / sqrt(re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 23.0], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 23:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < 23Initial program 50.8%
Taylor expanded in re around 0 62.1%
expm1-log1p-u59.1%
expm1-udef46.0%
sqrt-unprod46.0%
Applied egg-rr46.0%
expm1-def59.2%
expm1-log1p62.5%
*-commutative62.5%
Simplified62.5%
if 23 < re Initial program 6.9%
Taylor expanded in re around inf 52.1%
unpow252.1%
Simplified52.1%
expm1-log1p-u52.1%
expm1-udef13.4%
associate-*r*13.4%
metadata-eval13.4%
*-un-lft-identity13.4%
sqrt-div13.4%
sqrt-prod17.0%
add-sqr-sqrt17.0%
Applied egg-rr17.0%
expm1-def79.8%
expm1-log1p80.1%
Simplified80.1%
Final simplification66.4%
(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 41.1%
Taylor expanded in re around 0 53.9%
expm1-log1p-u51.2%
expm1-udef43.0%
sqrt-unprod43.0%
Applied egg-rr43.0%
expm1-def51.2%
expm1-log1p54.2%
*-commutative54.2%
Simplified54.2%
Final simplification54.2%
herbie shell --seed 2023240
(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)))))