exponential-number 1.0.0 → 1.2.0
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.
- package/.prettierrc.yml +3 -0
- package/LICENSE +202 -0
- package/README.md +9 -0
- package/dist/class/exponent-number.class.d.ts +19 -0
- package/dist/class/exponent-number.class.js +148 -0
- package/dist/const.d.ts +3 -0
- package/dist/const.js +6 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +15 -4
- package/dist/utils/util-math.utils.d.ts +5 -0
- package/dist/utils/util-math.utils.js +76 -0
- package/jest.config.js +2 -1
- package/package.json +27 -26
- package/src/class/exponent-number.class.ts +229 -0
- package/src/const.ts +3 -0
- package/src/index.ts +1 -3
- package/src/utils/util-math.utils.ts +118 -0
- package/test/test.ts +855 -9
- package/tsconfig.json +18 -13
- package/.idea/librry-test.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/prettier.xml +0 -9
- package/.idea/vcs.xml +0 -6
|
@@ -0,0 +1,229 @@
|
|
|
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
|
+
isMoreThanValue(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
|
+
isMoreThanOrEqualValue(otherNumber: ExponentNumber): boolean {
|
|
227
|
+
return this.isMoreThanValue(otherNumber) || this.isEqual(otherNumber);
|
|
228
|
+
}
|
|
229
|
+
}
|
package/src/const.ts
ADDED
package/src/index.ts
CHANGED
|
@@ -0,0 +1,118 @@
|
|
|
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
|
+
}
|