Created
December 9, 2015 13:59
-
-
Save saschanaz/e375dbb1c7b86f63f7b0 to your computer and use it in GitHub Desktop.
Arbitrary Precision
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Use: | |
| new ArbPre("123456781234780123487234").mulS(1000); | |
| new ArbPre("123456781234780123487234").mul(new ArbPre("6289304527348952893045206")) | |
| */ | |
| class ArbPre { | |
| constructor(input) { | |
| if (typeof input === "number") { | |
| let nums = this._nums = []; | |
| while (input > 0) { | |
| nums.push(input % 10); | |
| input = Math.floor(input / 10); | |
| } | |
| } | |
| else if (typeof input === "string") { | |
| let nums = this._nums = input.split('').map(i => +i); | |
| nums.reverse(); | |
| this._trimZero(); | |
| } | |
| else { | |
| this._nums = []; | |
| } | |
| } | |
| _trimZero() { | |
| while (this._nums[this.length - 1] === 0 && this.length > 1) { | |
| this._nums.pop(); | |
| } | |
| } | |
| get length() { | |
| return this._nums.length; | |
| } | |
| at(index) { | |
| return this._nums[index]; | |
| } | |
| atAsString(index) { | |
| return this._nums[this._nums.length - 1 - index] | |
| } | |
| toString() { | |
| let result = ''; | |
| for (let strNum of this._nums) { | |
| result = strNum + result; | |
| } | |
| return result; | |
| } | |
| clone() { | |
| let arbPre = new ArbPre; | |
| arbPre._nums = this._nums.slice(); | |
| return arbPre; | |
| } | |
| sliceAsString(start, end) { | |
| start = (start == null || start === 0) ? this.length : -start; | |
| end = end === null ? 0 : -end; | |
| this._nums = this._nums.slice(end, start); | |
| return this; | |
| } | |
| slice(start, end) { | |
| this._nums = this._nums.slice(start, end); | |
| return this; | |
| } | |
| _addUnnorm(input) { | |
| let thisNums = this._nums; | |
| let inputNums = input._nums; | |
| let length = Math.max(thisNums.length, inputNums.length); | |
| for (let i = 0; i < length; i++) { | |
| thisNums[i] = (thisNums[i] || 0) + (inputNums[i] || 0); | |
| } | |
| return this; | |
| } | |
| _normalize() { | |
| let nums = this._nums; | |
| for (let i = 0; i < this.length; i++) { | |
| let num = nums[i]; | |
| if (num >= 10) { | |
| nums[i + 1] = (nums[i + 1] || 0) + Math.floor(num / 10); | |
| nums[i] = num % 10; | |
| } | |
| } | |
| this._trimZero(); | |
| } | |
| add(arbPre) { | |
| this._addUnnorm(arbPre); | |
| this._normalize(); | |
| return this; | |
| } | |
| mul10(repeat) { | |
| for (let i = 0; i < repeat; i++) { | |
| this._nums.unshift(0); | |
| } | |
| return this; | |
| } | |
| mulS(inputNum) { | |
| for (let i = 0; i < this.length; i++) { | |
| this._nums[i] *= inputNum; | |
| } | |
| this._normalize(); | |
| return this; | |
| } | |
| mul(arbPre) { | |
| let longerA; | |
| let shorter; | |
| if (this.length > arbPre.length) { | |
| longerA = this._nums; | |
| shorter = arbPre; | |
| } | |
| else { | |
| longerA = arbPre._nums; | |
| shorter = this; | |
| } | |
| let result = new ArbPre(); | |
| for (let i = 0; i < longerA.length; i++) { | |
| let mul = shorter.clone().mulS(longerA[i]).mul10(i); | |
| result._addUnnorm(mul); | |
| } | |
| result._normalize(); | |
| this._nums = result._nums; | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment