Skip to content

Commit 6ac00cd

Browse files
📚 docs(manual): Add usage instructions.
1 parent 8799f1d commit 6ac00cd

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

doc/manual/usage.md

+139
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Usage
2+
23
The code needs a ES2015+ polyfill to work, for example
34
[@babel/polyfill](https://babeljs.io/docs/usage/polyfill).
45
```js
@@ -13,3 +14,141 @@ const integer = require( '@aureooms/js-integer' ) ;
1314
// or
1415
import * as integer from '@aureooms/js-integer' ;
1516
```
17+
18+
## Notation
19+
20+
The Notation tries to emulate the notation in [bn.js](https://github.com/indutny/bn.js).
21+
22+
## Instructions
23+
24+
Prefixes/postfixes are put in parens at the of the line.
25+
26+
27+
### Initialization
28+
29+
* [x] `ZZ.from(object, base = undefined, is_negative = 0)` (Base is guessed depending on object type if `undefined`, default is 10.)
30+
31+
### Copying
32+
33+
* [x] `a.clone()` - clone number
34+
* [x] `a.move(b)` - copy `a`'s' properties to `b`
35+
36+
> Note that we plan to be pure in the future.
37+
38+
39+
### Comparison
40+
41+
* [x] `a.cmp(b)` - compare numbers and return `-1` (a `<` b), `0` (a `=` b), or `1` (a `>` b) depending on the comparison result (`cmpn`)
42+
* [x] `a.lt(b)` - `a` less than `b` (`n`)
43+
* [x] `a.le(b)` - `a` less than or equals `b` (`n`)
44+
* [x] `a.gt(b)` - `a` greater than `b` (`n`)
45+
* [x] `a.ge(b)` - `a` greater than or equals `b` (`n`)
46+
* [x] `a.eq(b)` - `a` equals `b` (`n`)
47+
48+
49+
### Integer Arithmetic
50+
51+
* [x] `a.negate()` - negate sign (`ineg` in bn.js)
52+
* [x] `a.opposite()` - negate sign (`neg` in bn.js)
53+
* [x] `a.abs()` - absolute value (`i`)
54+
55+
* [x] `a.add(b)` - addition (`i`, `n`, `in`)
56+
* [x] `a.sub(b)` - subtraction (`i`, `n`, `in`)
57+
* [x] `a.mul(b)` - multiply (`i`, `n`, `in`)
58+
* [x] `a.square()` - square (`i`, `sqr` in bn.js)
59+
* [x] `a.pow(b)` - raise `a` to the power of `b` (`i`, `n`, `in`)
60+
61+
* [x] `[q,r] = a.divmod(b)` - divide (`i`, `n`)
62+
* [x] `q = a.div(b)` - division quotient (`u`, `n`)
63+
* [x] `r = a.mod(b)` - division remained (`u`, `n`) (but no `umodn`)
64+
* [x] `q = a.divround(b)` - rounded division
65+
66+
> In the future, remove in-place operations. They make very little sense except
67+
> in a few exceptional cases like increment/decrement. If the result is too big
68+
> to fit in the original array we will have to resize it to make it fit anyway.
69+
> Maybe little endianess would save the day in that case. Who knows...
70+
> Could use an immutable flag that is set as soon as a shallow copy is made?
71+
> Then all operations could try to run in-place if no shallow copy exists.
72+
73+
### Greatest Common Divisor
74+
75+
* [x] `a.gcd(b)` - GCD
76+
* [x] `a.egcd(b)` - Extended GCD results (`{ gcd: ..., x: ..., y: ..., u: ..., v: ... }`)
77+
78+
79+
### Modular Arithmetic
80+
81+
* [ ] `ZZ(n)` - integers modulo `n`
82+
* [ ] `ZZ(n).get(3)` - `returns the equivalence class [3]_n`
83+
* [ ] `a.add(b)` - no comment
84+
* [ ] `a.sub(b)` - no comment
85+
* [ ] `a.mul(b)` - no comment
86+
* [ ] `a.inv()` - inverse `a` modulo `n`
87+
* [ ] `a.square()` - no comment
88+
* [ ] `a.pow(b)` - no comment
89+
90+
91+
### Bit operations
92+
93+
We should really have two packages:
94+
95+
- One for immutable, keyable, hashable integers.
96+
- One for safe in-place word array manipulation.
97+
98+
There are multiple reasons for this:
99+
100+
- Some bit operations do not really care about endianess
101+
- It only works efficiently with a radix that is a power of 2
102+
103+
Q: Does it make any sense to have those operations defined for unbounded
104+
integers?
105+
106+
The following will be implemented in a package to come.
107+
108+
* [ ] `a.or(b)` - or (`i`, `u`, `iu`)
109+
* [ ] `a.and(b)` - and (`i`, `u`, `iu`, `andln`) (NOTE: `andln` is going to be replaced with `andn` in future)
110+
* [ ] `a.xor(b)` - xor (`i`, `u`, `iu`)
111+
* [ ] `a.setn(b)` - set specified bit to `1`
112+
* [ ] `a.shln(b)` - shift left (`i`, `u`, `iu`)
113+
* [ ] `a.shrn(b)` - shift right (`i`, `u`, `iu`)
114+
* [ ] `a.testn(b)` - test if specified bit is set
115+
* [ ] `a.maskn(b)` - clear bits with indexes higher or equal to `b` (`i`)
116+
* [ ] `a.bincn(b)` - add `1 << b` to the number
117+
* [ ] `a.notn(w)` - not (for the width specified by `w`) (`i`)
118+
119+
### Test
120+
121+
* [x] `a.sign()` - return -1, 0, 1
122+
* [x] `a.iszero()` - no comments
123+
* [x] `a.isone()` - no comments
124+
* [x] `a.ispositive()` - no comments
125+
* [x] `a.isnegative()` - no comments
126+
* [x] `a.isnonnegative()` - no comments
127+
* [x] `a.isnonpositive()` - no comments
128+
* [x] `a.iseven()` - no comments
129+
* [x] `a.isodd()` - no comments
130+
* [x] `a.divides(b)` - no comments
131+
132+
### Utilities
133+
134+
* [x] `ZZ.has(object)` - returns true if the supplied `object` is an integer.
135+
* [x] `ZZ.max(a, b)` - return `a` if `a` larger than `b`.
136+
* [x] `ZZ.min(a, b)` - return `a` if `a` smaller than `b`.
137+
* [ ] `a.bitLength()` - get number of bits occupied
138+
* [ ] `a.zeroBits()` - return number of less-significant consequent zero bits (example: `1010000` has 4 zero bits)
139+
* [ ] `a.byteLength()` - return number of bytes occupied
140+
* [ ] `a.toTwos(width)` - convert to two's complement representation, where `width` is bit width
141+
* [ ] `a.fromTwos(width)` - convert from two's complement representation, where `width` is the bit width
142+
143+
### Conversion
144+
* [x] `a.toString(base=10)` - convert to base-string (No zero padding, this is up to you)
145+
* [x] `a.bin()` - alias for `a.toString(2)`
146+
* [x] `a.oct()` - alias for `a.toString(8)`
147+
* [x] `a.hex()` - alias for `a.toString(16)`
148+
* [x] `d = a.digits(base=10)` - returns little endian array of digits in given base so that d[0] is the first digit, d[1] the second, etc.
149+
* [x] `a.bin()` - alias for `a.digits(2)`.
150+
* [x] `a.valueOf()` - convert to Javascript Number (limited to 53 bits `toNumber` in bn.js)
151+
* [x] `a.toJSON()` - convert to JSON compatible hex string (alias of `toString(16)`)
152+
* [ ] `a.to(type, endian, length)` - convert to an instance of `type`, which must behave like an `Array` (`toArrayLike` in bn.js)
153+
* [ ] `a.toArray(endian, length)` - convert to byte `Array`, and optionally zero pad to length, throwing if already exceeding
154+
* [ ] `a.toBuffer(endian, length)` - convert to Node.js Buffer (if available). For compatibility with browserify and similar tools, use this instead: `a.toArrayLike(Buffer, endian, length)`

0 commit comments

Comments
 (0)