You're viewing version 1.2.0 of mod. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16
  • (mod num div)
Modulus of num and div. Truncates toward negative infinity.

1 Example top

  • user=> (mod 10 5)
    user=> (mod 10 6)
    user=> (mod 10 10)
    user=> (mod 10 -1)
    ;; The mod function is defined as the amount by which a number exceeds the largest integer multiple of the divisor that is not greater than that number.
    ;; The largest integer multiple of 5 not greater than -2 is 5 * -1 = -5. The amount by which -2 exceeds -5 is 3. 
    user=> (mod -2  5) 
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:2734 top

(defn mod
  "Modulus of num and div. Truncates toward negative infinity."
  {:added "1.0"}
  [num div] 
  (let [m (rem num div)] 
    (if (or (zero? m) (pos? (* num div))) 
      (+ m div))))
Vars in clojure.core/mod: * + defn let num or pos? zero?
Used in 1 other vars gcd

Comments top

1 comment(s) for mod.

The difference between rem and mod can be remembered by noting that mod always returns a value between 0 and div.

Log in to add a comment.