smath 1.8.5 → 1.9.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 +0 -104
- package/dist/bin.js +5 -0
- package/dist/index.js +22 -3
- package/package.json +7 -9
- package/types/index.d.ts +12 -3
package/README.md
CHANGED
|
@@ -1,28 +1,3 @@
|
|
|
1
|
-
[Home](https://npm.nicfv.com/) | [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/@n1cfv) | Small math function library
|
|
2
|
-
|
|
3
|
-

|
|
4
|
-

|
|
5
|
-

|
|
6
|
-

|
|
7
|
-

|
|
8
|
-

|
|
9
|
-
[](https://paypal.me/nicfv)
|
|
10
|
-
|
|
11
|
-
## Installation
|
|
12
|
-
|
|
13
|
-
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.
|
|
14
|
-
|
|
15
|
-
```shell
|
|
16
|
-
npm i smath@1.8.5
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## Bugs and Requests
|
|
20
|
-
|
|
21
|
-
Is there a way we can make smath better? Please report all bugs, issues, and new feature requests to the [issues](https://github.com/nicfv/npm/issues) page in the [official repository](https://github.com/nicfv/npm). For critical security issues, please send an email to <smath@nicfv.com>.
|
|
22
|
-
|
|
23
|
-
## Contribute
|
|
24
|
-
|
|
25
|
-
Thank you for your interest in contributing to smath! smath is an open source software package maintained by Nicolas Ventura ([@nicfv](https://github.com/nicfv)) and built by users like you! You are allowed to fork the repository as permitted by the [MIT License](https://raw.githubusercontent.com/nicfv/npm/main/LICENSE) terms. Contributions are welcome by submitting a [pull request](https://github.com/nicfv/npm/pulls). Please follow the existing code styling if submitting a pull request. Thank you for your consideration!
|
|
26
1
|
## Getting Started
|
|
27
2
|
|
|
28
3
|
Small math? Simple math? Or supplemental math? Canonically, "SMath" is pronounced "smath" and stands for "small math (library.)" Similar to JavaScript's builtin [`Math`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math) object, `SMath` exports one global object with several math-related helper functions. There is no need to instantiate the class, just call functions directly. See the examples below to get started using SMath!
|
|
@@ -46,82 +21,3 @@ This example command returns the value 0.4.
|
|
|
46
21
|
```shell
|
|
47
22
|
npx smath normalize 4 0 10
|
|
48
23
|
```
|
|
49
|
-
## Examples
|
|
50
|
-
Here are a few quickstart examples written in JavaScript that showcase some out-of-box features of the `smath` package.
|
|
51
|
-
### JavaScript Math Oddities
|
|
52
|
-
Sometimes, JavaScript does weird math! Try adding `0.1 + 0.2` in your NodeJS terminal. What did you get?
|
|
53
|
-
|
|
54
|
-
> Hint: It's not 0.3!
|
|
55
|
-
|
|
56
|
-
The function `SMath.approx()` is an attempt to mitigate some of the issues that arise when doing arithmetic with non-whole numbers.
|
|
57
|
-
#### Instructions
|
|
58
|
-
1. Copy the source code
|
|
59
|
-
1. Paste into a new file
|
|
60
|
-
1. Save as `JavaScript-Math-Oddities.mjs`
|
|
61
|
-
1. Run this command in your terminal
|
|
62
|
-
```shell
|
|
63
|
-
node JavaScript-Math-Oddities.mjs
|
|
64
|
-
```
|
|
65
|
-
#### Source
|
|
66
|
-
```js
|
|
67
|
-
import { SMath } from 'smath';
|
|
68
|
-
|
|
69
|
-
// Determine the value of 0.1 + 0.2 using vanilla JavaScript and SMath
|
|
70
|
-
console.log('0.1 + 0.2 == 0.3 is ' + (0.1 + 0.2 == 0.3));
|
|
71
|
-
console.log('SMath.approx(0.1 + 0.2, 0.3) is ' + SMath.approx(0.1 + 0.2, 0.3));
|
|
72
|
-
```
|
|
73
|
-
#### Output
|
|
74
|
-
```text
|
|
75
|
-
0.1 + 0.2 == 0.3 is false
|
|
76
|
-
SMath.approx(0.1 + 0.2, 0.3) is true
|
|
77
|
-
```
|
|
78
|
-
### Temperature Conversion
|
|
79
|
-
This example demonstrates a simple temperature converter from Celsius to Fahrenheit, using `SMath.translate()` to linearly interpolate between unit systems. The translation uses freezing and boiling points to fix the bounds of the linear interpolation.
|
|
80
|
-
#### Instructions
|
|
81
|
-
1. Copy the source code
|
|
82
|
-
1. Paste into a new file
|
|
83
|
-
1. Save as `Temperature-Conversion.mjs`
|
|
84
|
-
1. Run this command in your terminal
|
|
85
|
-
```shell
|
|
86
|
-
node Temperature-Conversion.mjs
|
|
87
|
-
```
|
|
88
|
-
#### Source
|
|
89
|
-
```js
|
|
90
|
-
import { SMath } from 'smath';
|
|
91
|
-
|
|
92
|
-
// Water always freezes at the
|
|
93
|
-
// same temperature, but the
|
|
94
|
-
// units might be different.
|
|
95
|
-
// Define some constants to
|
|
96
|
-
// create two number ranges.
|
|
97
|
-
const C_Freeze = 0,
|
|
98
|
-
C_Boil = 100,
|
|
99
|
-
F_Freeze = 32,
|
|
100
|
-
F_Boil = 212;
|
|
101
|
-
|
|
102
|
-
// Use the `SMath` class to
|
|
103
|
-
// generate an array of five
|
|
104
|
-
// linearly spaced temperature
|
|
105
|
-
// values from 0 to 20.
|
|
106
|
-
const C = SMath.linspace(0, 20, 5);
|
|
107
|
-
|
|
108
|
-
// Use the `SMath` class to linearly
|
|
109
|
-
// interpolate the temperature in the
|
|
110
|
-
// C number range to a temperature
|
|
111
|
-
// in the F number range.
|
|
112
|
-
const F = C.map(c => SMath.translate(c, C_Freeze, C_Boil, F_Freeze, F_Boil));
|
|
113
|
-
|
|
114
|
-
// Print out each temperature
|
|
115
|
-
// in both units of C and F.
|
|
116
|
-
for (let i = 0; i < C.length; i++) {
|
|
117
|
-
console.log(C[i].toFixed() + 'C is ' + F[i].toFixed() + 'F')
|
|
118
|
-
}
|
|
119
|
-
```
|
|
120
|
-
#### Output
|
|
121
|
-
```text
|
|
122
|
-
0C is 32F
|
|
123
|
-
5C is 41F
|
|
124
|
-
10C is 50F
|
|
125
|
-
15C is 59F
|
|
126
|
-
20C is 68F
|
|
127
|
-
```
|
package/dist/bin.js
CHANGED
|
@@ -40,6 +40,7 @@ if (func.includes('help')) {
|
|
|
40
40
|
console.log(' rint <min> <max> : Generate a uniformly-distributed random integer, range inclusive');
|
|
41
41
|
console.log(' rnorm [mean] [stdev] : Generate a normally-distributed random float');
|
|
42
42
|
console.log(' rdist <n> [mean] [stdev] : Generate `n` normally-distributed random floats');
|
|
43
|
+
console.log(' rseq <min> <max> : Randomize a sequence of integers from `min` to `max`');
|
|
43
44
|
process.exit(1);
|
|
44
45
|
}
|
|
45
46
|
switch (func) {
|
|
@@ -131,6 +132,10 @@ switch (func) {
|
|
|
131
132
|
console.log(_1.SMath.rdist(nums[0], nums[1], nums[2]));
|
|
132
133
|
break;
|
|
133
134
|
}
|
|
135
|
+
case ('rseq'): {
|
|
136
|
+
console.log(_1.SMath.rseq(nums[0], nums[1]));
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
134
139
|
case (''): {
|
|
135
140
|
console.error('Missing argument. Use with "help" for a list of commands.');
|
|
136
141
|
process.exit(1);
|
package/dist/index.js
CHANGED
|
@@ -3,9 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SMath = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* @packageDocumentation
|
|
6
|
-
* Small math function library
|
|
7
|
-
*
|
|
8
|
-
* Exports the public-facing API for `smath`
|
|
6
|
+
*  Small math function library
|
|
9
7
|
*/
|
|
10
8
|
/**
|
|
11
9
|
* Contains a small math function library including
|
|
@@ -397,6 +395,27 @@ var SMath;
|
|
|
397
395
|
return distribution;
|
|
398
396
|
}
|
|
399
397
|
SMath.rdist = rdist;
|
|
398
|
+
/**
|
|
399
|
+
* Randomize a sequence of integers from `min` to `max`.
|
|
400
|
+
* @param min The minimum integer in the randomized sequence
|
|
401
|
+
* @param max The maximum integer in the randomized sequence
|
|
402
|
+
* @returns A randomized sequence of integers from `min` to `max`
|
|
403
|
+
* @example
|
|
404
|
+
* ```js
|
|
405
|
+
* const sequence = SMath.rseq(-2, 2); // [ 2, 0, 1, -2, -1 ]
|
|
406
|
+
* ```
|
|
407
|
+
*/
|
|
408
|
+
function rseq(min, max) {
|
|
409
|
+
min |= 0;
|
|
410
|
+
max |= 0;
|
|
411
|
+
max++;
|
|
412
|
+
var rawData = [];
|
|
413
|
+
for (var i = min; i < max; i++) {
|
|
414
|
+
rawData.push({ index: runif(-1, 1), value: i });
|
|
415
|
+
}
|
|
416
|
+
return rawData.sort(function (a, b) { return a.index - b.index; }).map(function (a) { return a.value; });
|
|
417
|
+
}
|
|
418
|
+
SMath.rseq = rseq;
|
|
400
419
|
/**
|
|
401
420
|
* Take the limit of a function. A return value of `NaN` indicates
|
|
402
421
|
* that no limit exists either due to a discontinuity or imaginary value.
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smath",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "Small math function library",
|
|
5
|
-
"homepage": "https://npm.nicfv.com/
|
|
5
|
+
"homepage": "https://npm.nicfv.com/",
|
|
6
6
|
"bin": "dist/bin.js",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"types": "types/index.d.ts",
|
|
@@ -11,11 +11,10 @@
|
|
|
11
11
|
"types"
|
|
12
12
|
],
|
|
13
13
|
"scripts": {
|
|
14
|
-
"build": "npm run postpack &&
|
|
15
|
-
"test": "tsc
|
|
16
|
-
"clean": "rm -rf node_modules package-lock.json && npm run postpack",
|
|
14
|
+
"build": "npm run postpack && tsc && node dist/test.js && rm dist/test.js types/test.d.ts",
|
|
15
|
+
"test": "tsc --noEmit",
|
|
17
16
|
"prepack": "npm run build",
|
|
18
|
-
"postpack": "rm -rf dist types
|
|
17
|
+
"postpack": "rm -rf dist types"
|
|
19
18
|
},
|
|
20
19
|
"keywords": [
|
|
21
20
|
"small",
|
|
@@ -56,8 +55,7 @@
|
|
|
56
55
|
"repository": "github:nicfv/npm",
|
|
57
56
|
"license": "MIT",
|
|
58
57
|
"devDependencies": {
|
|
59
|
-
"@types/node": "
|
|
60
|
-
"
|
|
61
|
-
"typedoc": "0.25.13"
|
|
58
|
+
"@types/node": "22.10.1",
|
|
59
|
+
"t6": "1.1.4"
|
|
62
60
|
}
|
|
63
61
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @packageDocumentation
|
|
3
|
-
* Small math function library
|
|
4
|
-
*
|
|
5
|
-
* Exports the public-facing API for `smath`
|
|
3
|
+
*  Small math function library
|
|
6
4
|
*/
|
|
7
5
|
/**
|
|
8
6
|
* Contains a small math function library including
|
|
@@ -263,6 +261,17 @@ export declare namespace SMath {
|
|
|
263
261
|
* ```
|
|
264
262
|
*/
|
|
265
263
|
function rdist(count: number, mean?: number, stdev?: number): Array<number>;
|
|
264
|
+
/**
|
|
265
|
+
* Randomize a sequence of integers from `min` to `max`.
|
|
266
|
+
* @param min The minimum integer in the randomized sequence
|
|
267
|
+
* @param max The maximum integer in the randomized sequence
|
|
268
|
+
* @returns A randomized sequence of integers from `min` to `max`
|
|
269
|
+
* @example
|
|
270
|
+
* ```js
|
|
271
|
+
* const sequence = SMath.rseq(-2, 2); // [ 2, 0, 1, -2, -1 ]
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
function rseq(min: number, max: number): Array<number>;
|
|
266
275
|
/**
|
|
267
276
|
* Take the limit of a function. A return value of `NaN` indicates
|
|
268
277
|
* that no limit exists either due to a discontinuity or imaginary value.
|