isNegative.js 1.9 KB

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