mathMod.js 1.3 KB

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