
(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 3e-38) (sqrt (* 0.5 (- (hypot re im) re))) (* 0.5 (* im (pow re -0.5)))))
double code(double re, double im) {
double tmp;
if (re <= 3e-38) {
tmp = sqrt((0.5 * (hypot(re, im) - re)));
} else {
tmp = 0.5 * (im * pow(re, -0.5));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (re <= 3e-38) {
tmp = Math.sqrt((0.5 * (Math.hypot(re, im) - re)));
} else {
tmp = 0.5 * (im * Math.pow(re, -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 3e-38: tmp = math.sqrt((0.5 * (math.hypot(re, im) - re))) else: tmp = 0.5 * (im * math.pow(re, -0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= 3e-38) tmp = sqrt(Float64(0.5 * Float64(hypot(re, im) - re))); else tmp = Float64(0.5 * Float64(im * (re ^ -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 3e-38) tmp = sqrt((0.5 * (hypot(re, im) - re))); else tmp = 0.5 * (im * (re ^ -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 3e-38], N[Sqrt[N[(0.5 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 3 \cdot 10^{-38}:\\
\;\;\;\;\sqrt{0.5 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\
\end{array}
\end{array}
if re < 2.99999999999999989e-38Initial program 47.6%
*-commutative47.6%
hypot-udef97.3%
*-commutative97.3%
add-sqr-sqrt96.5%
sqrt-unprod97.3%
*-commutative97.3%
*-commutative97.3%
swap-sqr97.3%
add-sqr-sqrt97.3%
metadata-eval97.3%
Applied egg-rr97.3%
*-commutative97.3%
associate-*r*97.3%
metadata-eval97.3%
Simplified97.3%
if 2.99999999999999989e-38 < re Initial program 14.3%
sqrt-prod14.3%
hypot-udef35.5%
sqrt-prod35.7%
add-cbrt-cube21.8%
add-sqr-sqrt21.8%
pow121.8%
pow1/221.8%
pow-prod-up21.8%
metadata-eval21.8%
Applied egg-rr21.8%
Taylor expanded in im around 0 77.4%
metadata-eval77.4%
associate-*r/77.4%
*-commutative77.4%
associate-*r/77.4%
metadata-eval77.4%
rem-exp-log73.0%
exp-neg73.0%
unpow1/273.0%
exp-prod72.9%
distribute-lft-neg-out72.9%
distribute-rgt-neg-in72.9%
metadata-eval72.9%
exp-to-pow77.4%
Simplified77.4%
Final simplification90.8%
(FPCore (re im)
:precision binary64
(if (<= re -1.45e-12)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 2.3e-74)
(* 0.5 (sqrt (* 2.0 (- im re))))
(* 0.5 (* im (pow re -0.5))))))
double code(double re, double im) {
double tmp;
if (re <= -1.45e-12) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 2.3e-74) {
tmp = 0.5 * sqrt((2.0 * (im - re)));
} else {
tmp = 0.5 * (im * pow(re, -0.5));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= (-1.45d-12)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 2.3d-74) then
tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
else
tmp = 0.5d0 * (im * (re ** (-0.5d0)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -1.45e-12) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 2.3e-74) {
tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
} else {
tmp = 0.5 * (im * Math.pow(re, -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -1.45e-12: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 2.3e-74: tmp = 0.5 * math.sqrt((2.0 * (im - re))) else: tmp = 0.5 * (im * math.pow(re, -0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -1.45e-12) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 2.3e-74) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re)))); else tmp = Float64(0.5 * Float64(im * (re ^ -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -1.45e-12) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 2.3e-74) tmp = 0.5 * sqrt((2.0 * (im - re))); else tmp = 0.5 * (im * (re ^ -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -1.45e-12], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.3e-74], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -1.45 \cdot 10^{-12}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 2.3 \cdot 10^{-74}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\
\end{array}
\end{array}
if re < -1.4500000000000001e-12Initial program 36.9%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 74.5%
*-commutative74.5%
Simplified74.5%
if -1.4500000000000001e-12 < re < 2.2999999999999998e-74Initial program 57.7%
Taylor expanded in re around 0 86.8%
if 2.2999999999999998e-74 < re Initial program 15.0%
sqrt-prod15.0%
hypot-udef36.3%
sqrt-prod36.5%
add-cbrt-cube23.2%
add-sqr-sqrt23.2%
pow123.2%
pow1/223.2%
pow-prod-up23.2%
metadata-eval23.2%
Applied egg-rr23.2%
Taylor expanded in im around 0 76.2%
metadata-eval76.2%
associate-*r/76.2%
*-commutative76.2%
associate-*r/76.2%
metadata-eval76.2%
rem-exp-log71.9%
exp-neg71.9%
unpow1/271.9%
exp-prod71.8%
distribute-lft-neg-out71.8%
distribute-rgt-neg-in71.8%
metadata-eval71.8%
exp-to-pow76.2%
Simplified76.2%
Final simplification79.4%
(FPCore (re im)
:precision binary64
(if (<= re -4.8e-15)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 1.9e-72)
(* 0.5 (sqrt (* 2.0 (- im re))))
(* 0.5 (* im (sqrt (/ 1.0 re)))))))
double code(double re, double im) {
double tmp;
if (re <= -4.8e-15) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 1.9e-72) {
tmp = 0.5 * sqrt((2.0 * (im - re)));
} else {
tmp = 0.5 * (im * sqrt((1.0 / 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.8d-15)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 1.9d-72) then
tmp = 0.5d0 * sqrt((2.0d0 * (im - re)))
else
tmp = 0.5d0 * (im * sqrt((1.0d0 / re)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -4.8e-15) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 1.9e-72) {
tmp = 0.5 * Math.sqrt((2.0 * (im - re)));
} else {
tmp = 0.5 * (im * Math.sqrt((1.0 / re)));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -4.8e-15: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 1.9e-72: tmp = 0.5 * math.sqrt((2.0 * (im - re))) else: tmp = 0.5 * (im * math.sqrt((1.0 / re))) return tmp
function code(re, im) tmp = 0.0 if (re <= -4.8e-15) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 1.9e-72) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(im - re)))); else tmp = Float64(0.5 * Float64(im * sqrt(Float64(1.0 / re)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -4.8e-15) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 1.9e-72) tmp = 0.5 * sqrt((2.0 * (im - re))); else tmp = 0.5 * (im * sqrt((1.0 / re))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -4.8e-15], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 1.9e-72], N[(0.5 * N[Sqrt[N[(2.0 * N[(im - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Sqrt[N[(1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -4.8 \cdot 10^{-15}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 1.9 \cdot 10^{-72}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot \sqrt{\frac{1}{re}}\right)\\
\end{array}
\end{array}
if re < -4.7999999999999999e-15Initial program 36.9%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 74.5%
*-commutative74.5%
Simplified74.5%
if -4.7999999999999999e-15 < re < 1.90000000000000001e-72Initial program 57.7%
Taylor expanded in re around 0 86.8%
if 1.90000000000000001e-72 < re Initial program 15.0%
sqrt-prod15.0%
hypot-udef36.3%
sqrt-prod36.5%
add-cbrt-cube23.2%
add-sqr-sqrt23.2%
pow123.2%
pow1/223.2%
pow-prod-up23.2%
metadata-eval23.2%
Applied egg-rr23.2%
Taylor expanded in im around 0 76.2%
Final simplification79.4%
(FPCore (re im)
:precision binary64
(if (<= re -3.1e-15)
(* 0.5 (sqrt (* re -4.0)))
(if (<= re 2.1e-35)
(* 0.5 (sqrt (* im 2.0)))
(* 0.5 (* im (pow re -0.5))))))
double code(double re, double im) {
double tmp;
if (re <= -3.1e-15) {
tmp = 0.5 * sqrt((re * -4.0));
} else if (re <= 2.1e-35) {
tmp = 0.5 * sqrt((im * 2.0));
} else {
tmp = 0.5 * (im * pow(re, -0.5));
}
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-15)) then
tmp = 0.5d0 * sqrt((re * (-4.0d0)))
else if (re <= 2.1d-35) then
tmp = 0.5d0 * sqrt((im * 2.0d0))
else
tmp = 0.5d0 * (im * (re ** (-0.5d0)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= -3.1e-15) {
tmp = 0.5 * Math.sqrt((re * -4.0));
} else if (re <= 2.1e-35) {
tmp = 0.5 * Math.sqrt((im * 2.0));
} else {
tmp = 0.5 * (im * Math.pow(re, -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= -3.1e-15: tmp = 0.5 * math.sqrt((re * -4.0)) elif re <= 2.1e-35: tmp = 0.5 * math.sqrt((im * 2.0)) else: tmp = 0.5 * (im * math.pow(re, -0.5)) return tmp
function code(re, im) tmp = 0.0 if (re <= -3.1e-15) tmp = Float64(0.5 * sqrt(Float64(re * -4.0))); elseif (re <= 2.1e-35) tmp = Float64(0.5 * sqrt(Float64(im * 2.0))); else tmp = Float64(0.5 * Float64(im * (re ^ -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -3.1e-15) tmp = 0.5 * sqrt((re * -4.0)); elseif (re <= 2.1e-35) tmp = 0.5 * sqrt((im * 2.0)); else tmp = 0.5 * (im * (re ^ -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, -3.1e-15], N[(0.5 * N[Sqrt[N[(re * -4.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 2.1e-35], N[(0.5 * N[Sqrt[N[(im * 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq -3.1 \cdot 10^{-15}:\\
\;\;\;\;0.5 \cdot \sqrt{re \cdot -4}\\
\mathbf{elif}\;re \leq 2.1 \cdot 10^{-35}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\
\end{array}
\end{array}
if re < -3.0999999999999999e-15Initial program 36.9%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 74.5%
*-commutative74.5%
Simplified74.5%
if -3.0999999999999999e-15 < re < 2.1e-35Initial program 56.5%
hypot-def95.0%
Simplified95.0%
Taylor expanded in re around 0 83.6%
*-commutative83.6%
Simplified83.6%
if 2.1e-35 < re Initial program 14.3%
sqrt-prod14.3%
hypot-udef35.5%
sqrt-prod35.7%
add-cbrt-cube21.8%
add-sqr-sqrt21.8%
pow121.8%
pow1/221.8%
pow-prod-up21.8%
metadata-eval21.8%
Applied egg-rr21.8%
Taylor expanded in im around 0 77.4%
metadata-eval77.4%
associate-*r/77.4%
*-commutative77.4%
associate-*r/77.4%
metadata-eval77.4%
rem-exp-log73.0%
exp-neg73.0%
unpow1/273.0%
exp-prod72.9%
distribute-lft-neg-out72.9%
distribute-rgt-neg-in72.9%
metadata-eval72.9%
exp-to-pow77.4%
Simplified77.4%
Final simplification78.8%
(FPCore (re im) :precision binary64 (if (<= re -1.3e-17) (* 0.5 (sqrt (* re -4.0))) (* 0.5 (sqrt (* im 2.0)))))
double code(double re, double im) {
double tmp;
if (re <= -1.3e-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 <= (-1.3d-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 <= -1.3e-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 <= -1.3e-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 <= -1.3e-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 <= -1.3e-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, -1.3e-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 -1.3 \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 < -1.30000000000000002e-17Initial program 36.9%
hypot-def100.0%
Simplified100.0%
Taylor expanded in re around -inf 74.5%
*-commutative74.5%
Simplified74.5%
if -1.30000000000000002e-17 < re Initial program 36.7%
hypot-def67.2%
Simplified67.2%
Taylor expanded in re around 0 56.9%
*-commutative56.9%
Simplified56.9%
Final simplification62.3%
(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 36.8%
hypot-def77.3%
Simplified77.3%
Taylor expanded in re around 0 48.1%
*-commutative48.1%
Simplified48.1%
Final simplification48.1%
herbie shell --seed 2023182
(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)))))