isNegative.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createIsNegative = void 0;
  6. var _collection = require("../../utils/collection.js");
  7. var _factory = require("../../utils/factory.js");
  8. var _index = require("../../plain/number/index.js");
  9. var name = 'isNegative';
  10. var dependencies = ['typed'];
  11. var createIsNegative = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  12. var typed = _ref.typed;
  13. /**
  14. * Test whether a value is negative: smaller than zero.
  15. * The function supports types `number`, `BigNumber`, `Fraction`, and `Unit`.
  16. *
  17. * The function is evaluated element-wise in case of Array or Matrix input.
  18. *
  19. * Syntax:
  20. *
  21. * math.isNegative(x)
  22. *
  23. * Examples:
  24. *
  25. * math.isNegative(3) // returns false
  26. * math.isNegative(-2) // returns true
  27. * math.isNegative(0) // returns false
  28. * math.isNegative(-0) // returns false
  29. * math.isNegative(math.bignumber(2)) // returns false
  30. * math.isNegative(math.fraction(-2, 5)) // returns true
  31. * math.isNegative('-2') // returns true
  32. * math.isNegative([2, 0, -3]) // returns [false, false, true]
  33. *
  34. * See also:
  35. *
  36. * isNumeric, isPositive, isZero, isInteger
  37. *
  38. * @param {number | BigNumber | Fraction | Unit | Array | Matrix} x Value to be tested
  39. * @return {boolean} Returns true when `x` is larger than zero.
  40. * Throws an error in case of an unknown data type.
  41. */
  42. return typed(name, {
  43. number: _index.isNegativeNumber,
  44. BigNumber: function BigNumber(x) {
  45. return x.isNeg() && !x.isZero() && !x.isNaN();
  46. },
  47. Fraction: function Fraction(x) {
  48. return x.s < 0; // It's enough to decide on the sign
  49. },
  50. Unit: typed.referToSelf(function (self) {
  51. return function (x) {
  52. return typed.find(self, x.valueType())(x.value);
  53. };
  54. }),
  55. 'Array | Matrix': typed.referToSelf(function (self) {
  56. return function (x) {
  57. return (0, _collection.deepMap)(x, self);
  58. };
  59. })
  60. });
  61. });
  62. exports.createIsNegative = createIsNegative;