modulus on negative numbers 
Author Message
 modulus on negative numbers

Is the result of using the % operator on negative numbers
implementation defined in C89/C90?

AFAIK it is well defined in C99 because of the
(a/b)*b + a%b == a always true guarantee.

--
Thomas Stegen.



Tue, 05 Apr 2005 02:23:53 GMT  
 modulus on negative numbers

Quote:

> Is the result of using the % operator on negative numbers
> implementation defined in C89/C90?

> AFAIK it is well defined in C99 because of the
> (a/b)*b + a%b == a always true guarantee.

The 1988 draft says: "If either operand is negative, whether the result
of the / operator is the largest integer less than the algebraic
quotient or the smallest integer greater than the algebraic quotient is
implementation-defined, as is the sign of the result of the % operator."

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Tue, 05 Apr 2005 03:59:31 GMT  
 modulus on negative numbers
On Thu, 17 Oct 2002 20:23:53 +0200, Thomas Stegen

Quote:

>Is the result of using the % operator on negative numbers
>implementation defined in C89/C90?

Yes.

Quote:
>AFAIK it is well defined in C99 because of the
>(a/b)*b + a%b == a always true guarantee.

That applies to C90 as well, but in C99, there is the additional
guarantee that the quotient is rounded towards zero.

-- Mat.



Tue, 05 Apr 2005 05:24:08 GMT  
 modulus on negative numbers
[snip]

Thanks Mathew and Richard.



Tue, 05 Apr 2005 04:37:43 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. division and modulus of negative integers

2. encoding negative number into a unsigned byte array

3. bitshifting negative numbers

4. rand_r() returns negative number?

5. quicksort and negative numbers.

6. Array indexing, using negative numbers

7. Negative numbers in edit box

8. Borland C and Negative Floating Point Numbers

9. GetWindowItemRect() returns negative numbers

10. GetMenuItemRect() returning negative numbers

11. Negative Numbers problem!!

12. Negative Numbers in the resource file

 

 
Powered by phpBB® Forum Software