0

With the function:

int five(int n)
{
    if ((n%5)==0)
        return 1;
    else
        return 0;
}

Why are the limitations of this positive numbers only even if there is no remainder?

Tushar Joshi
  • 55
  • 1
  • 2
  • 7

1 Answers1

2

for n == -2, some hardware will compute n%5 as 3, while other hardware evaluates it as 2. To accommodate that , the standard leaves % ambiguous for negative values.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • 4
    “the standard leaves % ambiguous for negative values.” Not since C99. Division rounds towards zero and modulo is defined such that `(a/b)*b + a%b` equals `a`. (6.5.5:6) – Pascal Cuoq Oct 12 '12 at 18:51
  • by mathematical definition -2%5 == 3 == (-2+5)%5, http://mathforum.org/library/drmath/view/52343.html – 0x90 Oct 12 '12 at 18:55
  • 2
    @0x90 `%` operator in C is a remainder and not a modulo operator. – ouah Oct 12 '12 at 18:58