forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisNegative.js
More file actions
56 lines (49 loc) · 2.11 KB
/
isNegative.js
File metadata and controls
56 lines (49 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { deepMap } from '../../utils/collection.js'
import { factory } from '../../utils/factory.js'
import { isNegativeNumber } from '../../plain/number/index.js'
import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual.js'
import { nearlyEqual } from '../../utils/number.js'
const name = 'isNegative'
const dependencies = ['typed', 'config']
export const createIsNegative = /* #__PURE__ */ factory(name, dependencies, ({ typed, config }) => {
/**
* 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 | bigint | 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: x => nearlyEqual(x, 0, config.relTol, config.absTol) ? false : isNegativeNumber(x),
BigNumber: x => bigNearlyEqual(x, new x.constructor(0), config.relTol, config.absTol)
? false
: x.isNeg() && !x.isZero() && !x.isNaN(),
bigint: x => x < 0n,
Fraction: x => x.s < 0n, // It's enough to decide on the sign
Unit: typed.referToSelf(self =>
x => typed.find(self, x.valueType())(x.value)),
'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self))
})
})