| Alternative 1 | |
|---|---|
| Error | 15.8 |
| Cost | 7249 |
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
:precision binary64
(let* ((t_0 (* 0.5 (sqrt (* 2.0 (- (hypot re im) re))))))
(if (<= re 100000.0)
t_0
(if (<= re 3.2e+19)
(* 0.5 (* im (pow re -0.5)))
(if (<= re 8.5e+54) t_0 (* 0.5 (/ im (sqrt re))))))))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 = 0.5 * sqrt((2.0 * (hypot(re, im) - re)));
double tmp;
if (re <= 100000.0) {
tmp = t_0;
} else if (re <= 3.2e+19) {
tmp = 0.5 * (im * pow(re, -0.5));
} else if (re <= 8.5e+54) {
tmp = t_0;
} else {
tmp = 0.5 * (im / sqrt(re));
}
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 = 0.5 * Math.sqrt((2.0 * (Math.hypot(re, im) - re)));
double tmp;
if (re <= 100000.0) {
tmp = t_0;
} else if (re <= 3.2e+19) {
tmp = 0.5 * (im * Math.pow(re, -0.5));
} else if (re <= 8.5e+54) {
tmp = t_0;
} else {
tmp = 0.5 * (im / Math.sqrt(re));
}
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 = 0.5 * math.sqrt((2.0 * (math.hypot(re, im) - re))) tmp = 0 if re <= 100000.0: tmp = t_0 elif re <= 3.2e+19: tmp = 0.5 * (im * math.pow(re, -0.5)) elif re <= 8.5e+54: tmp = t_0 else: tmp = 0.5 * (im / math.sqrt(re)) 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 = Float64(0.5 * sqrt(Float64(2.0 * Float64(hypot(re, im) - re)))) tmp = 0.0 if (re <= 100000.0) tmp = t_0; elseif (re <= 3.2e+19) tmp = Float64(0.5 * Float64(im * (re ^ -0.5))); elseif (re <= 8.5e+54) tmp = t_0; else tmp = Float64(0.5 * Float64(im / sqrt(re))); 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 = 0.5 * sqrt((2.0 * (hypot(re, im) - re))); tmp = 0.0; if (re <= 100000.0) tmp = t_0; elseif (re <= 3.2e+19) tmp = 0.5 * (im * (re ^ -0.5)); elseif (re <= 8.5e+54) tmp = t_0; else tmp = 0.5 * (im / sqrt(re)); 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[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision] - re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[re, 100000.0], t$95$0, If[LessEqual[re, 3.2e+19], N[(0.5 * N[(im * N[Power[re, -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[re, 8.5e+54], t$95$0, N[(0.5 * N[(im / N[Sqrt[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\begin{array}{l}
t_0 := 0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{if}\;re \leq 100000:\\
\;\;\;\;t_0\\
\mathbf{elif}\;re \leq 3.2 \cdot 10^{+19}:\\
\;\;\;\;0.5 \cdot \left(im \cdot {re}^{-0.5}\right)\\
\mathbf{elif}\;re \leq 8.5 \cdot 10^{+54}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im}{\sqrt{re}}\\
\end{array}
Results
if re < 1e5 or 3.2e19 < re < 8.4999999999999995e54Initial program 32.8
Simplified6.3
[Start]32.8 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
|---|---|
metadata-eval [<=]32.8 | \[ 0.5 \cdot \sqrt{\color{blue}{\left(2 \cdot 1\right)} \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
metadata-eval [<=]32.8 | \[ 0.5 \cdot \sqrt{\left(2 \cdot \color{blue}{\left(--1\right)}\right) \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
associate-*r* [<=]32.8 | \[ 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(\left(--1\right) \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)\right)}}
\] |
metadata-eval [=>]32.8 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{1} \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)\right)}
\] |
*-lft-identity [=>]32.8 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right)}}
\] |
hypot-def [=>]6.3 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)}
\] |
if 1e5 < re < 3.2e19Initial program 48.2
Simplified29.9
[Start]48.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
|---|---|
metadata-eval [<=]48.2 | \[ 0.5 \cdot \sqrt{\color{blue}{\left(2 \cdot 1\right)} \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
metadata-eval [<=]48.2 | \[ 0.5 \cdot \sqrt{\left(2 \cdot \color{blue}{\left(--1\right)}\right) \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
associate-*r* [<=]48.2 | \[ 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(\left(--1\right) \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)\right)}}
\] |
metadata-eval [=>]48.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{1} \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)\right)}
\] |
*-lft-identity [=>]48.2 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right)}}
\] |
hypot-def [=>]29.9 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)}
\] |
Taylor expanded in re around inf 46.8
Simplified46.8
[Start]46.8 | \[ 0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \frac{{im}^{2}}{re}\right)}
\] |
|---|---|
associate-*r/ [=>]46.8 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5 \cdot {im}^{2}}{re}}}
\] |
associate-/l* [=>]46.8 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5}{\frac{re}{{im}^{2}}}}}
\] |
unpow2 [=>]46.8 | \[ 0.5 \cdot \sqrt{2 \cdot \frac{0.5}{\frac{re}{\color{blue}{im \cdot im}}}}
\] |
Applied egg-rr57.8
Simplified29.3
[Start]57.8 | \[ 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)} - 1\right)
\] |
|---|---|
expm1-def [=>]29.3 | \[ 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)\right)}
\] |
expm1-log1p [=>]29.3 | \[ 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}}
\] |
Applied egg-rr29.3
if 8.4999999999999995e54 < re Initial program 58.5
Simplified38.1
[Start]58.5 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
|---|---|
metadata-eval [<=]58.5 | \[ 0.5 \cdot \sqrt{\color{blue}{\left(2 \cdot 1\right)} \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
metadata-eval [<=]58.5 | \[ 0.5 \cdot \sqrt{\left(2 \cdot \color{blue}{\left(--1\right)}\right) \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\] |
associate-*r* [<=]58.5 | \[ 0.5 \cdot \sqrt{\color{blue}{2 \cdot \left(\left(--1\right) \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)\right)}}
\] |
metadata-eval [=>]58.5 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{1} \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)\right)}
\] |
*-lft-identity [=>]58.5 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\sqrt{re \cdot re + im \cdot im} - re\right)}}
\] |
hypot-def [=>]38.1 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\mathsf{hypot}\left(re, im\right)} - re\right)}
\] |
Taylor expanded in re around inf 32.3
Simplified32.8
[Start]32.3 | \[ 0.5 \cdot \sqrt{2 \cdot \left(0.5 \cdot \frac{{im}^{2}}{re}\right)}
\] |
|---|---|
associate-*r/ [=>]32.3 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5 \cdot {im}^{2}}{re}}}
\] |
associate-/l* [=>]32.8 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\frac{0.5}{\frac{re}{{im}^{2}}}}}
\] |
unpow2 [=>]32.8 | \[ 0.5 \cdot \sqrt{2 \cdot \frac{0.5}{\frac{re}{\color{blue}{im \cdot im}}}}
\] |
Applied egg-rr42.9
Simplified13.2
[Start]42.9 | \[ 0.5 \cdot \left(e^{\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)} - 1\right)
\] |
|---|---|
expm1-def [=>]13.7 | \[ 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{im}{\sqrt{re}}\right)\right)}
\] |
expm1-log1p [=>]13.2 | \[ 0.5 \cdot \color{blue}{\frac{im}{\sqrt{re}}}
\] |
Final simplification7.9
| Alternative 1 | |
|---|---|
| Error | 15.8 |
| Cost | 7249 |
| Alternative 2 | |
|---|---|
| Error | 15.8 |
| Cost | 7248 |
| Alternative 3 | |
|---|---|
| Error | 15.2 |
| Cost | 7248 |
| Alternative 4 | |
|---|---|
| Error | 22.9 |
| Cost | 6852 |
| Alternative 5 | |
|---|---|
| Error | 30.6 |
| Cost | 6720 |
herbie shell --seed 2023059
(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)))))