UNPKG

1.99 kBJavaScriptView Raw
1'use strict'
2
3const deepMap = require('../../utils/collection/deepMap')
4const nearlyEqual = require('../../utils/number').nearlyEqual
5const bigNearlyEqual = require('../../utils/bignumber/nearlyEqual')
6
7function factory (type, config, load, typed) {
8 const round = load(require('../../function/arithmetic/round'))
9
10 /**
11 * Round a value towards plus infinity
12 * If `x` is complex, both real and imaginary part are rounded towards plus infinity.
13 * For matrices, the function is evaluated element wise.
14 *
15 * Syntax:
16 *
17 * math.ceil(x)
18 *
19 * Examples:
20 *
21 * math.ceil(3.2) // returns number 4
22 * math.ceil(3.8) // returns number 4
23 * math.ceil(-4.2) // returns number -4
24 * math.ceil(-4.7) // returns number -4
25 *
26 * const c = math.complex(3.2, -2.7)
27 * math.ceil(c) // returns Complex 4 - 2i
28 *
29 * math.ceil([3.2, 3.8, -4.7]) // returns Array [4, 4, -4]
30 *
31 * See also:
32 *
33 * floor, fix, round
34 *
35 * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded
36 * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
37 */
38 const ceil = typed('ceil', {
39 'number': function (x) {
40 if (nearlyEqual(x, round(x), config.epsilon)) {
41 return round(x)
42 } else {
43 return Math.ceil(x)
44 }
45 },
46
47 'Complex': function (x) {
48 return x.ceil()
49 },
50
51 'BigNumber': function (x) {
52 if (bigNearlyEqual(x, round(x), config.epsilon)) {
53 return round(x)
54 } else {
55 return x.ceil()
56 }
57 },
58
59 'Fraction': function (x) {
60 return x.ceil()
61 },
62
63 'Array | Matrix': function (x) {
64 // deep map collection, skip zeros since ceil(0) = 0
65 return deepMap(x, ceil, true)
66 }
67 })
68
69 ceil.toTex = { 1: `\\left\\lceil\${args[0]}\\right\\rceil` }
70
71 return ceil
72}
73
74exports.name = 'ceil'
75exports.factory = factory