exponential-number 1.2.2 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,15 +5,18 @@ export declare class ExponentNumber {
5
5
  copy(): ExponentNumber;
6
6
  normalize(): void;
7
7
  applyNewValues(newNumber: ExponentNumber): void;
8
+ resetValue(): void;
8
9
  toString(): string;
9
10
  plus(otherNumber: ExponentNumber): ExponentNumber;
10
11
  minus(otherNumber: ExponentNumber): ExponentNumber;
11
12
  multiply(otherNumber: ExponentNumber): ExponentNumber;
12
13
  divide(otherNumber: ExponentNumber): ExponentNumber;
13
14
  power(otherNumber: ExponentNumber): ExponentNumber;
15
+ root(otherNumber: ExponentNumber): ExponentNumber;
16
+ sqrt(): ExponentNumber;
14
17
  log(base: ExponentNumber): ExponentNumber;
15
18
  log10(): ExponentNumber;
16
- isMoreThanValue(otherNumber: ExponentNumber): boolean;
19
+ isGreaterThanValue(otherNumber: ExponentNumber): boolean;
17
20
  isEqual(otherNumber: ExponentNumber): boolean;
18
- isMoreThanOrEqualValue(otherNumber: ExponentNumber): boolean;
21
+ isGreaterThanOrEqualValue(otherNumber: ExponentNumber): boolean;
19
22
  }
@@ -28,14 +28,17 @@ class ExponentNumber {
28
28
  this.value = Math.max(Math.log10(this.value), 0);
29
29
  }
30
30
  if (this.value === Math.pow(10, -const_1.VALUE_EXPONENT_DIFFERENCE_LIMIT)) {
31
- this.value = 0;
32
- this.exponentFactor = 0;
31
+ this.resetValue();
33
32
  }
34
33
  }
35
34
  applyNewValues(newNumber) {
36
35
  this.exponentFactor = newNumber.exponentFactor;
37
36
  this.value = newNumber.value;
38
37
  }
38
+ resetValue() {
39
+ this.exponentFactor = 0;
40
+ this.value = 0;
41
+ }
39
42
  toString() {
40
43
  const cutNumber = Number(this.value.toPrecision(const_1.DECIMAL_DIGITS));
41
44
  let numberText = cutNumber.toString();
@@ -49,7 +52,10 @@ class ExponentNumber {
49
52
  const firstPart = `${firstNumbers[0]}.${firstNumbers.slice(1)}`;
50
53
  numberText = `${Number(firstPart)}e${numberExp}`;
51
54
  }
52
- return `${'e'.repeat(this.exponentFactor)}${numberText}`;
55
+ const exponentText = this.exponentFactor <= const_1.EXPONENT_COUNT_LIMIT
56
+ ? 'e'.repeat(this.exponentFactor)
57
+ : `e(${this.exponentFactor})`;
58
+ return `${exponentText}${numberText}`;
53
59
  }
