liveBook · Manning

Mpz_mod

As explained in Clojure: The Essential Reference

mod is a Clojure function that returns the modulus of two numbers, with sign behavior determined by the divisor.

mod returns the modulus left after dividing one number by another. It resembles rem for positive values, but handles negative numbers according to the divisor’s sign behavior. section 5.7

Use mod with numeric arguments and account for its contract:

  • Call it as (mod num div), where both arguments must be numbers. section 5.7
  • Expect an ArithmeticException when div is zero. section 5.7
  • Expect a double result when either argument is a double. section 5.7
  • Prefer mod when negative values should produce a result consistent with the divisor rather than rem’s sign behavior. section 5.7

Wrapping a value

A modular index wraps the alphabet from its final element back to its first: (mod 26 26) returns 0, allowing (++ "z") to produce "a" when used with an alphabet lookup.

How Clojure: The Essential Reference covers it

How deep it goes
Defines inputs, outputs, numeric types, zero-divisor failure, and negative-number differences while deferring detailed negative arithmetic.
Examples
Uses alphabet rotation, including wrapping z to a, to demonstrate modular indexing.
What it assumes
Assumes familiarity with numeric division and introduces the distinction among quotient, remainder, and modulus.
What you can do after
Apply mod in Clojure functions that require divisor-aware remainders or cyclic indexes.
How it connects
Connects mod with Euclidean division, quot, rem, numeric types, and index wrapping.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.