UNPKG

1.35 kBJavaScriptView Raw
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2");
4
5var _isInteger =
6/*#__PURE__*/
7require("./internal/_isInteger");
8/**
9 * `mathMod` behaves like the modulo operator should mathematically, unlike the
10 * `%` operator (and by extension, [`R.modulo`](#modulo)). So while
11 * `-17 % 5` is `-2`, `mathMod(-17, 5)` is `3`. `mathMod` requires Integer
12 * arguments, and returns NaN when the modulus is zero or negative.
13 *
14 * @func
15 * @memberOf R
16 * @since v0.3.0
17 * @category Math
18 * @sig Number -> Number -> Number
19 * @param {Number} m The dividend.
20 * @param {Number} p the modulus.
21 * @return {Number} The result of `b mod a`.
22 * @see R.modulo
23 * @example
24 *
25 * R.mathMod(-17, 5); //=> 3
26 * R.mathMod(17, 5); //=> 2
27 * R.mathMod(17, -5); //=> NaN
28 * R.mathMod(17, 0); //=> NaN
29 * R.mathMod(17.2, 5); //=> NaN
30 * R.mathMod(17, 5.3); //=> NaN
31 *
32 * const clock = R.mathMod(R.__, 12);
33 * clock(15); //=> 3
34 * clock(24); //=> 0
35 *
36 * const seventeenMod = R.mathMod(17);
37 * seventeenMod(3); //=> 2
38 * seventeenMod(4); //=> 1
39 * seventeenMod(10); //=> 7
40 */
41
42
43var mathMod =
44/*#__PURE__*/
45_curry2(function mathMod(m, p) {
46 if (!_isInteger(m)) {
47 return NaN;
48 }
49
50 if (!_isInteger(p) || p < 1) {
51 return NaN;
52 }
53
54 return (m % p + p) % p;
55});
56
57module.exports = mathMod;
\No newline at end of file