
(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 0.35) (* 0.5 (sqrt (* 2.0 (- (hypot re im) re)))) (* 0.5 (/ im (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= 0.35) {
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 <= 0.35) {
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 <= 0.35: 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 <= 0.35) 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 <= 0.35) 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, 0.35], 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 0.35:\\
\;\;\;\;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 < 0.34999999999999998Initial program 59.0%
hypot-def94.9%
Simplified94.9%
if 0.34999999999999998 < re Initial program 10.9%
Taylor expanded in re around inf 65.2%
unpow265.2%
Simplified65.2%
Taylor expanded in im around 0 85.2%
*-commutative85.2%
rem-exp-log80.2%
unpow1/280.2%
rem-exp-log79.5%
exp-neg79.5%
exp-prod79.5%
distribute-lft-neg-out79.5%
distribute-rgt-neg-in79.5%
metadata-eval79.5%
*-commutative79.5%
log-pow79.5%
exp-to-pow79.5%
metadata-eval79.5%
distribute-rgt-neg-in79.5%
exp-neg79.5%
log-rec79.5%
exp-to-pow79.5%
unpow1/279.5%
exp-sum79.2%
sub-neg79.2%
log-div80.0%
Simplified85.3%
Final simplification92.5%
(FPCore (re im)
:precision binary64
(if (<= re -9.4e-30)
(* 0.5 (sqrt (* 2.0 (* re -2.0))))
(if (<= re 12.0)
(* 0.5 (sqrt (* 2.0 (- im re))))
(* 0.5 (/ im (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -9.4e-30) {
tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
} else if (re <= 12.0) {
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 <= (-9.4d-30)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
else if (re <= 12.0d0) 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 <= -9.4e-30) {
tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
} else if (re <= 12.0) {
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 <= -9.4e-30: tmp = 0.5 * math.sqrt((2.0 * (re * -2.0))) elif re <= 12.0: 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 <= -9.4e-30) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))); elseif (re <= 12.0) 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 <= -9.4e-30) tmp = 0.5 * sqrt((2.0 * (re * -2.0))); elseif (re <= 12.0) 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, -9.4e-30], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 12.0], 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 -9.4 \cdot 10^{-30}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{elif}\;re \leq 12:\\
\;\;\;\;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 < -9.39999999999999938e-30Initial program 48.5%
Taylor expanded in re around -inf 77.6%
*-commutative77.6%
Simplified77.6%
if -9.39999999999999938e-30 < re < 12Initial program 64.4%
Taylor expanded in re around 0 83.8%
if 12 < re Initial program 10.9%
Taylor expanded in re around inf 65.2%
unpow265.2%
Simplified65.2%
Taylor expanded in im around 0 85.2%
*-commutative85.2%
rem-exp-log80.2%
unpow1/280.2%
rem-exp-log79.5%
exp-neg79.5%
exp-prod79.5%
distribute-lft-neg-out79.5%
distribute-rgt-neg-in79.5%
metadata-eval79.5%
*-commutative79.5%
log-pow79.5%
exp-to-pow79.5%
metadata-eval79.5%
distribute-rgt-neg-in79.5%
exp-neg79.5%
log-rec79.5%
exp-to-pow79.5%
unpow1/279.5%
exp-sum79.2%
sub-neg79.2%
log-div80.0%
Simplified85.3%
Final simplification82.6%
(FPCore (re im)
:precision binary64
(if (<= re -7.5e-30)
(* 0.5 (sqrt (* 2.0 (* re -2.0))))
(if (<= re 27000000000000.0)
(* 0.5 (sqrt (* 2.0 im)))
(* 0.5 (/ im (sqrt re))))))
double code(double re, double im) {
double tmp;
if (re <= -7.5e-30) {
tmp = 0.5 * sqrt((2.0 * (re * -2.0)));
} else if (re <= 27000000000000.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 <= (-7.5d-30)) then
tmp = 0.5d0 * sqrt((2.0d0 * (re * (-2.0d0))))
else if (re <= 27000000000000.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 <= -7.5e-30) {
tmp = 0.5 * Math.sqrt((2.0 * (re * -2.0)));
} else if (re <= 27000000000000.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 <= -7.5e-30: tmp = 0.5 * math.sqrt((2.0 * (re * -2.0))) elif re <= 27000000000000.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 <= -7.5e-30) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re * -2.0)))); elseif (re <= 27000000000000.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 <= -7.5e-30) tmp = 0.5 * sqrt((2.0 * (re * -2.0))); elseif (re <= 27000000000000.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, -7.5e-30], N[(0.5 * N[Sqrt[N[(2.0 * N[(re * -2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 27000000000000.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 -7.5 \cdot 10^{-30}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{elif}\;re \leq 27000000000000:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < -7.5000000000000006e-30Initial program 48.5%
Taylor expanded in re around -inf 77.6%
*-commutative77.6%
Simplified77.6%
if -7.5000000000000006e-30 < re < 2.7e13Initial program 63.5%
hypot-udef91.7%
add-sqr-sqrt91.4%
pow291.4%
Applied egg-rr91.4%
unpow291.4%
add-sqr-sqrt91.7%
add-sqr-sqrt44.0%
add-sqr-sqrt44.0%
sqr-neg44.0%
sqrt-unprod0.0%
add-sqr-sqrt42.9%
distribute-lft-neg-in42.9%
add-sqr-sqrt82.5%
*-un-lft-identity82.5%
*-commutative82.5%
neg-mul-182.5%
prod-diff82.5%
add-sqr-sqrt42.9%
distribute-rgt-neg-in42.9%
add-sqr-sqrt0.0%
sqrt-unprod44.0%
sqr-neg44.0%
add-sqr-sqrt44.0%
add-sqr-sqrt91.7%
Applied egg-rr91.7%
fma-udef91.7%
distribute-rgt-neg-in91.7%
metadata-eval91.7%
distribute-rgt-in91.7%
+-commutative91.7%
*-lft-identity91.7%
*-lft-identity91.7%
+-commutative91.7%
distribute-rgt-in91.7%
*-rgt-identity91.7%
*-commutative91.7%
mul-1-neg91.7%
Simplified91.7%
Taylor expanded in re around 0 83.0%
if 2.7e13 < re Initial program 11.1%
Taylor expanded in re around inf 65.6%
unpow265.6%
Simplified65.6%
Taylor expanded in im around 0 86.3%
*-commutative86.3%
rem-exp-log81.1%
unpow1/281.1%
rem-exp-log80.4%
exp-neg80.4%
exp-prod80.4%
distribute-lft-neg-out80.4%
distribute-rgt-neg-in80.4%
metadata-eval80.4%
*-commutative80.4%
log-pow80.4%
exp-to-pow80.4%
metadata-eval80.4%
distribute-rgt-neg-in80.4%
exp-neg80.4%
log-rec80.4%
exp-to-pow80.4%
unpow1/280.4%
exp-sum80.2%
sub-neg80.2%
log-div81.0%
Simplified86.4%
Final simplification82.4%
(FPCore (re im) :precision binary64 (if (<= re 30000000000000.0) (* 0.5 (sqrt (* 2.0 im))) (* 0.5 (/ im (sqrt re)))))
double code(double re, double im) {
double tmp;
if (re <= 30000000000000.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 <= 30000000000000.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 <= 30000000000000.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 <= 30000000000000.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 <= 30000000000000.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 <= 30000000000000.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, 30000000000000.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 30000000000000:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\end{array}
\end{array}
if re < 3e13Initial program 58.4%
hypot-udef94.5%
add-sqr-sqrt94.3%
pow294.3%
Applied egg-rr94.3%
unpow294.3%
add-sqr-sqrt94.5%
add-sqr-sqrt29.2%
add-sqr-sqrt29.2%
sqr-neg29.2%
sqrt-unprod0.0%
add-sqr-sqrt28.5%
distribute-lft-neg-in28.5%
add-sqr-sqrt62.9%
*-un-lft-identity62.9%
*-commutative62.9%
neg-mul-162.9%
prod-diff62.9%
add-sqr-sqrt28.5%
distribute-rgt-neg-in28.5%
add-sqr-sqrt0.0%
sqrt-unprod29.1%
sqr-neg29.1%
add-sqr-sqrt29.1%
add-sqr-sqrt94.5%
Applied egg-rr94.5%
fma-udef94.5%
distribute-rgt-neg-in94.5%
metadata-eval94.5%
distribute-rgt-in94.5%
+-commutative94.5%
*-lft-identity94.5%
*-lft-identity94.5%
+-commutative94.5%
distribute-rgt-in94.5%
*-rgt-identity94.5%
*-commutative94.5%
mul-1-neg94.5%
Simplified94.5%
Taylor expanded in re around 0 64.1%
if 3e13 < re Initial program 11.1%
Taylor expanded in re around inf 65.6%
unpow265.6%
Simplified65.6%
Taylor expanded in im around 0 86.3%
*-commutative86.3%
rem-exp-log81.1%
unpow1/281.1%
rem-exp-log80.4%
exp-neg80.4%
exp-prod80.4%
distribute-lft-neg-out80.4%
distribute-rgt-neg-in80.4%
metadata-eval80.4%
*-commutative80.4%
log-pow80.4%
exp-to-pow80.4%
metadata-eval80.4%
distribute-rgt-neg-in80.4%
exp-neg80.4%
log-rec80.4%
exp-to-pow80.4%
unpow1/280.4%
exp-sum80.2%
sub-neg80.2%
log-div81.0%
Simplified86.4%
Final simplification69.5%
(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 46.8%
hypot-udef78.9%
add-sqr-sqrt76.3%
pow276.3%
Applied egg-rr76.3%
unpow276.3%
add-sqr-sqrt78.9%
add-sqr-sqrt27.2%
add-sqr-sqrt27.2%
sqr-neg27.2%
sqrt-unprod0.0%
add-sqr-sqrt25.5%
distribute-lft-neg-in25.5%
add-sqr-sqrt51.4%
*-un-lft-identity51.4%
*-commutative51.4%
neg-mul-151.4%
prod-diff51.4%
add-sqr-sqrt25.5%
distribute-rgt-neg-in25.5%
add-sqr-sqrt0.0%
sqrt-unprod28.7%
sqr-neg28.7%
add-sqr-sqrt28.7%
add-sqr-sqrt78.9%
Applied egg-rr78.9%
fma-udef78.9%
distribute-rgt-neg-in78.9%
metadata-eval78.9%
distribute-rgt-in78.9%
+-commutative78.9%
*-lft-identity78.9%
*-lft-identity78.9%
+-commutative78.9%
distribute-rgt-in78.9%
*-rgt-identity78.9%
*-commutative78.9%
mul-1-neg78.9%
Simplified78.9%
Taylor expanded in re around 0 52.7%
Final simplification52.7%
herbie shell --seed 2023189
(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)))))