smath 1.1.8 → 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/README.md +49 -17
- package/dist/bin.js +90 -0
- package/package.json +4 -2
- package/types/bin.d.ts +2 -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.2.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/bin.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
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
|
+
function N(index, defaultVal) {
|
|
7
|
+
if (defaultVal === void 0) { defaultVal = NaN; }
|
|
8
|
+
if (index >= args.length) {
|
|
9
|
+
if (Number.isFinite(defaultVal)) {
|
|
10
|
+
return defaultVal;
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
console.error('Required argument ' + index + ' is missing!');
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
var arg = Number.parseFloat(args[index]);
|
|
18
|
+
if (Number.isFinite(arg)) {
|
|
19
|
+
return arg;
|
|
20
|
+
}
|
|
21
|
+
else if (Number.isFinite(defaultVal)) {
|
|
22
|
+
return defaultVal;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
console.error('Argument #' + index + ' is ' + arg + ' but a number was expected.');
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (args.length < 1 || args[0].includes('help')) {
|
|
30
|
+
console.log('Key: <required> [optional]');
|
|
31
|
+
console.log('Arguments:');
|
|
32
|
+
console.log(' help : Show this page');
|
|
33
|
+
console.log(' approx <a> <b> [eps] : Check if `a` and `b` are approximately equal');
|
|
34
|
+
console.log(' avg <c0> [c1] ... [cn] : Take an average of `n` numbers');
|
|
35
|
+
console.log(' clamp <n> <min> <max> : Clamp `n` between `min` and `max`');
|
|
36
|
+
console.log(' expand <n> <min> <max> : Expand normalized `n` between `min` and `max`');
|
|
37
|
+
console.log(' linspace <min> <max> <n> : Generate `n` linearly spaced numbers between `min` and `max`');
|
|
38
|
+
console.log(' logspace <min> <max> <n> : Generate `n` logarithmically spaced numbers between `min` and `max`');
|
|
39
|
+
console.log(' normalize <n> <min> <max>');
|
|
40
|
+
console.log(' : Normalize `n` between `min` and `max`');
|
|
41
|
+
console.log(' translate <n> <min1> <max1> <min2> <max2>');
|
|
42
|
+
console.log(' : Linearly interpolate `n` from `min1`, `max1` to `min2`, `max2`');
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
45
|
+
switch (args[0]) {
|
|
46
|
+
case ('approx'): {
|
|
47
|
+
console.log(_1.SMath.approx(N(1), N(2), N(3, 1e-6)));
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case ('avg'): {
|
|
51
|
+
if (args.length < 2) {
|
|
52
|
+
console.error('Need at least 1 argument.');
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
var operands = [];
|
|
56
|
+
for (var i = 1; i < args.length; i++) {
|
|
57
|
+
operands.push(N(i));
|
|
58
|
+
}
|
|
59
|
+
console.log(_1.SMath.avg.apply(_1.SMath, operands));
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case ('clamp'): {
|
|
63
|
+
console.log(_1.SMath.clamp(N(1), N(2), N(3)));
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
case ('expand'): {
|
|
67
|
+
console.log(_1.SMath.expand(N(1), N(2), N(3)));
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
case ('linspace'): {
|
|
71
|
+
console.log(_1.SMath.linspace(N(1), N(2), N(3)));
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
case ('logspace'): {
|
|
75
|
+
console.log(_1.SMath.logspace(N(1), N(2), N(3)));
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
case ('normalize'): {
|
|
79
|
+
console.log(_1.SMath.normalize(N(1), N(2), N(3)));
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
case ('translate'): {
|
|
83
|
+
console.log(_1.SMath.translate(N(1), N(2), N(3), N(4), N(5)));
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
default: {
|
|
87
|
+
console.error('Unknown argument "' + args[0] + '". Use with "help" for a list of commands.');
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smath",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Small math function library",
|
|
5
5
|
"homepage": "https://npm.nicfv.com/smath",
|
|
6
|
+
"bin": "dist/bin.js",
|
|
6
7
|
"main": "dist/index.js",
|
|
7
8
|
"types": "types/index.d.ts",
|
|
8
9
|
"files": [
|
|
@@ -47,7 +48,8 @@
|
|
|
47
48
|
"repository": "github:nicfv/npm",
|
|
48
49
|
"license": "MIT",
|
|
49
50
|
"devDependencies": {
|
|
51
|
+
"@types/node": "20.11.30",
|
|
50
52
|
"typedoc": "0.25.12",
|
|
51
53
|
"typescript": "5.4.2"
|
|
52
54
|
}
|
|
53
|
-
}
|
|
55
|
+
}
|
package/types/bin.d.ts
ADDED