Modulo operation

From Maths
Jump to: navigation, search

Definition

For [ilmath]a\in\mathbb{N}_0[/ilmath] and [ilmath]b\in\mathbb{N} [/ilmath] "[ilmath]a[/ilmath] modulo [ilmath]b[/ilmath]", written usually as a%b in most programming languages or using a mod(a,b) function is the remainder of dividing [ilmath]a[/ilmath] by [ilmath]b[/ilmath].

Implementation

I write this page because I have used:

  • [math]a\% b\eq \Bigg(a-b\cdot[/math][math]\text{Floor} [/math][math]{\left(\frac{a}{b}\right)}\Bigg) [/math]
    • Caveat:This only works for non-negative values!
      TODO: Proof?

See also

Extensions

  • What about negative numbers?
  • [ilmath]2.5[/ilmath] mod [ilmath]1[/ilmath] is safely and obviously [ilmath]0.5[/ilmath] - this is quite common.

References