54
60
  plus(otherNumber) {
55
61
  if (this.exponentFactor === otherNumber.exponentFactor) {
@@ -74,12 +80,13 @@ class ExponentNumber {
74
80
  if (this.exponentFactor === otherNumber.exponentFactor) {
75
81
  if (this.exponentFactor > 1) {
76
82
  if (otherNumber.value >= this.value) {
77
- this.applyNewValues(new ExponentNumber(0, 0));
83
+ this.resetValue();
78
84
  }
79
85
  return this;
80
86
  }
87
+ console.log(this, otherNumber);
81
88
  if (this.value === otherNumber.value) {
82
- this.applyNewValues(new ExponentNumber(0, 0));
89
+ this.resetValue();
83
90
  return this;
84
91
  }
85
92
  this.applyNewValues((0, util_math_utils_1.minusEqualExponentLevelNumber)(this, otherNumber));
@@ -89,8 +96,8 @@ class ExponentNumber {
89
96
  this.applyNewValues((0, util_math_utils_1.minusDifferentExponentLevelNumber)(this, otherNumber));
90
97
  }
91
98
  else {
92
- if (otherNumber.exponentFactor > this.exponentFactor) {
93
- this.applyNewValues(new ExponentNumber(0, 0));
99
+ if (otherNumber.exponentFactor >= this.exponentFactor) {
100
+ this.resetValue();
94
101
  }
95
102
  }
96
103
  }
@@ -111,6 +118,7 @@ class ExponentNumber {
111
118
  }
112
119
  const result = new ExponentNumber(this.exponentFactor, Math.log10(this.value));
113
120
  result.minus(new ExponentNumber(otherNumber.exponentFactor, Math.log10(otherNumber.value)));
121
+ console.log(result);
114
122
  this.exponentFactor = result.exponentFactor + 1;
115
123
  this.value = result.value;
116
124
  this.normalize();
@@ -124,6 +132,24 @@ class ExponentNumber {
124
132
  this.normalize();
125
133
  return this;
126
134
  }
135
+ root(otherNumber) {
136
+ const result = new ExponentNumber(this.exponentFactor, Math.log10(this.value));
137
+ if (result.exponentFactor >= 1 &&
138
+ (otherNumber.exponentFactor > result.exponentFactor ||
139
+ (otherNumber.exponentFactor === result.exponentFactor && otherNumber.value > result.value))) {
140
+ this.resetValue();
141
+ return this;
142
+ }
143
+ result.divide(otherNumber);
144
+ console.log(result);
145
+ this.exponentFactor = result.exponentFactor + 1;
146
+ this.value = result.value;
147
+ this.normalize();
148
+ return this;
149
+ }
150
+ sqrt() {
151
+ return this.root(new ExponentNumber(0, 2));
152
+ }
127
153
  log(base) {
128
154
  const result = new ExponentNumber(this.exponentFactor, Math.log10(this.value));
129
155
  result.divide(new ExponentNumber(base.exponentFactor, Math.log10(base.value)));
@@ -134,15 +160,15 @@ class ExponentNumber {
134
160
  log10() {
135
161
  return new ExponentNumber(this.exponentFactor - 1, this.value);
136
162
  }
137
- isMoreThanValue(otherNumber) {
163
+ isGreaterThanValue(otherNumber) {
138
164
  return (this.exponentFactor > otherNumber.exponentFactor ||
139
165
  (this.exponentFactor === otherNumber.exponentFactor && this.value > otherNumber.value));
140
166
  }
141
167
  isEqual(otherNumber) {
142
168
  return otherNumber.exponentFactor === this.exponentFactor && otherNumber.value === this.value;
143
169
  }
144
- isMoreThanOrEqualValue(otherNumber) {
145
- return this.isMoreThanValue(otherNumber) || this.isEqual(otherNumber);
170
+ isGreaterThanOrEqualValue(otherNumber) {
171
+ return this.isGreaterThanValue(otherNumber) || this.isEqual(otherNumber);
146
172
  }
147
173
  }
148
174
  exports.ExponentNumber = ExponentNumber;
package/dist/const.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export declare const VALUE_EXPONENT_LIMIT = 100;
2
2
  export declare const VALUE_EXPONENT_DIFFERENCE_LIMIT = 12;
3
3
  export declare const DECIMAL_DIGITS = 5;
4
+ export declare const EXPONENT_COUNT_LIMIT = 5;
package/dist/const.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DECIMAL_DIGITS = exports.VALUE_EXPONENT_DIFFERENCE_LIMIT = exports.VALUE_EXPONENT_LIMIT = void 0;
3
+ exports.EXPONENT_COUNT_LIMIT = exports.DECIMAL_DIGITS = exports.VALUE_EXPONENT_DIFFERENCE_LIMIT = exports.VALUE_EXPONENT_LIMIT = void 0;
4
4
  exports.VALUE_EXPONENT_LIMIT = 100;
5
5
  exports.VALUE_EXPONENT_DIFFERENCE_LIMIT = 12;
6
6
  exports.DECIMAL_DIGITS = 5;
7
+ exports.EXPONENT_COUNT_LIMIT = 5;
package/package.json CHANGED
@@ -1,14 +1,19 @@
1
1
  {
2
2
  "name": "exponential-number",
3
- "version": "1.2.2",
3
+ "version": "1.3.1",
4
4
  "description": "Small package with class to work with big numbers. This class allows to work with 1e...(1.8e308)...e1e100. Allowed methods",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
8
11
  "license": "Apache-2.0",
9
- "repository": "https://github.com/AlexSmykov/exponential-numbers",
12
+ "repository": {
13
+ "url": "git+https://github.com/AlexSmykov/exponential-numbers.git"
14
+ },
10
15
  "scripts": {
11
- "build": "tsc -w",
16
+ "build": "tsc",
12
17
  "test": "jest --config=jest.config.js test.ts"
13
18
  },
14
19
  "keywords": [
package/.prettierignore DELETED
@@ -1,5 +0,0 @@
1
- **/.idea
2
- **/dist
3
- **/node_modules
4
-
5
- package-lock.json
package/.prettierrc.yml DELETED
@@ -1,3 +0,0 @@
1
- endOfLine: auto
2
- printWidth: 100
3
- singleQuote: true
package/jest.config.js DELETED
@@ -1,12 +0,0 @@
1
- const { createDefaultPreset } = require("ts-jest");
2
-
3
- const tsJestTransformCfg = createDefaultPreset().transform;
4
-
5
- /** @type {import("jest").Config} **/
6
- module.exports = {
7
- testEnvironment: "node",
8
- rootDir: "./",
9
- transform: {
10
- ...tsJestTransformCfg,
11
- },
12
- };
@@ -1,229 +0,0 @@
1
- import {
2
- minusDifferentExponentLevelNumber,
3
- minusEqualExponentLevelNumber,
4
- plusDifferentExponentLevelNumber,
5
- plusEqualExponentLevelNumber,
6
- } from '../utils/util-math.utils';
7
- import { DECIMAL_DIGITS, VALUE_EXPONENT_DIFFERENCE_LIMIT, VALUE_EXPONENT_LIMIT } from '../const';
8
-
9
- export class ExponentNumber {
10
- exponentFactor = 0;
11
- value = 0;
12
-
13
- constructor(exponentFactor = 0, value = 0) {
14
- this.exponentFactor = exponentFactor;
15
- this.value = value;
16
-
17
- this.normalize();
18
- }
19
-
20
- copy(): ExponentNumber {
21
- return new ExponentNumber(this.exponentFactor, this.value);
22
- }
23
-
24
- normalize(): void {
25
- while (Math.log10(this.value) >= VALUE_EXPONENT_LIMIT) {
26
- this.exponentFactor += 1;
27
- this.value = Math.log10(this.value);
28
- }
29
-
30
- while (this.exponentFactor >= 1 && this.value < VALUE_EXPONENT_LIMIT) {
31
- this.exponentFactor -= 1;
32
- this.value = Math.pow(10, this.value);
33
- }
34
-
35
- while (this.exponentFactor < 0 && this.value > 0) {
36
- this.exponentFactor += 1;
37
- this.value = Math.max(Math.log10(this.value), 0);
38
- }
39
-
40
- if (this.value === Math.pow(10, -VALUE_EXPONENT_DIFFERENCE_LIMIT)) {
41
- this.resetValue();
42
- }
43
- }
44
-
45
- applyNewValues(newNumber: ExponentNumber): void {
46
- this.exponentFactor = newNumber.exponentFactor;
47
- this.value = newNumber.value;
48
- }
49
-
50
- resetValue(): void {
51
- this.exponentFactor = 0;
52
- this.value = 0;
53
- }
54
-
55
- toString(): string {
56
- const cutNumber = Number(this.value.toPrecision(DECIMAL_DIGITS));
57
-
58
- let numberText = cutNumber.toString();
59
-
60
- if (Math.log10(cutNumber) >= VALUE_EXPONENT_DIFFERENCE_LIMIT) {
61
- const numberExp = Math.floor(Math.log10(cutNumber));
62
- const firstNumbers = cutNumber
63
- .toString()
64
- .replace('.', '')
65
- .split('e')[0]
66
- .slice(0, DECIMAL_DIGITS);
67
- const firstPart = `${firstNumbers[0]}.${firstNumbers.slice(1)}`;
68
- numberText = `${Number(firstPart)}e${numberExp}`;
69
- }
70
-
71
- return `${'e'.repeat(this.exponentFactor)}${numberText}`;
72
- }
73
-
74
- plus(otherNumber: ExponentNumber): ExponentNumber {
75
- if (this.exponentFactor === otherNumber.exponentFactor) {
76
- if (this.exponentFactor > 1) {
77
- return this;
78
- }
79
-
80
- this.applyNewValues(plusEqualExponentLevelNumber(this, otherNumber));
81
- } else {
82
- if (this.exponentFactor <= 1 && otherNumber.exponentFactor <= 1) {
83
- this.applyNewValues(plusDifferentExponentLevelNumber(this, otherNumber));
84
- } else {
85
- const biggerNumber = this.exponentFactor > otherNumber.exponentFactor ? this : otherNumber;
86
- this.applyNewValues(biggerNumber);
87
- }
88
- }
89
-
90
- this.normalize();
91
-
92
- return this;
93
- }
94
-
95
- minus(otherNumber: ExponentNumber): ExponentNumber {
96
- if (this.exponentFactor === otherNumber.exponentFactor) {
97
- if (this.exponentFactor > 1) {
98
- if (otherNumber.value >= this.value) {
99
- this.resetValue();
100
- }
101
-
102
- return this;
103
- }
104
-
105
- console.log(this, otherNumber);
106
- if (this.value === otherNumber.value) {
107
- this.resetValue();
108
-
109
- return this;
110
- }
111
-
112
- this.applyNewValues(minusEqualExponentLevelNumber(this, otherNumber));
113
- } else {
114
- if (this.exponentFactor <= 1 && otherNumber.exponentFactor <= 1) {
115
- this.applyNewValues(minusDifferentExponentLevelNumber(this, otherNumber));
116
- } else {
117
- if (otherNumber.exponentFactor >= this.exponentFactor) {
118
- this.resetValue();
119
- }
120
- }
121
- }
122
-
123
- this.normalize();
124
-
125
- return this;
126
- }
127
-
128
- multiply(otherNumber: ExponentNumber): ExponentNumber {
129
- const result = new ExponentNumber(this.exponentFactor, Math.log10(this.value));
130
-
131
- result.plus(new ExponentNumber(otherNumber.exponentFactor, Math.log10(otherNumber.value)));
132
-
133
- this.exponentFactor = result.exponentFactor + 1;
134
- this.value = result.value;
135
-
136
- this.normalize();
137
-
138
- return this;
139
- }
140
-
141
- divide(otherNumber: ExponentNumber): ExponentNumber {
142
- if (this.exponentFactor === 0 && this.value === 0) {
143
- return this;
144
- }
145
-
146
- const result = new ExponentNumber(this.exponentFactor, Math.log10(this.value));
147
-
148
- result.minus(new ExponentNumber(otherNumber.exponentFactor, Math.log10(otherNumber.value)));
149
-
150
- console.log(result);
151
- this.exponentFactor = result.exponentFactor + 1;
152
- this.value = result.value;
153
-
154
- this.normalize();
155
-
156
- return this;
157
- }
158
-
159
- power(otherNumber: ExponentNumber): ExponentNumber {
160
- const result = new ExponentNumber(this.exponentFactor, Math.log10(this.value));
161
-
162
- result.multiply(otherNumber);
163
-
164
- this.exponentFactor = result.exponentFactor + 1;
165
- this.value = result.value;
166
-
167
- this.normalize();
168
-
169
- return this;
170
- }
171
-
172
- root(otherNumber: ExponentNumber): ExponentNumber {
173
- const result = new ExponentNumber(this.exponentFactor, Math.log10(this.value));
174
-
175
- if (
176
- result.exponentFactor >= 1 &&
177
- (otherNumber.exponentFactor > result.exponentFactor ||
178
- (otherNumber.exponentFactor === result.exponentFactor && otherNumber.value > result.value))
179
- ) {
180
- this.resetValue();
181
-
182
- return this;
183
- }
184
-
185
- result.divide(otherNumber);
186
-
187
- console.log(result);
188
- this.exponentFactor = result.exponentFactor + 1;
189
- this.value = result.value;
190
-
191
- this.normalize();
192
-
193
- return this;
194
- }
195
-
196
- sqrt(): ExponentNumber {
197
- return this.root(new ExponentNumber(0, 2));
198
- }
199
-
200
- log(base: ExponentNumber): ExponentNumber {
201
- const result = new ExponentNumber(this.exponentFactor, Math.log10(this.value));
202
- result.divide(new ExponentNumber(base.exponentFactor, Math.log10(base.value)));
203
-
204
- this.applyNewValues(result);
205
-
206
- this.normalize();
207
-
208
- return this;
209
- }
210
-
211
- log10(): ExponentNumber {
212
- return new ExponentNumber(this.exponentFactor - 1, this.value);
213
- }
214
-
215
- isGreaterThanValue(otherNumber: ExponentNumber): boolean {
216
- return (
217
- this.exponentFactor > otherNumber.exponentFactor ||
218
- (this.exponentFactor === otherNumber.exponentFactor && this.value > otherNumber.value)
219
- );
220
- }
221
-
222
- isEqual(otherNumber: ExponentNumber): boolean {
223
- return otherNumber.exponentFactor === this.exponentFactor && otherNumber.value === this.value;
224
- }
225
-
226
- isGreaterThanOrEqualValue(otherNumber: ExponentNumber): boolean {
227
- return this.isGreaterThanValue(otherNumber) || this.isEqual(otherNumber);
228
- }
229
- }
package/src/const.ts DELETED
@@ -1,3 +0,0 @@
1
- export const VALUE_EXPONENT_LIMIT = 100;
2
- export const VALUE_EXPONENT_DIFFERENCE_LIMIT = 12;
3
- export const DECIMAL_DIGITS = 5;
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from './class/exponent-number.class';
@@ -1,118 +0,0 @@
1
- import { ExponentNumber } from '../class/exponent-number.class';
2
- import { VALUE_EXPONENT_DIFFERENCE_LIMIT } from '../const';
3
-
4
- export function plusEqualExponentLevelNumber(
5
- first: ExponentNumber,
6
- second: ExponentNumber,
7
- ): ExponentNumber {
8
- if (first.exponentFactor !== second.exponentFactor) {
9
- throw new Error('Expected exponentFactor to be equal');
10
- }
11
-
12
- if (second.exponentFactor > 1) {
13
- throw new Error('Expected exponentFactor to be lower or equal than 1');
14
- }
15
-
16
- if (second.exponentFactor === 1) {
17
- if (Math.abs(first.value - second.value) > VALUE_EXPONENT_DIFFERENCE_LIMIT) {
18
- return first.value - second.value > 0 ? first.copy() : second.copy();
19
- }
20
-
21
- const biggerValue = Math.max(first.value, second.value);
22
- const smallerValue = Math.min(first.value, second.value);
23
-
24
- return new ExponentNumber(
25
- 1,
26
- biggerValue + Math.log10(1 + Math.pow(10, smallerValue - biggerValue)),
27
- );
28
- }
29
-
30
- return new ExponentNumber(0, first.value + second.value);
31
- }
32
-
33
- export function plusDifferentExponentLevelNumber(
34
- first: ExponentNumber,
35
- second: ExponentNumber,
36
- ): ExponentNumber {
37
- if (first.exponentFactor === second.exponentFactor) {
38
- throw new Error('Expected exponentFactor to be different');
39
- }
40
-
41
- if (first.exponentFactor > 1 || second.exponentFactor > 1) {
42
- throw new Error('Expected exponentFactor to be lower or equal than 1');
43
- }
44
-
45
- const biggerValue = first.exponentFactor > second.exponentFactor ? first : second;
46
- const smallerValue = first.exponentFactor > second.exponentFactor ? second : first;
47
-
48
- const smallValueExponent = Math.log10(smallerValue.value);
49
- const exponentDifference = biggerValue.value - smallValueExponent;
50
-
51
- if (exponentDifference > VALUE_EXPONENT_DIFFERENCE_LIMIT) {
52
- return biggerValue.copy();
53
- }
54
-
55
- return new ExponentNumber(
56
- 1,
57
- biggerValue.value + Math.log10(1 + 1 / Math.pow(10, exponentDifference)),
58
- );
59
- }
60
-
61
- export function minusEqualExponentLevelNumber(
62
- first: ExponentNumber,
63
- second: ExponentNumber,
64
- ): ExponentNumber {
65
- if (first.exponentFactor !== second.exponentFactor) {
66
- throw new Error('Expected exponentFactor to be equal');
67
- }
68
-
69
- if (second.exponentFactor > 1) {
70
- throw new Error('Expected exponentFactor to be lower or equal than 1');
71
- }
72
-
73
- if (second.exponentFactor === 1) {
74
- if (second.value >= first.value) {
75
- return new ExponentNumber(0, 0);
76
- }
77
-
78
- if (first.value - second.value > VALUE_EXPONENT_DIFFERENCE_LIMIT) {
79
- return first.value - second.value < 0 ? first.copy() : second.copy();
80
- }
81
-
82
- return new ExponentNumber(
83
- 1,
84
- first.value + Math.log10(1 - Math.pow(10, second.value - first.value)),
85
- );
86
- }
87
-
88
- return new ExponentNumber(
89
- 0,
90
- Math.max(first.value - second.value, -VALUE_EXPONENT_DIFFERENCE_LIMIT),
91
- );
92
- }
93
-
94
- export function minusDifferentExponentLevelNumber(
95
- first: ExponentNumber,
96
- second: ExponentNumber,
97
- ): ExponentNumber {
98
- if (first.exponentFactor === second.exponentFactor) {
99
- throw new Error('Expected exponentFactor to be different');
100
- }
101
-
102
- if (first.exponentFactor > 1 || second.exponentFactor > 1) {
103
- throw new Error('Expected exponentFactor to be lower or equal than 1');
104
- }
105
-
106
- if (second.exponentFactor > first.exponentFactor) {
107
- return new ExponentNumber(0, 0);
108
- }
109
-
110
- const smallValueExponent = Math.log10(second.value);
111
- const exponentDifference = first.value - smallValueExponent;
112
-
113
- if (exponentDifference > VALUE_EXPONENT_DIFFERENCE_LIMIT) {
114
- return first.copy();
115
- }
116
-
117
- return new ExponentNumber(1, first.value + Math.log10(1 - 1 / Math.pow(10, exponentDifference)));
118
- }