smath 1.11.0 → 1.12.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/dist/bin.js CHANGED
@@ -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');
@@ -81,6 +82,10 @@ switch (func) {
81
82
  console.log(_1.SMath.factors(nums[0]));
82
83
  break;
83
84
  }
85
+ case ('round2'): {
86
+ console.log(_1.SMath.round2(nums[0], nums[1]));
87
+ break;
88
+ }
84
89
  case ('error'): {
85
90
  console.log(_1.SMath.error(nums[0], nums[1]));
86
91
  break;
package/dist/index.js CHANGED
@@ -204,6 +204,21 @@ var SMath;
204
204
  return f;
205
205
  }
206
206
  SMath.factors = factors;
207
+ /**
208
+ * Round a number to the nearest multiple of an arbitrary
209
+ * base. Does not round when the base is set to zero.
210
+ * @param n Any number to round
211
+ * @param base Any base to round to
212
+ * @returns `n` rounded to the nearest multiple of `base`
213
+ * @example
214
+ * ```js
215
+ * const y = SMath.round2(Math.PI, 0.2); // 3.2
216
+ * ```
217
+ */
218
+ function round2(n, base) {
219
+ return base ? base * Math.round(n / base) : n;
220
+ }
221
+ SMath.round2 = round2;
207
222
  /**
208
223
  * Calculate the relative normalized error or deviation from any
209
224
  * value to an accepted value. An error of 0 indicates that the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smath",
3
- "version": "1.11.0",
3
+ "version": "1.12.0",
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
@@ -120,6 +120,18 @@ export declare namespace SMath {
120
120
  * ```
121
121
  */
122
122
  function factors(n: number): Array<number>;
123
+ /**
124
+ * Round a number to the nearest multiple of an arbitrary
125
+ * base. Does not round when the base is set to zero.
126
+ * @param n Any number to round
127
+ * @param base Any base to round to
128
+ * @returns `n` rounded to the nearest multiple of `base`
129
+ * @example
130
+ * ```js
131
+ * const y = SMath.round2(Math.PI, 0.2); // 3.2
132
+ * ```
133
+ */
134
+ function round2(n: number, base: number): number;
123
135
  /**
124
136
  * Calculate the relative normalized error or deviation from any
125
137
  * value to an accepted value. An error of 0 indicates that the