forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisPositive.js
More file actions
59 lines (52 loc) · 2.22 KB
/
isPositive.js
File metadata and controls
59 lines (52 loc) · 2.22 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
57
58
59
import { deepMap } from '../../utils/collection.js'
import { factory } from '../../utils/factory.js'
import { isPositiveNumber } from '../../plain/number/index.js'
import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual.js'
import { nearlyEqual } from '../../utils/number.js'
const name = 'isPositive'
const dependencies = ['typed', 'config']
export const createIsPositive = /* #__PURE__ */ factory(name, dependencies, ({ typed, config }) => {
/**
* Test whether a value is positive: larger 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.isPositive(x)
*
* Examples:
*
* math.isPositive(3) // returns true
* math.isPositive(-2) // returns false
* math.isPositive(0) // returns false
* math.isPositive(-0) // returns false
* math.isPositive(0.5) // returns true
* math.isPositive(math.bignumber(2)) // returns true
* math.isPositive(math.fraction(-2, 5)) // returns false
* math.isPositive(math.fraction(1, 3)) // returns true
* math.isPositive('2') // returns true
* math.isPositive([2, 0, -3]) // returns [true, false, false]
*
* See also:
*
* isNumeric, isZero, isNegative, 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 : isPositiveNumber(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 && x.n > 0n,
Unit: typed.referToSelf(self =>
x => typed.find(self, x.valueType())(x.value)),
'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self))
})
})