smath 1.11.0 → 1.12.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.
package/dist/bin.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
- var _a, _b, _c, _d;
3
+ var _a, _b, _c, _d, _e;
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  var _1 = require(".");
6
6
  var func = ((_a = process.argv[2]) !== null && _a !== void 0 ? _a : '').toLowerCase(), nums = process.argv.slice(3).map(function (arg, i) {
@@ -27,6 +27,7 @@ if (func.includes('help')) {
27
27
  console.log(' logspace <min> <max> <n> : Generate `n` logarithmically spaced numbers between `min` and `max`');
28
28
  console.log(' factorial <n> : Compute `n!` (factorial)');
29
29
  console.log(' factors <n> : List the prime factors of `n`');
30
+ console.log(' round2 <n> <base> : Round `n` to a multiple of any `base`');
30
31
  console.log(' error <exp> <act> : Calculate the normaized percent error between `exp` and `act`');
31
32
  console.log(' sum <c0> [c1] ... [cn] : Compute a total of `n` numbers');
32
33
  console.log(' prod <c0> [c1] ... [cn] : Compute a product of `n` numbers');
@@ -42,6 +43,7 @@ if (func.includes('help')) {
42
43
  console.log(' rdist <n> [mean] [stdev] : Generate `n` normally-distributed random floats');
43
44
  console.log(' rat <n> [eps] : Decompose `n` into a ratio');
44
45
  console.log(' mixed <n> [eps] : Decompose `n` into a mixed number');
46
+ console.log(' toHex <n> [length] : Convert decimal `n` into hexadecimal');
45
47
  process.exit(1);
46
48
  }
47
49
  switch (func) {
@@ -81,6 +83,10 @@ switch (func) {
81
83
  console.log(_1.SMath.factors(nums[0]));
82
84
  break;
83
85
  }
86
+ case ('round2'): {
87
+ console.log(_1.SMath.round2(nums[0], nums[1]));
88
+ break;
89
+ }
84
90
  case ('error'): {
85
91
  console.log(_1.SMath.error(nums[0], nums[1]));
86
92
  break;
@@ -141,6 +147,10 @@ switch (func) {
141
147
  console.log(_1.SMath.mixed(nums[0], (_d = nums[1]) !== null && _d !== void 0 ? _d : 1e-6));
142
148
  break;
143
149
  }
150
+ case ('tohex'): {
151
+ console.log(_1.SMath.toHex(nums[0], (_e = nums[1]) !== null && _e !== void 0 ? _e : 0));
152
+ break;
153
+ }
144
154
  case (''): {
145
155
  console.error('Missing argument. Use with "help" for a list of commands.');
146
156
  process.exit(1);
package/dist/index.js CHANGED
@@ -17,6 +17,7 @@ exports.SMath = void 0;
17
17
  * Small math function library
18
18
  *
19
19
  * ![NPM Downloads](https://img.shields.io/npm/d18m/smath)
20
+ * ![NPM Last Update](https://img.shields.io/npm/last-update/smath)
20
21
  */
21
22
  /**
22
23
  * Contains a small math function library including
@@ -204,6 +205,21 @@ var SMath;
204
205
  return f;
205
206
  }
206
207
  SMath.factors = factors;
208
+ /**
209
+ * Round a number to the nearest multiple of an arbitrary
210
+ * base. Does not round when the base is set to zero.
211
+ * @param n Any number to round
212
+ * @param base Any base to round to
213
+ * @returns `n` rounded to the nearest multiple of `base`
214
+ * @example
215
+ * ```js
216
+ * const y = SMath.round2(Math.PI, 0.2); // 3.2
217
+ * ```
218
+ */
219
+ function round2(n, base) {
220
+ return base ? base * Math.round(n / base) : n;
221
+ }
222
+ SMath.round2 = round2;
207
223
  /**
208
224
  * Calculate the relative normalized error or deviation from any
209
225
  * value to an accepted value. An error of 0 indicates that the
@@ -576,4 +592,19 @@ var SMath;
576
592
  return __assign({ whole: n | 0 }, rat(n < -1 ? (n | 0) - n : n - (n | 0), epsilon));
577
593
  }
578
594
  SMath.mixed = mixed;
595
+ /**
596
+ * Convert any number to its hexadecimal equivalent.
597
+ * @param n A decimal number to convert
598
+ * @param length The minimum number of digits to show
599
+ * @returns The number `n` converted to hexadecimal
600
+ * @example
601
+ * ```js
602
+ * const hex = SMath.toHex(10, 2); // '0A'
603
+ * ```
604
+ */
605
+ function toHex(n, length) {
606
+ if (length === void 0) { length = 0; }
607
+ return (n < 0 ? '-' : '') + (n < 0 ? -n : n).toString(16).padStart(length, '0').toUpperCase();
608
+ }
609
+ SMath.toHex = toHex;
579
610
  })(SMath || (exports.SMath = SMath = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smath",
3
- "version": "1.11.0",
3
+ "version": "1.12.1",
4
4
  "description": "Small math function library",
5
5
  "homepage": "https://npm.nicfv.com/",
6
6
  "bin": "dist/bin.js",
package/types/index.d.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  * Small math function library
4
4
  *
5
5
  * ![NPM Downloads](https://img.shields.io/npm/d18m/smath)
6
+ * ![NPM Last Update](https://img.shields.io/npm/last-update/smath)
6
7
  */
7
8
  /**
8
9
  * Contains a small math function library including
@@ -120,6 +121,18 @@ export declare namespace SMath {
120
121
  * ```
121
122
  */
122
123
  function factors(n: number): Array<number>;
124
+ /**
125
+ * Round a number to the nearest multiple of an arbitrary
126
+ * base. Does not round when the base is set to zero.
127
+ * @param n Any number to round
128
+ * @param base Any base to round to
129
+ * @returns `n` rounded to the nearest multiple of `base`
130
+ * @example
131
+ * ```js
132
+ * const y = SMath.round2(Math.PI, 0.2); // 3.2
133
+ * ```
134
+ */
135
+ function round2(n: number, base: number): number;
123
136
  /**
124
137
  * Calculate the relative normalized error or deviation from any
125
138
  * value to an accepted value. An error of 0 indicates that the
@@ -344,4 +357,15 @@ export declare namespace SMath {
344
357
  num: number;
345
358
  den: number;
346
359
  };
360
+ /**
361
+ * Convert any number to its hexadecimal equivalent.
362
+ * @param n A decimal number to convert
363
+ * @param length The minimum number of digits to show
364
+ * @returns The number `n` converted to hexadecimal
365
+ * @example
366
+ * ```js
367
+ * const hex = SMath.toHex(10, 2); // '0A'
368
+ * ```
369
+ */
370
+ function toHex(n: number, length?: number): string;
347
371
  }