| Alternative 1 | |
|---|---|
| Accuracy | 99.5% |
| Cost | 13380 |

(FPCore (x) :precision binary64 (sqrt (* 2.0 (* x x))))
(FPCore (x) :precision binary64 (if (<= x -2e-310) (* x (- (sqrt 2.0))) (* (pow 2.0 0.125) (* x (pow 8.0 0.125)))))
double code(double x) {
return sqrt((2.0 * (x * x)));
}
double code(double x) {
double tmp;
if (x <= -2e-310) {
tmp = x * -sqrt(2.0);
} else {
tmp = pow(2.0, 0.125) * (x * pow(8.0, 0.125));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt((2.0d0 * (x * x)))
end function
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-2d-310)) then
tmp = x * -sqrt(2.0d0)
else
tmp = (2.0d0 ** 0.125d0) * (x * (8.0d0 ** 0.125d0))
end if
code = tmp
end function
public static double code(double x) {
return Math.sqrt((2.0 * (x * x)));
}
public static double code(double x) {
double tmp;
if (x <= -2e-310) {
tmp = x * -Math.sqrt(2.0);
} else {
tmp = Math.pow(2.0, 0.125) * (x * Math.pow(8.0, 0.125));
}
return tmp;
}
def code(x): return math.sqrt((2.0 * (x * x)))
def code(x): tmp = 0 if x <= -2e-310: tmp = x * -math.sqrt(2.0) else: tmp = math.pow(2.0, 0.125) * (x * math.pow(8.0, 0.125)) return tmp
function code(x) return sqrt(Float64(2.0 * Float64(x * x))) end
function code(x) tmp = 0.0 if (x <= -2e-310) tmp = Float64(x * Float64(-sqrt(2.0))); else tmp = Float64((2.0 ^ 0.125) * Float64(x * (8.0 ^ 0.125))); end return tmp end
function tmp = code(x) tmp = sqrt((2.0 * (x * x))); end
function tmp_2 = code(x) tmp = 0.0; if (x <= -2e-310) tmp = x * -sqrt(2.0); else tmp = (2.0 ^ 0.125) * (x * (8.0 ^ 0.125)); end tmp_2 = tmp; end
code[x_] := N[Sqrt[N[(2.0 * N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[x_] := If[LessEqual[x, -2e-310], N[(x * (-N[Sqrt[2.0], $MachinePrecision])), $MachinePrecision], N[(N[Power[2.0, 0.125], $MachinePrecision] * N[(x * N[Power[8.0, 0.125], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\sqrt{2 \cdot \left(x \cdot x\right)}
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{-310}:\\
\;\;\;\;x \cdot \left(-\sqrt{2}\right)\\
\mathbf{else}:\\
\;\;\;\;{2}^{0.125} \cdot \left(x \cdot {8}^{0.125}\right)\\
\end{array}
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
if x < -1.999999999999994e-310Initial program 50.0%
Taylor expanded in x around -inf 99.4%
Simplified99.4%
[Start]99.4% | \[ -1 \cdot \left(\sqrt{2} \cdot x\right)
\] |
|---|---|
mul-1-neg [=>]99.4% | \[ \color{blue}{-\sqrt{2} \cdot x}
\] |
distribute-rgt-neg-in [=>]99.4% | \[ \color{blue}{\sqrt{2} \cdot \left(-x\right)}
\] |
if -1.999999999999994e-310 < x Initial program 50.6%
Taylor expanded in x around -inf 2.4%
Simplified2.4%
[Start]2.4% | \[ -1 \cdot \left(\sqrt{2} \cdot x\right)
\] |
|---|---|
mul-1-neg [=>]2.4% | \[ \color{blue}{-\sqrt{2} \cdot x}
\] |
distribute-rgt-neg-in [=>]2.4% | \[ \color{blue}{\sqrt{2} \cdot \left(-x\right)}
\] |
Applied egg-rr50.0%
[Start]2.4% | \[ \sqrt{2} \cdot \left(-x\right)
\] |
|---|---|
add-sqr-sqrt [=>]0.0% | \[ \sqrt{2} \cdot \color{blue}{\left(\sqrt{-x} \cdot \sqrt{-x}\right)}
\] |
sqrt-unprod [=>]50.2% | \[ \sqrt{2} \cdot \color{blue}{\sqrt{\left(-x\right) \cdot \left(-x\right)}}
\] |
sqr-neg [=>]50.2% | \[ \sqrt{2} \cdot \sqrt{\color{blue}{x \cdot x}}
\] |
sqrt-unprod [<=]98.9% | \[ \sqrt{2} \cdot \color{blue}{\left(\sqrt{x} \cdot \sqrt{x}\right)}
\] |
add-sqr-sqrt [<=]99.1% | \[ \sqrt{2} \cdot \color{blue}{x}
\] |
*-commutative [=>]99.1% | \[ \color{blue}{x \cdot \sqrt{2}}
\] |
rem-cube-cbrt [<=]97.9% | \[ \color{blue}{{\left(\sqrt[3]{x \cdot \sqrt{2}}\right)}^{3}}
\] |
sqr-pow [=>]97.9% | \[ \color{blue}{{\left(\sqrt[3]{x \cdot \sqrt{2}}\right)}^{\left(\frac{3}{2}\right)} \cdot {\left(\sqrt[3]{x \cdot \sqrt{2}}\right)}^{\left(\frac{3}{2}\right)}}
\] |
pow-prod-down [=>]97.9% | \[ \color{blue}{{\left(\sqrt[3]{x \cdot \sqrt{2}} \cdot \sqrt[3]{x \cdot \sqrt{2}}\right)}^{\left(\frac{3}{2}\right)}}
\] |
cbrt-unprod [=>]49.9% | \[ {\color{blue}{\left(\sqrt[3]{\left(x \cdot \sqrt{2}\right) \cdot \left(x \cdot \sqrt{2}\right)}\right)}}^{\left(\frac{3}{2}\right)}
\] |
*-commutative [<=]49.9% | \[ {\left(\sqrt[3]{\color{blue}{\left(\sqrt{2} \cdot x\right)} \cdot \left(x \cdot \sqrt{2}\right)}\right)}^{\left(\frac{3}{2}\right)}
\] |
*-commutative [<=]49.9% | \[ {\left(\sqrt[3]{\left(\sqrt{2} \cdot x\right) \cdot \color{blue}{\left(\sqrt{2} \cdot x\right)}}\right)}^{\left(\frac{3}{2}\right)}
\] |
swap-sqr [=>]49.9% | \[ {\left(\sqrt[3]{\color{blue}{\left(\sqrt{2} \cdot \sqrt{2}\right) \cdot \left(x \cdot x\right)}}\right)}^{\left(\frac{3}{2}\right)}
\] |
add-sqr-sqrt [<=]50.0% | \[ {\left(\sqrt[3]{\color{blue}{2} \cdot \left(x \cdot x\right)}\right)}^{\left(\frac{3}{2}\right)}
\] |
metadata-eval [=>]50.0% | \[ {\left(\sqrt[3]{2 \cdot \left(x \cdot x\right)}\right)}^{\color{blue}{1.5}}
\] |
Applied egg-rr99.4%
[Start]50.0% | \[ {\left(\sqrt[3]{2 \cdot \left(x \cdot x\right)}\right)}^{1.5}
\] |
|---|---|
pow1/3 [=>]46.6% | \[ {\color{blue}{\left({\left(2 \cdot \left(x \cdot x\right)\right)}^{0.3333333333333333}\right)}}^{1.5}
\] |
pow-pow [=>]50.6% | \[ \color{blue}{{\left(2 \cdot \left(x \cdot x\right)\right)}^{\left(0.3333333333333333 \cdot 1.5\right)}}
\] |
metadata-eval [=>]50.6% | \[ {\left(2 \cdot \left(x \cdot x\right)\right)}^{\color{blue}{0.5}}
\] |
pow1/2 [<=]50.6% | \[ \color{blue}{\sqrt{2 \cdot \left(x \cdot x\right)}}
\] |
sqrt-prod [=>]50.2% | \[ \color{blue}{\sqrt{2} \cdot \sqrt{x \cdot x}}
\] |
sqrt-prod [=>]98.9% | \[ \sqrt{2} \cdot \color{blue}{\left(\sqrt{x} \cdot \sqrt{x}\right)}
\] |
add-sqr-sqrt [<=]99.1% | \[ \sqrt{2} \cdot \color{blue}{x}
\] |
pow1/2 [=>]99.1% | \[ \color{blue}{{2}^{0.5}} \cdot x
\] |
metadata-eval [<=]99.1% | \[ {2}^{\color{blue}{\left(0.25 + 0.25\right)}} \cdot x
\] |
pow-prod-up [<=]99.2% | \[ \color{blue}{\left({2}^{0.25} \cdot {2}^{0.25}\right)} \cdot x
\] |
associate-*r* [<=]99.4% | \[ \color{blue}{{2}^{0.25} \cdot \left({2}^{0.25} \cdot x\right)}
\] |
add-sqr-sqrt [=>]99.4% | \[ \color{blue}{\left(\sqrt{{2}^{0.25}} \cdot \sqrt{{2}^{0.25}}\right)} \cdot \left({2}^{0.25} \cdot x\right)
\] |
pow2 [=>]99.4% | \[ \color{blue}{{\left(\sqrt{{2}^{0.25}}\right)}^{2}} \cdot \left({2}^{0.25} \cdot x\right)
\] |
sqrt-pow1 [=>]99.4% | \[ {\color{blue}{\left({2}^{\left(\frac{0.25}{2}\right)}\right)}}^{2} \cdot \left({2}^{0.25} \cdot x\right)
\] |
sqrt-pow2 [<=]99.4% | \[ {\color{blue}{\left({\left(\sqrt{2}\right)}^{0.25}\right)}}^{2} \cdot \left({2}^{0.25} \cdot x\right)
\] |
pow2 [<=]99.4% | \[ \color{blue}{\left({\left(\sqrt{2}\right)}^{0.25} \cdot {\left(\sqrt{2}\right)}^{0.25}\right)} \cdot \left({2}^{0.25} \cdot x\right)
\] |
associate-*l* [=>]99.4% | \[ \color{blue}{{\left(\sqrt{2}\right)}^{0.25} \cdot \left({\left(\sqrt{2}\right)}^{0.25} \cdot \left({2}^{0.25} \cdot x\right)\right)}
\] |
sqrt-pow2 [=>]99.4% | \[ \color{blue}{{2}^{\left(\frac{0.25}{2}\right)}} \cdot \left({\left(\sqrt{2}\right)}^{0.25} \cdot \left({2}^{0.25} \cdot x\right)\right)
\] |
metadata-eval [=>]99.4% | \[ {2}^{\color{blue}{0.125}} \cdot \left({\left(\sqrt{2}\right)}^{0.25} \cdot \left({2}^{0.25} \cdot x\right)\right)
\] |
sqrt-pow2 [=>]99.4% | \[ {2}^{0.125} \cdot \left(\color{blue}{{2}^{\left(\frac{0.25}{2}\right)}} \cdot \left({2}^{0.25} \cdot x\right)\right)
\] |
metadata-eval [=>]99.4% | \[ {2}^{0.125} \cdot \left({2}^{\color{blue}{0.125}} \cdot \left({2}^{0.25} \cdot x\right)\right)
\] |
Taylor expanded in x around 0 99.7%
Final simplification99.5%
| Alternative 1 | |
|---|---|
| Accuracy | 99.5% |
| Cost | 13380 |
| Alternative 2 | |
|---|---|
| Accuracy | 97.9% |
| Cost | 19584 |
| Alternative 3 | |
|---|---|
| Accuracy | 99.4% |
| Cost | 13252 |
| Alternative 4 | |
|---|---|
| Accuracy | 99.3% |
| Cost | 6788 |
| Alternative 5 | |
|---|---|
| Accuracy | 50.5% |
| Cost | 6592 |
herbie shell --seed 2023256
(FPCore (x)
:name "sqrt C (should all be same)"
:precision binary64
(sqrt (* 2.0 (* x x))))