UNPKG

1.33 kBJavaScriptView Raw
1'use strict'
2
3const deepMap = require('../../utils/collection/deepMap')
4const bigBitNot = require('../../utils/bignumber/bitNot')
5const isInteger = require('../../utils/number').isInteger
6
7function factory (type, config, load, typed) {
8 const latex = require('../../utils/latex')
9
10 /**
11 * Bitwise NOT value, `~x`.
12 * For matrices, the function is evaluated element wise.
13 * For units, the function is evaluated on the best prefix base.
14 *
15 * Syntax:
16 *
17 * math.bitNot(x)
18 *
19 * Examples:
20 *
21 * math.bitNot(1) // returns number -2
22 *
23 * math.bitNot([2, -3, 4]) // returns Array [-3, 2, 5]
24 *
25 * See also:
26 *
27 * bitAnd, bitOr, bitXor, leftShift, rightArithShift, rightLogShift
28 *
29 * @param {number | BigNumber | Array | Matrix} x Value to not
30 * @return {number | BigNumber | Array | Matrix} NOT of `x`
31 */
32 const bitNot = typed('bitNot', {
33 'number': function (x) {
34 if (!isInteger(x)) {
35 throw new Error('Integer expected in function bitNot')
36 }
37
38 return ~x
39 },
40
41 'BigNumber': bigBitNot,
42
43 'Array | Matrix': function (x) {
44 return deepMap(x, bitNot)
45 }
46 })
47
48 bitNot.toTex = {
49 1: latex.operators['bitNot'] + `\\left(\${args[0]}\\right)`
50 }
51
52 return bitNot
53}
54
55exports.name = 'bitNot'
56exports.factory = factory