| Alternative 1 | |
|---|---|
| Accuracy | 74.0% |
| Cost | 7513 |
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
:precision binary64
(if (<= re 3.8e-119)
(* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))
(if (or (<= re 1.02e-54) (not (<= re 2.5e-9)))
(* 0.5 (/ im (sqrt re)))
(* 0.5 (sqrt (* 2.0 im))))))double code(double re, double im) {
return 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re)));
}
double code(double re, double im) {
double tmp;
if (re <= 3.8e-119) {
tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
} else if ((re <= 1.02e-54) || !(re <= 2.5e-9)) {
tmp = 0.5 * (im / sqrt(re));
} else {
tmp = 0.5 * sqrt((2.0 * im));
}
return tmp;
}
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) - re)));
}
public static double code(double re, double im) {
double tmp;
if (re <= 3.8e-119) {
tmp = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
} else if ((re <= 1.02e-54) || !(re <= 2.5e-9)) {
tmp = 0.5 * (im / Math.sqrt(re));
} else {
tmp = 0.5 * Math.sqrt((2.0 * im));
}
return tmp;
}
def code(re, im): return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) - re)))
def code(re, im): tmp = 0 if re <= 3.8e-119: tmp = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re))) elif (re <= 1.02e-54) or not (re <= 2.5e-9): tmp = 0.5 * (im / math.sqrt(re)) else: tmp = 0.5 * math.sqrt((2.0 * im)) return tmp
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) - re)))) end
function code(re, im) tmp = 0.0 if (re <= 3.8e-119) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re)))); elseif ((re <= 1.02e-54) || !(re <= 2.5e-9)) tmp = Float64(0.5 * Float64(im / sqrt(re))); else tmp = Float64(0.5 * sqrt(Float64(2.0 * im))); end return tmp end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) - re))); end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 3.8e-119) tmp = 0.5 * sqrt((2.0 * (hypot(re, im) - re))); elseif ((re <= 1.02e-54) || ~((re <= 2.5e-9))) tmp = 0.5 * (im / sqrt(re)); else tmp = 0.5 * sqrt((2.0 * im)); end tmp_2 = tmp; 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]
code[re_, im_] := If[LessEqual[re, 3.8e-119], N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, 1.02e-54], N[Not[LessEqual[re, 2.5e-9]], $MachinePrecision]], N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Sqrt[N[(2.0 * im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\begin{array}{l}
\mathbf{if}\;re \leq 3.8 \cdot 10^{-119}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{elif}\;re \leq 1.02 \cdot 10^{-54} \lor \neg \left(re \leq 2.5 \cdot 10^{-9}\right):\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot im}\\
\end{array}
Results
if re < 3.79999999999999975e-119Initial program 51.2%
Simplified96.2%
[Start]51.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
|---|---|
hypot-def [=>]96.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)}
\] |
if 3.79999999999999975e-119 < re < 1.01999999999999999e-54 or 2.5000000000000001e-9 < re Initial program 16.3%
Simplified44.7%
[Start]16.3 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
|---|---|
hypot-def [=>]44.7 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)}
\] |
Taylor expanded in im around 0 70.4%
Simplified70.5%
[Start]70.4 | \[ 0.5 \cdot \left(\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot im\right)\right) \cdot \sqrt{\frac{1}{re}}\right)
\] |
|---|---|
associate-*l* [=>]70.4 | \[ 0.5 \cdot \color{blue}{\left(\sqrt{2} \cdot \left(\left(\sqrt{0.5} \cdot im\right) \cdot \sqrt{\frac{1}{re}}\right)\right)}
\] |
*-commutative [=>]70.4 | \[ 0.5 \cdot \left(\sqrt{2} \cdot \left(\color{blue}{\left(im \cdot \sqrt{0.5}\right)} \cdot \sqrt{\frac{1}{re}}\right)\right)
\] |
associate-*l* [=>]70.5 | \[ 0.5 \cdot \left(\sqrt{2} \cdot \color{blue}{\left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)}\right)
\] |
Applied egg-rr70.9%
[Start]70.5 | \[ 0.5 \cdot \left(\sqrt{2} \cdot \left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)\right)
\] |
|---|---|
add-log-exp [=>]14.9 | \[ 0.5 \cdot \color{blue}{\log \left(e^{\sqrt{2} \cdot \left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)}\right)}
\] |
*-un-lft-identity [=>]14.9 | \[ 0.5 \cdot \log \color{blue}{\left(1 \cdot e^{\sqrt{2} \cdot \left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)}\right)}
\] |
log-prod [=>]14.9 | \[ 0.5 \cdot \color{blue}{\left(\log 1 + \log \left(e^{\sqrt{2} \cdot \left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)}\right)\right)}
\] |
metadata-eval [=>]14.9 | \[ 0.5 \cdot \left(\color{blue}{0} + \log \left(e^{\sqrt{2} \cdot \left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)}\right)\right)
\] |
add-log-exp [<=]70.5 | \[ 0.5 \cdot \left(0 + \color{blue}{\sqrt{2} \cdot \left(im \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right)}\right)
\] |
*-commutative [=>]70.5 | \[ 0.5 \cdot \left(0 + \sqrt{2} \cdot \color{blue}{\left(\left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right) \cdot im\right)}\right)
\] |
associate-*r* [=>]70.4 | \[ 0.5 \cdot \left(0 + \color{blue}{\left(\sqrt{2} \cdot \left(\sqrt{0.5} \cdot \sqrt{\frac{1}{re}}\right)\right) \cdot im}\right)
\] |
Simplified71.0%
[Start]70.9 | \[ 0.5 \cdot \left(0 + {\left(2 \cdot \frac{0.5}{re}\right)}^{0.5} \cdot im\right)
\] |
|---|---|
+-lft-identity [=>]70.9 | \[ 0.5 \cdot \color{blue}{\left({\left(2 \cdot \frac{0.5}{re}\right)}^{0.5} \cdot im\right)}
\] |
associate-*r/ [=>]70.9 | \[ 0.5 \cdot \left({\color{blue}{\left(\frac{2 \cdot 0.5}{re}\right)}}^{0.5} \cdot im\right)
\] |
metadata-eval [=>]70.9 | \[ 0.5 \cdot \left({\left(\frac{\color{blue}{1}}{re}\right)}^{0.5} \cdot im\right)
\] |
unpow-1 [<=]70.9 | \[ 0.5 \cdot \left({\color{blue}{\left({re}^{-1}\right)}}^{0.5} \cdot im\right)
\] |
exp-to-pow [<=]66.9 | \[ 0.5 \cdot \left({\color{blue}{\left(e^{\log re \cdot -1}\right)}}^{0.5} \cdot im\right)
\] |
exp-prod [<=]66.9 | \[ 0.5 \cdot \left(\color{blue}{e^{\left(\log re \cdot -1\right) \cdot 0.5}} \cdot im\right)
\] |
*-commutative [=>]66.9 | \[ 0.5 \cdot \left(e^{\color{blue}{\left(-1 \cdot \log re\right)} \cdot 0.5} \cdot im\right)
\] |
neg-mul-1 [<=]66.9 | \[ 0.5 \cdot \left(e^{\color{blue}{\left(-\log re\right)} \cdot 0.5} \cdot im\right)
\] |
distribute-lft-neg-out [=>]66.9 | \[ 0.5 \cdot \left(e^{\color{blue}{-\log re \cdot 0.5}} \cdot im\right)
\] |
exp-neg [=>]66.9 | \[ 0.5 \cdot \left(\color{blue}{\frac{1}{e^{\log re \cdot 0.5}}} \cdot im\right)
\] |
associate-*l/ [=>]66.9 | \[ 0.5 \cdot \color{blue}{\frac{1 \cdot im}{e^{\log re \cdot 0.5}}}
\] |
*-lft-identity [=>]66.9 | \[ 0.5 \cdot \frac{\color{blue}{im}}{e^{\log re \cdot 0.5}}
\] |
exp-to-pow [=>]71.0 | \[ 0.5 \cdot \frac{im}{\color{blue}{{re}^{0.5}}}
\] |
unpow1/2 [=>]71.0 | \[ 0.5 \cdot \frac{im}{\color{blue}{\sqrt{re}}}
\] |
if 1.01999999999999999e-54 < re < 2.5000000000000001e-9Initial program 34.8%
Simplified58.6%
[Start]34.8 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
|---|---|
hypot-def [=>]58.6 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)}
\] |
Taylor expanded in re around 0 57.8%
Final simplification87.0%
| Alternative 1 | |
|---|---|
| Accuracy | 74.0% |
| Cost | 7513 |
| Alternative 2 | |
|---|---|
| Accuracy | 73.7% |
| Cost | 7513 |
| Alternative 3 | |
|---|---|
| Accuracy | 62.9% |
| Cost | 7117 |
| Alternative 4 | |
|---|---|
| Accuracy | 51.4% |
| Cost | 6720 |
herbie shell --seed 2023146
(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)))))