smath 1.1.8 → 1.3.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/README.md +49 -17
- package/dist/src/bin.js +93 -0
- package/dist/src/index.js +154 -0
- package/package.json +12 -10
- package/types/src/bin.d.ts +2 -0
- package/dist/index.js +0 -51
- /package/types/{index.d.ts → src/index.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
[Docs](https://npm.nicfv.com/smath/) | [GitHub](https://github.com/nicfv/npm/tree/main/smath/) | [npm](https://www.npmjs.com/package/smath) | [Changelog](https://github.com/nicfv/npm/blob/main/smath//CHANGELOG.md) | Small math function library
|
|
1
|
+
[Home](/) | [Docs](https://npm.nicfv.com/smath/) | [GitHub](https://github.com/nicfv/npm/tree/main/smath/) | [npm](https://www.npmjs.com/package/smath) | [Changelog](https://github.com/nicfv/npm/blob/main/smath//CHANGELOG.md) | [YouTube](https://www.youtube.com/@nciv) | Small math function library
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|

|
|
5
|
-

|
|
6
6
|

|
|
7
7
|

|
|
8
8
|

|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
smath can be installed from the official [npm package repository](https://www.npmjs.com/package/smath). It is highly recommended to install the latest version, which is installed by default with the following command.
|
|
13
13
|
|
|
14
14
|
```shell
|
|
15
|
-
npm i smath@1.
|
|
15
|
+
npm i smath@1.3.0
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
## Bugs and Requests
|
|
@@ -28,31 +28,63 @@ Similar to JavaScript's builtin [`Math`](https://developer.mozilla.org/en-US/doc
|
|
|
28
28
|
|
|
29
29
|
## Example
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
Here are a few examples written in JavaScript for a quick start into `SMath`.
|
|
32
|
+
|
|
33
|
+
### JavaScript Math Oddities
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import { SMath } from 'smath';
|
|
37
|
+
|
|
38
|
+
// Determine the value of 0.1 + 0.2 using vanilla JavaScript and SMath
|
|
39
|
+
console.log('0.1 + 0.2 == 0.3 is ' + (0.1 + 0.2 == 0.3));
|
|
40
|
+
console.log('SMath.approx(0.1 + 0.2, 0.3) is ' + SMath.approx(0.1 + 0.2, 0.3));
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```text
|
|
44
|
+
0.1 + 0.2 == 0.3 is false
|
|
45
|
+
SMath.approx(0.1 + 0.2, 0.3) is true
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Temperature Conversion
|
|
49
|
+
|
|
50
|
+
A temperature conversion tool using [`SMath.translate`](https://npm.nicfv.com/smath/classes/SMath.html#translate) to convert units. The translation uses freezing and boiling points for 2 unit systems to linearly interpolate between them.
|
|
32
51
|
|
|
33
52
|
```js
|
|
34
53
|
import { SMath } from 'smath';
|
|
35
54
|
|
|
55
|
+
// Water always freezes at the
|
|
56
|
+
// same temperature, but the
|
|
57
|
+
// units might be different.
|
|
36
58
|
// Define some constants to
|
|
37
|
-
//
|
|
59
|
+
// create two number ranges.
|
|
38
60
|
const C_Freeze = 0,
|
|
39
61
|
C_Boil = 100,
|
|
40
62
|
F_Freeze = 32,
|
|
41
63
|
F_Boil = 212;
|
|
42
64
|
|
|
43
65
|
// Use the `SMath` class to
|
|
44
|
-
//
|
|
66
|
+
// generate an array of five
|
|
67
|
+
// linearly spaced temperature
|
|
68
|
+
// values from 0 to 20.
|
|
69
|
+
const C = SMath.linspace(0, 20, 5);
|
|
70
|
+
|
|
71
|
+
// Use the `SMath` class to linearly
|
|
72
|
+
// interpolate the temperature in the
|
|
45
73
|
// C number range to a temperature
|
|
46
|
-
// in the F number range
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
// is valid using `approx`
|
|
54
|
-
const valid = SMath.approx(F_expected, F_actual);
|
|
55
|
-
if (!valid) {
|
|
56
|
-
throw new Error('Invalid result.');
|
|
74
|
+
// in the F number range.
|
|
75
|
+
const F = C.map(c => SMath.translate(c, C_Freeze, C_Boil, F_Freeze, F_Boil));
|
|
76
|
+
|
|
77
|
+
// Print out each temperature
|
|
78
|
+
// in both units of C and F.
|
|
79
|
+
for (let i = 0; i < C.length; i++) {
|
|
80
|
+
console.log(C[i].toFixed() + 'C is ' + F[i].toFixed() + 'F')
|
|
57
81
|
}
|
|
58
82
|
```
|
|
83
|
+
|
|
84
|
+
```text
|
|
85
|
+
0C is 32F
|
|
86
|
+
5C is 41F
|
|
87
|
+
10C is 50F
|
|
88
|
+
15C is 59F
|
|
89
|
+
20C is 68F
|
|
90
|
+
```
|
package/dist/src/bin.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
var _1 = require(".");
|
|
5
|
+
var args = process.argv.slice(2);
|
|
6
|
+
/**
|
|
7
|
+
* Try to convert an argument into a numeric value.
|
|
8
|
+
*/
|
|
9
|
+
function N(index, defaultVal) {
|
|
10
|
+
if (defaultVal === void 0) { defaultVal = NaN; }
|
|
11
|
+
if (index >= args.length) {
|
|
12
|
+
if (Number.isFinite(defaultVal)) {
|
|
13
|
+
return defaultVal;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
console.error('Required argument ' + index + ' is missing!');
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
var arg = Number.parseFloat(args[index]);
|
|
21
|
+
if (Number.isFinite(arg)) {
|
|
22
|
+
return arg;
|
|
23
|
+
}
|
|
24
|
+
else if (Number.isFinite(defaultVal)) {
|
|
25
|
+
return defaultVal;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
console.error('Argument #' + index + ' is ' + arg + ' but a number was expected.');
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (args.length < 1 || args[0].includes('help')) {
|
|
33
|
+
console.log('Key: <required> [optional]');
|
|
34
|
+
console.log('Arguments:');
|
|
35
|
+
console.log(' help : Show this page');
|
|
36
|
+
console.log(' approx <a> <b> [eps] : Check if `a` and `b` are approximately equal');
|
|
37
|
+
console.log(' avg <c0> [c1] ... [cn] : Take an average of `n` numbers');
|
|
38
|
+
console.log(' clamp <n> <min> <max> : Clamp `n` between `min` and `max`');
|
|
39
|
+
console.log(' expand <n> <min> <max> : Expand normalized `n` between `min` and `max`');
|
|
40
|
+
console.log(' linspace <min> <max> <n> : Generate `n` linearly spaced numbers between `min` and `max`');
|
|
41
|
+
console.log(' logspace <min> <max> <n> : Generate `n` logarithmically spaced numbers between `min` and `max`');
|
|
42
|
+
console.log(' normalize <n> <min> <max>');
|
|
43
|
+
console.log(' : Normalize `n` between `min` and `max`');
|
|
44
|
+
console.log(' translate <n> <min1> <max1> <min2> <max2>');
|
|
45
|
+
console.log(' : Linearly interpolate `n` from `min1`, `max1` to `min2`, `max2`');
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
switch (args[0]) {
|
|
49
|
+
case ('approx'): {
|
|
50
|
+
console.log(_1.SMath.approx(N(1), N(2), N(3, 1e-6)));
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
case ('avg'): {
|
|
54
|
+
if (args.length < 2) {
|
|
55
|
+
console.error('Need at least 1 argument.');
|
|
56
|
+
process.exit(1);
|
|
57
|
+
}
|
|
58
|
+
var operands = [];
|
|
59
|
+
for (var i = 1; i < args.length; i++) {
|
|
60
|
+
operands.push(N(i));
|
|
61
|
+
}
|
|
62
|
+
console.log(_1.SMath.avg.apply(_1.SMath, operands));
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
case ('clamp'): {
|
|
66
|
+
console.log(_1.SMath.clamp(N(1), N(2), N(3)));
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
case ('expand'): {
|
|
70
|
+
console.log(_1.SMath.expand(N(1), N(2), N(3)));
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
case ('linspace'): {
|
|
74
|
+
console.log(_1.SMath.linspace(N(1), N(2), N(3)));
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
case ('logspace'): {
|
|
78
|
+
console.log(_1.SMath.logspace(N(1), N(2), N(3)));
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
case ('normalize'): {
|
|
82
|
+
console.log(_1.SMath.normalize(N(1), N(2), N(3)));
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
case ('translate'): {
|
|
86
|
+
console.log(_1.SMath.translate(N(1), N(2), N(3), N(4), N(5)));
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
default: {
|
|
90
|
+
console.error('Unknown argument "' + args[0] + '". Use with "help" for a list of commands.');
|
|
91
|
+
process.exit(1);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SMath = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
* Small math function library
|
|
7
|
+
*
|
|
8
|
+
* Exports the public-facing API for `smath`
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Contains a small math function library including
|
|
12
|
+
* useful interpolation and extrapolation functions.
|
|
13
|
+
*/
|
|
14
|
+
var SMath = /** @class */ (function () {
|
|
15
|
+
function SMath() {
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Compute the average, or mean, of a set of numbers.
|
|
19
|
+
* @param n Any amount of numeric inputs
|
|
20
|
+
* @returns The average, or mean
|
|
21
|
+
* @example
|
|
22
|
+
* ```js
|
|
23
|
+
* const mean = SMath.avg(1, 2, 3, 4); // 2.5
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
SMath.avg = function () {
|
|
27
|
+
var n = [];
|
|
28
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
29
|
+
n[_i] = arguments[_i];
|
|
30
|
+
}
|
|
31
|
+
return n.reduce(function (prev, curr) { return prev + curr; }) / n.length;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Clamp a number within a range.
|
|
35
|
+
* @param n The number to clamp
|
|
36
|
+
* @param min The minimum value of the range
|
|
37
|
+
* @param max The maximum value of the range
|
|
38
|
+
* @returns A clamped number
|
|
39
|
+
* @example
|
|
40
|
+
* ```js
|
|
41
|
+
* const n1 = SMath.clamp(5, 0, 10), // 5
|
|
42
|
+
* n2 = SMath.clamp(-2, 0, 10); // 0
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
SMath.clamp = function (n, min, max) {
|
|
46
|
+
if (n < min) {
|
|
47
|
+
return min;
|
|
48
|
+
}
|
|
49
|
+
if (n > max) {
|
|
50
|
+
return max;
|
|
51
|
+
}
|
|
52
|
+
return n;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Check if two numbers are approximately equal with a maximum abolute error.
|
|
56
|
+
* @param a Any number
|
|
57
|
+
* @param b Any number
|
|
58
|
+
* @param epsilon Maximum absolute error
|
|
59
|
+
* @returns True if `a` is approximately `b`
|
|
60
|
+
* @example
|
|
61
|
+
* ```js
|
|
62
|
+
* const b1 = SMath.approx(1 / 3, 0.33, 1e-6), // false
|
|
63
|
+
* b2 = SMath.approx(1 / 3, 0.33, 1e-2); // true
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
SMath.approx = function (a, b, epsilon) {
|
|
67
|
+
if (epsilon === void 0) { epsilon = 1e-6; }
|
|
68
|
+
return a - b < epsilon && b - a < epsilon;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Normalize the number `n` from the range `min, max` to the range `0, 1`
|
|
72
|
+
* @param n The number to normalize
|
|
73
|
+
* @param min The minimum value in the range
|
|
74
|
+
* @param max The maximum value in the range
|
|
75
|
+
* @returns A normalized value
|
|
76
|
+
* @example
|
|
77
|
+
* ```js
|
|
78
|
+
* const y = SMath.normalize(18, 9, 99); // 0.1
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
SMath.normalize = function (n, min, max) {
|
|
82
|
+
if (min === max) {
|
|
83
|
+
return 0;
|
|
84
|
+
}
|
|
85
|
+
return (n - min) / (max - min);
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Expand a normalized number `n` to the range `min, max`
|
|
89
|
+
* @param n A normalized number
|
|
90
|
+
* @param min The minimum value in the range
|
|
91
|
+
* @param max The maximum value in the range
|
|
92
|
+
* @returns A value within the number range
|
|
93
|
+
* @example
|
|
94
|
+
* ```js
|
|
95
|
+
* const y = SMath.expand(0.25, 4, 6); // 4.5
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
SMath.expand = function (n, min, max) {
|
|
99
|
+
return (max - min) * n + min;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Translate a number `n` from the range `min1, max1` to the range `min2, max2`
|
|
103
|
+
* @param n The number to translate
|
|
104
|
+
* @param min1 The minimum value from the initial range
|
|
105
|
+
* @param max1 The maximum value from the initial range
|
|
106
|
+
* @param min2 The minimum value for the final range
|
|
107
|
+
* @param max2 The maximum value for the final range
|
|
108
|
+
* @returns A translated number in the final range
|
|
109
|
+
* @example
|
|
110
|
+
* ```js
|
|
111
|
+
* const C = 20,
|
|
112
|
+
* F = SMath.translate(C, 0, 100, 32, 212); // 68
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
SMath.translate = function (n, min1, max1, min2, max2) {
|
|
116
|
+
return this.expand(this.normalize(n, min1, max1), min2, max2);
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Generate an array of linearly spaced numbers.
|
|
120
|
+
* @param min The initial value of the linear space
|
|
121
|
+
* @param max The final value of the linear space
|
|
122
|
+
* @param count The number of values in the space
|
|
123
|
+
* @returns The linear space as an array of numbers
|
|
124
|
+
* @example
|
|
125
|
+
* ```js
|
|
126
|
+
* const space = SMath.linspace(1, 5, 6);
|
|
127
|
+
* // [ 1, 1.8, 2.6, 3.4, 4.2, 5 ]
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
SMath.linspace = function (min, max, count) {
|
|
131
|
+
var space = [];
|
|
132
|
+
for (var i = 0; i < count; i++) {
|
|
133
|
+
space[i] = this.translate(i, 0, count - 1, min, max);
|
|
134
|
+
}
|
|
135
|
+
return space;
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Generate an array of logarithmically spaced numbers.
|
|
139
|
+
* @param min The initial magnitude of the space
|
|
140
|
+
* @param max The final magnitude of the space
|
|
141
|
+
* @param count The number of values in the space
|
|
142
|
+
* @returns The logarithmic space as an array of numbers
|
|
143
|
+
* @example
|
|
144
|
+
* ```js
|
|
145
|
+
* const space = SMath.logspace(0, 2, 5);
|
|
146
|
+
* // [ 1, 3.2, 10, 31.6, 100 ]
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
SMath.logspace = function (min, max, count) {
|
|
150
|
+
return this.linspace(min, max, count).map(function (n) { return Math.pow(10, n); });
|
|
151
|
+
};
|
|
152
|
+
return SMath;
|
|
153
|
+
}());
|
|
154
|
+
exports.SMath = SMath;
|
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smath",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Small math function library",
|
|
5
5
|
"homepage": "https://npm.nicfv.com/smath",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
6
|
+
"bin": "dist/src/bin.js",
|
|
7
|
+
"main": "dist/src/index.js",
|
|
8
|
+
"types": "types/src/index.d.ts",
|
|
8
9
|
"files": [
|
|
9
|
-
"dist",
|
|
10
|
-
"types"
|
|
10
|
+
"dist/src",
|
|
11
|
+
"types/src"
|
|
11
12
|
],
|
|
12
13
|
"scripts": {
|
|
13
|
-
"build": "rm -rf dist && tsc
|
|
14
|
-
"
|
|
15
|
-
"test": "tsc --noEmit",
|
|
14
|
+
"build": "rm -rf dist && tsc",
|
|
15
|
+
"test": "npm run build && node dist/test/index.js",
|
|
16
16
|
"clean": "rm -rf node_modules package-lock.json dist types docs",
|
|
17
17
|
"docs": "rm -rf docs && typedoc --includeVersion --disableSources --hideGenerator src",
|
|
18
|
-
"prepack": "npm run build
|
|
18
|
+
"prepack": "npm run build",
|
|
19
19
|
"postpack": "rm -rf dist types"
|
|
20
20
|
},
|
|
21
21
|
"keywords": [
|
|
@@ -47,7 +47,9 @@
|
|
|
47
47
|
"repository": "github:nicfv/npm",
|
|
48
48
|
"license": "MIT",
|
|
49
49
|
"devDependencies": {
|
|
50
|
+
"@types/node": "20.11.30",
|
|
50
51
|
"typedoc": "0.25.12",
|
|
51
|
-
"typescript": "5.4.
|
|
52
|
+
"typescript": "5.4.3",
|
|
53
|
+
"xpt": "0.2.0"
|
|
52
54
|
}
|
|
53
55
|
}
|
package/dist/index.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SMath = void 0;
|
|
4
|
-
var SMath = (function () {
|
|
5
|
-
function SMath() {
|
|
6
|
-
}
|
|
7
|
-
SMath.avg = function () {
|
|
8
|
-
var n = [];
|
|
9
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
10
|
-
n[_i] = arguments[_i];
|
|
11
|
-
}
|
|
12
|
-
return n.reduce(function (prev, curr) { return prev + curr; }) / n.length;
|
|
13
|
-
};
|
|
14
|
-
SMath.clamp = function (n, min, max) {
|
|
15
|
-
if (n < min) {
|
|
16
|
-
return min;
|
|
17
|
-
}
|
|
18
|
-
if (n > max) {
|
|
19
|
-
return max;
|
|
20
|
-
}
|
|
21
|
-
return n;
|
|
22
|
-
};
|
|
23
|
-
SMath.approx = function (a, b, epsilon) {
|
|
24
|
-
if (epsilon === void 0) { epsilon = 1e-6; }
|
|
25
|
-
return a - b < epsilon && b - a < epsilon;
|
|
26
|
-
};
|
|
27
|
-
SMath.normalize = function (n, min, max) {
|
|
28
|
-
if (min === max) {
|
|
29
|
-
return 0;
|
|
30
|
-
}
|
|
31
|
-
return (n - min) / (max - min);
|
|
32
|
-
};
|
|
33
|
-
SMath.expand = function (n, min, max) {
|
|
34
|
-
return (max - min) * n + min;
|
|
35
|
-
};
|
|
36
|
-
SMath.translate = function (n, min1, max1, min2, max2) {
|
|
37
|
-
return this.expand(this.normalize(n, min1, max1), min2, max2);
|
|
38
|
-
};
|
|
39
|
-
SMath.linspace = function (min, max, count) {
|
|
40
|
-
var space = [];
|
|
41
|
-
for (var i = 0; i < count; i++) {
|
|
42
|
-
space[i] = this.translate(i, 0, count - 1, min, max);
|
|
43
|
-
}
|
|
44
|
-
return space;
|
|
45
|
-
};
|
|
46
|
-
SMath.logspace = function (min, max, count) {
|
|
47
|
-
return this.linspace(min, max, count).map(function (n) { return Math.pow(10, n); });
|
|
48
|
-
};
|
|
49
|
-
return SMath;
|
|
50
|
-
}());
|
|
51
|
-
exports.SMath = SMath;
|
|
File without changes
|