1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | module.exports = function rightArithShift (x, y) {
|
22 | if ((x.isFinite() && !x.isInteger()) || (y.isFinite() && !y.isInteger())) {
|
23 | throw new Error('Integers expected in function rightArithShift')
|
24 | }
|
25 |
|
26 | const BigNumber = x.constructor
|
27 | if (x.isNaN() || y.isNaN() || (y.isNegative() && !y.isZero())) {
|
28 | return new BigNumber(NaN)
|
29 | }
|
30 | if (x.isZero() || y.isZero()) {
|
31 | return x
|
32 | }
|
33 | if (!y.isFinite()) {
|
34 | if (x.isNegative()) {
|
35 | return new BigNumber(-1)
|
36 | }
|
37 | if (!x.isFinite()) {
|
38 | return new BigNumber(NaN)
|
39 | }
|
40 | return new BigNumber(0)
|
41 | }
|
42 |
|
43 |
|
44 | if (y.lt(55)) {
|
45 | return x.div(Math.pow(2, y.toNumber()) + '').floor()
|
46 | }
|
47 | return x.div(new BigNumber(2).pow(y)).floor()
|
48 | }
|