mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif
58 lines (51 loc) • 1.82 kB
JavaScript
import { deepMap } from '../../utils/collection'
import { factory } from '../../utils/factory'
import { isNegativeNumber } from '../../plain/number'
const name = 'isNegative'
const dependencies = ['typed']
export const createIsNegative = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
/**
* Test whether a value is negative: smaller than zero.
* The function supports types `number`, `BigNumber`, `Fraction`, and `Unit`.
*
* The function is evaluated element-wise in case of Array or Matrix input.
*
* Syntax:
*
* math.isNegative(x)
*
* Examples:
*
* math.isNegative(3) // returns false
* math.isNegative(-2) // returns true
* math.isNegative(0) // returns false
* math.isNegative(-0) // returns false
* math.isNegative(math.bignumber(2)) // returns false
* math.isNegative(math.fraction(-2, 5)) // returns true
* math.isNegative('-2') // returns true
* math.isNegative([2, 0, -3]') // returns [false, false, true]
*
* See also:
*
* isNumeric, isPositive, isZero, isInteger
*
* @param {number | BigNumber | Fraction | Unit | Array | Matrix} x Value to be tested
* @return {boolean} Returns true when `x` is larger than zero.
* Throws an error in case of an unknown data type.
*/
return typed(name, {
number: isNegativeNumber,
BigNumber: function (x) {
return x.isNeg() && !x.isZero() && !x.isNaN()
},
Fraction: function (x) {
return x.s < 0 // It's enough to decide on the sign
},
Unit: function (x) {
return this(x.value)
},
'Array | Matrix': function (x) {
return deepMap(x, this)
}
})
})