-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
math.js
136 lines (118 loc) · 2.16 KB
/
math.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict';
const utils = require('../utils');
/**
* Return the product of `a` plus `b`.
*
* ```js
* <%= add(1, 2) %>
* //=> '3'
* ```
* @param {Number} `a`
* @param {Number} `b`
* @api public
*/
exports.add = (a, b) => a + b;
/**
* Subtract `b` from `a`.
*
* ```js
* <%= subtract(5, 2) %>
* //=> '3'
* ```
* @param {Number} `a`
* @param {Number} `b`
* @api public
*/
exports.subtract = (a, b) => Number(a) - Number(b);
/**
* Divide `a` (the numerator) by `b` (the divisor).
*
* ```js
* <%= divide(10, 2) %>
* //=> '5'
* ```
* @param {Number} `a` the numerator.
* @param {Number} `b` the divisor.
* @return {Number} The quotient of `a` divided by `b`.
* @api public
*/
exports.divide = (a, b) => Number(a) / Number(b);
/**
* Multiply `a` by `b`.
*
* ```js
* <%= divide(10, 2) %>
* //=> '5'
* ```
* @param {Number} `a`
* @param {Number} `b`
* @return {Number} The product of `a` times `b`.
* @api public
*/
exports.multiply = (a, b) => Number(a) * Number(b);
/**
* Returns the largest integer less than or equal to the
* given `number`.
*
* ```js
* <%= floor(10.6) %>
* //=> '10'
* ```
* @param {Number} `number`
* @return {Number}
* @api public
*/
exports.floor = n => Math.floor(n);
/**
* Returns the smallest integer greater than or equal to the
* given `number`.
*
* ```js
* <%= ceil(10.1) %>
* //=> '11'
* ```
* @param {Number} `number`
* @return {Number}
* @api public
*/
exports.ceil = n => Math.ceil(n);
/**
* Returns the value of the given `number` rounded to the
* nearest integer.
*
* ```js
* <%= round(10.1) %>
* //=> '10'
*
* <%= round(10.5) %>
* //=> '11'
* ```
* @param {Number} `number`
* @return {Number}
* @api public
*/
exports.round = n => Math.round(n);
/**
* Returns the sum of all numbers in the given array.
*
* ```js
* <%= sum([1, 2, 3, 4, 5]) %>
* //=> '15'
* ```
* @param {Number} `number`
* @return {Number}
* @api public
*/
exports.sum = (...args) => {
let arr = [].concat.apply([], args);
let len = arr.length;
let idx = -1;
let num = 0;
while (++idx < len) {
if (!utils.isNumber(arr[idx])) {
continue;
}
num += (+arr[idx]);
}
return num;
};