| Alternative 1 | |
|---|---|
| Accuracy | 85.3% |
| Cost | 26884 |
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
:precision binary64
(let* ((t_0 (log (/ -1.0 re)))
(t_1 (* 0.5 (pow (exp (* 0.25 (+ (log (pow im 2.0)) t_0))) 2.0)))
(t_2 (* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))))
(if (<= re -1.65e+263)
t_1
(if (<= re -2.05e+257)
t_2
(if (<= re -2.15e+155)
t_1
(if (<= re -1.85e+136)
(* 0.5 (exp (- (* 0.5 t_0) (log (/ -1.0 im)))))
t_2))))))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 t_0 = log((-1.0 / re));
double t_1 = 0.5 * pow(exp((0.25 * (log(pow(im, 2.0)) + t_0))), 2.0);
double t_2 = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
double tmp;
if (re <= -1.65e+263) {
tmp = t_1;
} else if (re <= -2.05e+257) {
tmp = t_2;
} else if (re <= -2.15e+155) {
tmp = t_1;
} else if (re <= -1.85e+136) {
tmp = 0.5 * exp(((0.5 * t_0) - log((-1.0 / im))));
} else {
tmp = t_2;
}
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 t_0 = Math.log((-1.0 / re));
double t_1 = 0.5 * Math.pow(Math.exp((0.25 * (Math.log(Math.pow(im, 2.0)) + t_0))), 2.0);
double t_2 = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
double tmp;
if (re <= -1.65e+263) {
tmp = t_1;
} else if (re <= -2.05e+257) {
tmp = t_2;
} else if (re <= -2.15e+155) {
tmp = t_1;
} else if (re <= -1.85e+136) {
tmp = 0.5 * Math.exp(((0.5 * t_0) - Math.log((-1.0 / im))));
} else {
tmp = t_2;
}
return tmp;
}
def code(re, im): return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
def code(re, im): t_0 = math.log((-1.0 / re)) t_1 = 0.5 * math.pow(math.exp((0.25 * (math.log(math.pow(im, 2.0)) + t_0))), 2.0) t_2 = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) tmp = 0 if re <= -1.65e+263: tmp = t_1 elif re <= -2.05e+257: tmp = t_2 elif re <= -2.15e+155: tmp = t_1 elif re <= -1.85e+136: tmp = 0.5 * math.exp(((0.5 * t_0) - math.log((-1.0 / im)))) else: tmp = t_2 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) t_0 = log(Float64(-1.0 / re)) t_1 = Float64(0.5 * (exp(Float64(0.25 * Float64(log((im ^ 2.0)) + t_0))) ^ 2.0)) t_2 = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))) tmp = 0.0 if (re <= -1.65e+263) tmp = t_1; elseif (re <= -2.05e+257) tmp = t_2; elseif (re <= -2.15e+155) tmp = t_1; elseif (re <= -1.85e+136) tmp = Float64(0.5 * exp(Float64(Float64(0.5 * t_0) - log(Float64(-1.0 / im))))); else tmp = t_2; 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) t_0 = log((-1.0 / re)); t_1 = 0.5 * (exp((0.25 * (log((im ^ 2.0)) + t_0))) ^ 2.0); t_2 = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); tmp = 0.0; if (re <= -1.65e+263) tmp = t_1; elseif (re <= -2.05e+257) tmp = t_2; elseif (re <= -2.15e+155) tmp = t_1; elseif (re <= -1.85e+136) tmp = 0.5 * exp(((0.5 * t_0) - log((-1.0 / im)))); else tmp = t_2; 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_] := Block[{t$95$0 = N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(0.5 * N[Power[N[Exp[N[(0.25 * N[(N[Log[N[Power[im, 2.0], $MachinePrecision]], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, -1.65e+263], t$95$1, If[LessEqual[re, -2.05e+257], t$95$2, If[LessEqual[re, -2.15e+155], t$95$1, If[LessEqual[re, -1.85e+136], N[(0.5 * N[Exp[N[(N[(0.5 * t$95$0), $MachinePrecision] - N[Log[N[(-1.0 / im), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$2]]]]]]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\begin{array}{l}
t_0 := \log \left(\frac{-1}{re}\right)\\
t_1 := 0.5 \cdot {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + t_0\right)}\right)}^{2}\\
t_2 := 0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\mathbf{if}\;re \leq -1.65 \cdot 10^{+263}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;re \leq -2.05 \cdot 10^{+257}:\\
\;\;\;\;t_2\\
\mathbf{elif}\;re \leq -2.15 \cdot 10^{+155}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;re \leq -1.85 \cdot 10^{+136}:\\
\;\;\;\;0.5 \cdot e^{0.5 \cdot t_0 - \log \left(\frac{-1}{im}\right)}\\
\mathbf{else}:\\
\;\;\;\;t_2\\
\end{array}
Results
| Original | 39.0% |
|---|---|
| Target | 46.9% |
| Herbie | 83.0% |
if re < -1.65e263 or -2.0500000000000001e257 < re < -2.1500000000000001e155Initial program 0.0%
Simplified26.2%
[Start]0.0 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\] |
|---|---|
+-commutative [=>]0.0 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}}
\] |
hypot-def [=>]26.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)}
\] |
Applied egg-rr26.2%
[Start]26.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}
\] |
|---|---|
add-sqr-sqrt [=>]26.2 | \[ 0.5 \cdot \color{blue}{\left(\sqrt{\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \cdot \sqrt{\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}}\right)}
\] |
pow2 [=>]26.2 | \[ 0.5 \cdot \color{blue}{{\left(\sqrt{\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}}\right)}^{2}}
\] |
pow1/2 [=>]26.2 | \[ 0.5 \cdot {\left(\sqrt{\color{blue}{{\left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)}^{0.5}}}\right)}^{2}
\] |
sqrt-pow1 [=>]26.2 | \[ 0.5 \cdot {\color{blue}{\left({\left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)}^{\left(\frac{0.5}{2}\right)}\right)}}^{2}
\] |
*-commutative [=>]26.2 | \[ 0.5 \cdot {\left({\color{blue}{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right)}}^{\left(\frac{0.5}{2}\right)}\right)}^{2}
\] |
metadata-eval [=>]26.2 | \[ 0.5 \cdot {\left({\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right)}^{\color{blue}{0.25}}\right)}^{2}
\] |
Taylor expanded in re around -inf 73.6%
if -1.65e263 < re < -2.0500000000000001e257 or -1.85000000000000005e136 < re Initial program 47.1%
Simplified85.2%
[Start]47.1 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\] |
|---|---|
+-commutative [=>]47.1 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}}
\] |
hypot-def [=>]85.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)}
\] |
if -2.1500000000000001e155 < re < -1.85000000000000005e136Initial program 18.4%
Simplified45.1%
[Start]18.4 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\] |
|---|---|
+-commutative [=>]18.4 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}}
\] |
hypot-def [=>]45.1 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)}
\] |
Taylor expanded in re around -inf 93.4%
Simplified93.4%
[Start]93.4 | \[ 0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}
\] |
|---|---|
*-commutative [=>]93.4 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\frac{{im}^{2}}{re} \cdot -0.5\right)}}
\] |
unpow2 [=>]93.4 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\frac{\color{blue}{im \cdot im}}{re} \cdot -0.5\right)}
\] |
Applied egg-rr90.1%
[Start]93.4 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}
\] |
|---|---|
add-cube-cbrt [=>]92.7 | \[ 0.5 \cdot \color{blue}{\left(\left(\sqrt[3]{\sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}} \cdot \sqrt[3]{\sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}}\right) \cdot \sqrt[3]{\sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}}\right)}
\] |
pow3 [=>]92.7 | \[ 0.5 \cdot \color{blue}{{\left(\sqrt[3]{\sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}}\right)}^{3}}
\] |
metadata-eval [<=]92.7 | \[ 0.5 \cdot {\left(\sqrt[3]{\sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}}\right)}^{\color{blue}{\left(1 + 2\right)}}
\] |
pow-to-exp [=>]90.1 | \[ 0.5 \cdot \color{blue}{e^{\log \left(\sqrt[3]{\sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}}\right) \cdot \left(1 + 2\right)}}
\] |
Taylor expanded in im around -inf 96.6%
Simplified96.6%
[Start]96.6 | \[ 0.5 \cdot e^{0.5 \cdot \left(-2 \cdot \log \left(\frac{-1}{im}\right) + \log \left(\frac{-1}{re}\right)\right)}
\] |
|---|---|
+-commutative [=>]96.6 | \[ 0.5 \cdot e^{0.5 \cdot \color{blue}{\left(\log \left(\frac{-1}{re}\right) + -2 \cdot \log \left(\frac{-1}{im}\right)\right)}}
\] |
distribute-lft-in [=>]96.6 | \[ 0.5 \cdot e^{\color{blue}{0.5 \cdot \log \left(\frac{-1}{re}\right) + 0.5 \cdot \left(-2 \cdot \log \left(\frac{-1}{im}\right)\right)}}
\] |
*-commutative [<=]96.6 | \[ 0.5 \cdot e^{\color{blue}{\log \left(\frac{-1}{re}\right) \cdot 0.5} + 0.5 \cdot \left(-2 \cdot \log \left(\frac{-1}{im}\right)\right)}
\] |
associate-*r* [=>]96.6 | \[ 0.5 \cdot e^{\log \left(\frac{-1}{re}\right) \cdot 0.5 + \color{blue}{\left(0.5 \cdot -2\right) \cdot \log \left(\frac{-1}{im}\right)}}
\] |
metadata-eval [=>]96.6 | \[ 0.5 \cdot e^{\log \left(\frac{-1}{re}\right) \cdot 0.5 + \color{blue}{-1} \cdot \log \left(\frac{-1}{im}\right)}
\] |
mul-1-neg [=>]96.6 | \[ 0.5 \cdot e^{\log \left(\frac{-1}{re}\right) \cdot 0.5 + \color{blue}{\left(-\log \left(\frac{-1}{im}\right)\right)}}
\] |
unsub-neg [=>]96.6 | \[ 0.5 \cdot e^{\color{blue}{\log \left(\frac{-1}{re}\right) \cdot 0.5 - \log \left(\frac{-1}{im}\right)}}
\] |
Final simplification83.9%
| Alternative 1 | |
|---|---|
| Accuracy | 85.3% |
| Cost | 26884 |
| Alternative 2 | |
|---|---|
| Accuracy | 58.3% |
| Cost | 7112 |
| Alternative 3 | |
|---|---|
| Accuracy | 31.5% |
| Cost | 6984 |
| Alternative 4 | |
|---|---|
| Accuracy | 43.9% |
| Cost | 6984 |
| Alternative 5 | |
|---|---|
| Accuracy | 57.9% |
| Cost | 6984 |
| Alternative 6 | |
|---|---|
| Accuracy | 8.8% |
| Cost | 328 |
| Alternative 7 | |
|---|---|
| Accuracy | 8.8% |
| Cost | 328 |
| Alternative 8 | |
|---|---|
| Accuracy | 8.8% |
| Cost | 196 |
| Alternative 9 | |
|---|---|
| Accuracy | 8.8% |
| Cost | 196 |
| Alternative 10 | |
|---|---|
| Accuracy | 8.9% |
| Cost | 196 |
| Alternative 11 | |
|---|---|
| Accuracy | 8.9% |
| Cost | 196 |
| Alternative 12 | |
|---|---|
| Accuracy | 6.1% |
| Cost | 64 |
herbie shell --seed 2023157
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:herbie-target
(if (< re 0.0) (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))