0

I have recently started learning code (Java), and have looked up the modulus operator on the Oracle website, as per the section 15.17.3. of the following link:

http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.3

Basically, if a is the denominator, and b is the nominator, it states that: (a/b) * b + (a%b) = a, which defines the modulus operator as: a%b = a – (a/b) * b.

I am confused because the equation simply does not work, which can be simplified as a%b = 0. I know my basic math algebra, so I am hoping if someone can enlighten me on how is it supposed to equal?

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
Sean
  • 508
  • 6
  • 20
  • There is no modulo operator in Java. It is a [remainder operator](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.3). – user207421 Sep 08 '15 at 02:53

1 Answers1

1

For integers, operator / means integral division, not mathematical one, for example:

7/2 = 3
6/2 = 3
5/2 = 2
4/2 = 2
3/2 = 1
2/2 = 1
1/2 = 0

In math terms, formula from java spec will be written like:

⌊a/b⌋ * b + (a%b) = a
a%b = a – ⌊a/b⌋ * b
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • 1
    Nice use of floor math symbol, but I believe that `⌊x⌋` is `Math.floor(x)`, which rounds towards negative infinity, while `a/b` rounds towards `0`. As long as `a` and `b` are positive, you're good though, and who in their right mind tries modulus on negative numbers anyway. – Andreas Sep 08 '15 at 01:59
  • That last equation really makes sense when you think about what the remainder operator actually does, and it's relationship with integral division. – xdhmoore Sep 08 '15 at 02:12
  • @Lashane, Thanks that was exactly the missing knowledge that I needed to understand the equation. problem solved :) – Sean Sep 09 '15 at 02:42