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