smath 1.13.3 → 1.14.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
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
- var _a, _b, _c, _d, _e;
3
+ var _a, _b, _c, _d;
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  var SMath = require(".");
6
6
  var func = ((_a = process.argv[2]) !== null && _a !== void 0 ? _a : '').toLowerCase(), nums = process.argv.slice(3).map(function (arg, i) {
@@ -147,11 +147,6 @@ switch (func) {
147
147
  console.log(SMath.mixed(nums[0], (_d = nums[1]) !== null && _d !== void 0 ? _d : 1e-6));
148
148
  break;
149
149
  }
150
- case ('tohex'): {
151
- console.warn('toHex() is deprecated!');
152
- console.log(SMath.toHex(nums[0], (_e = nums[1]) !== null && _e !== void 0 ? _e : 0));
153
- break;
154
- }
155
150
  case (''): {
156
151
  console.error('Missing argument. Use with "help" for a list of commands.');
157
152
  process.exit(1);
package/dist/index.js CHANGED
@@ -40,7 +40,6 @@ exports.differentiate = differentiate;
40
40
  exports.integrate = integrate;
41
41
  exports.rat = rat;
42
42
  exports.mixed = mixed;
43
- exports.toHex = toHex;
44
43
  /**
45
44
  * @packageDocumentation
46
45
  * Small math function library
@@ -407,8 +406,8 @@ function shuffle(stack) {
407
406
  * const y = SMath.lim(Math.log, 0); // -Infinity
408
407
  */
409
408
  function lim(f, x, h, discontinuity_cutoff) {
410
- if (h === void 0) { h = 1e-3; }
411
- if (discontinuity_cutoff === void 0) { discontinuity_cutoff = 1; }
409
+ if (h === void 0) { h = 1e-6; }
410
+ if (discontinuity_cutoff === void 0) { discontinuity_cutoff = 1e-3; }
412
411
  var center = f(x), left1 = f(x - h), left2 = f(x - h / 2), right1 = f(x + h), right2 = f(x + h / 2);
413
412
  var left, right;
414
413
  if (Number.isFinite(center)) {
@@ -416,46 +415,46 @@ function lim(f, x, h, discontinuity_cutoff) {
416
415
  }
417
416
  // Check the limit approaching from the left
418
417
  if (Number.isFinite(left1) && Number.isFinite(left2)) {
419
- if (left2 > left1 + 2 * h) {
420
- left = Infinity;
418
+ if (approx(left1, left2, discontinuity_cutoff)) {
419
+ left = left2; // Converges
421
420
  }
422
- else if (left2 < left1 - 2 * h) {
423
- left = -Infinity;
421
+ else if (left1 > 0 && left2 > left1) {
422
+ left = Infinity; // Diverges to +inf
423
+ }
424
+ else if (left1 < 0 && left2 < left1) {
425
+ left = -Infinity; // Diverges to -inf
424
426
  }
425
427
  else {
426
- left = avg([left1, left2]);
428
+ left = NaN; // Diverges
427
429
  }
428
430
  }
429
- else if (left1 === left2) { // Handles +/-Infinity case
430
- left = left1;
431
- }
432
431
  else {
433
- left = NaN;
432
+ left = NaN; // Discontinuous
434
433
  }
435
434
  // Check the limit approaching from the right
436
435
  if (Number.isFinite(right1) && Number.isFinite(right2)) {
437
- if (right2 > right1 + 2 * h) {
438
- right = Infinity;
436
+ if (approx(right1, right2, discontinuity_cutoff)) {
437
+ right = right2; // Converges
439
438
  }
440
- else if (right2 < right1 - 2 * h) {
441
- right = -Infinity;
439
+ else if (right1 > 0 && right2 > right1) {
440
+ right = Infinity; // Diverges to +inf
441
+ }
442
+ else if (right1 < 0 && right2 < right1) {
443
+ right = -Infinity; // Diverges to -inf
442
444
  }
443
445
  else {
444
- right = avg([right1, right2]);
446
+ right = NaN; // Diverges
445
447
  }
446
448
  }
447
- else if (right1 === right2) { // Handles +/-Infinity case
448
- right = right1;
449
- }
450
449
  else {
451
- right = NaN;
450
+ right = NaN; // Discontinuous
452
451
  }
453
452
  // Check if limits match or are close
454
453
  if (left === right) { // Handles +/-Infinity case
455
454
  return left;
456
455
  }
457
- else if (approx(left, right, discontinuity_cutoff)) {
458
- return avg([left, right]);
456
+ else if (Number.isNaN(left) && Number.isNaN(right)) {
457
+ return center;
459
458
  }
460
459
  else if (!Number.isNaN(left) && Number.isNaN(right)) {
461
460
  return left;
@@ -463,6 +462,9 @@ function lim(f, x, h, discontinuity_cutoff) {
463
462
  else if (Number.isNaN(left) && !Number.isNaN(right)) {
464
463
  return right;
465
464
  }
465
+ else if (approx(left, right, discontinuity_cutoff)) {
466
+ return avg([left, right]);
467
+ }
466
468
  else {
467
469
  return NaN;
468
470
  }
@@ -471,14 +473,14 @@ function lim(f, x, h, discontinuity_cutoff) {
471
473
  * Take the derivative of a function.
472
474
  * @param f Function `f(x)`
473
475
  * @param x The x-value where to evaluate the derivative
474
- * @param h Small step value
476
+ * @param epsilon Small step value
475
477
  * @returns `f'(x)`
476
478
  * @example
477
479
  * const y = SMath.differentiate(x => 3 * x ** 2, 2); // 12
478
480
  */
479
- function differentiate(f, x, h) {
480
- if (h === void 0) { h = 1e-3; }
481
- return (f(x + h) - f(x - h)) / (2 * h);
481
+ function differentiate(f, x, epsilon) {
482
+ if (epsilon === void 0) { epsilon = 1e-6; }
483
+ return lim(function (h) { return (f(x + h) - f(x - h)) / (2 * h); }, 0, epsilon);
482
484
  }
483
485
  /**
484
486
  * Compute the definite integral of a function.
@@ -491,7 +493,7 @@ function differentiate(f, x, h) {
491
493
  * const y = SMath.integrate(x => 3 * x ** 2, 1, 2); // 7
492
494
  */
493
495
  function integrate(f, a, b, Ndx) {
494
- if (Ndx === void 0) { Ndx = 1e3; }
496
+ if (Ndx === void 0) { Ndx = 1e6; }
495
497
  return ((b - a) / Ndx) * sum(linspace(a, b, Ndx).map(function (x) { return f(x); }));
496
498
  }
497
499
  /**
@@ -531,16 +533,3 @@ function mixed(n, epsilon) {
531
533
  if (epsilon === void 0) { epsilon = 1e-6; }
532
534
  return __assign({ whole: n | 0 }, rat(n < -1 ? (n | 0) - n : n - (n | 0), epsilon));
533
535
  }
534
- /**
535
- * Convert any number to its hexadecimal equivalent.
536
- * @param n A decimal number to convert
537
- * @param length The minimum number of digits to show
538
- * @returns The number `n` converted to hexadecimal
539
- * @example
540
- * const hex = SMath.toHex(10, 2); // '0A'
541
- * @deprecated Use native `number.toString(16)`
542
- */
543
- function toHex(n, length) {
544
- if (length === void 0) { length = 0; }
545
- return (n < 0 ? '-' : '') + (n < 0 ? -n : n).toString(16).padStart(length, '0').toUpperCase();
546
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smath",
3
- "version": "1.13.3",
3
+ "version": "1.14.0",
4
4
  "description": "Small math function library",
5
5
  "homepage": "https://npm.nicfv.com/",
6
6
  "bin": "dist/bin.js",
@@ -52,7 +52,7 @@
52
52
  "repository": "github:nicfv/npm",
53
53
  "license": "MIT",
54
54
  "devDependencies": {
55
- "@types/node": "24.3.0",
55
+ "@types/node": "25.0.10",
56
56
  "t6": "1.2.1"
57
57
  }
58
58
  }
package/types/index.d.ts CHANGED
@@ -249,12 +249,12 @@ export declare function lim(f: (x: number) => number, x: number, h?: number, dis
249
249
  * Take the derivative of a function.
250
250
  * @param f Function `f(x)`
251
251
  * @param x The x-value where to evaluate the derivative
252
- * @param h Small step value
252
+ * @param epsilon Small step value
253
253
  * @returns `f'(x)`
254
254
  * @example
255
255
  * const y = SMath.differentiate(x => 3 * x ** 2, 2); // 12
256
256
  */
257
- export declare function differentiate(f: (x: number) => number, x: number, h?: number): number;
257
+ export declare function differentiate(f: (x: number) => number, x: number, epsilon?: number): number;
258
258
  /**
259
259
  * Compute the definite integral of a function.
260
260
  * @param f Function `f(x)`
@@ -294,13 +294,3 @@ export declare function mixed(n: number, epsilon?: number): {
294
294
  num: number;
295
295
  den: number;
296
296
  };
297
- /**
298
- * Convert any number to its hexadecimal equivalent.
299
- * @param n A decimal number to convert
300
- * @param length The minimum number of digits to show
301
- * @returns The number `n` converted to hexadecimal
302
- * @example
303
- * const hex = SMath.toHex(10, 2); // '0A'
304
- * @deprecated Use native `number.toString(16)`
305
- */
306
- export declare function toHex(n: number, length?: number): string;