Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: -1%N = -1
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 14 Topic 9512 of 10096
Post > Topic >>

Re: -1%N = -1

by Marco Manfredini <ok_nospam_ok@[EMAIL PROTECTED] > Apr 17, 2008 at 11:41 AM

OuFeRRaT wrote:

> Does anyone know why the remainder and the division operations are
> incorrect with negative numbers in C++? Is it about assembly?

They aren't incorrect. They're just not required to follow the
definition of "modulus", which is why "%" is called the remainder
operator instead.  In fact, it's implementation defined. However, C99
(IIRC) defines the result of a%b to have the sign of a, therefore I'd
expect this to sneak into most C++ implementations as well.

This is probably even reasonable. The basic equality I'd expect to hold
is a==b*(a/b)+(a%b). If the sign of the remainder would follow the sign
of the divisor, it would be necessary that -10/3==-4 and -10%3==2. In
other words, ((a!=0) == ((a/b) != -(-a/b))) would always be true, quite
uncomfortably.

-- 
IYesNo yes=YesNoFactory.getFactoryInstance().YES;
yes.getDescription().equals(array[0].toUpperCase());

      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 14 Posts in Topic:
-1%N = -1
OuFeRRaT <ouferrat@[EM  2008-04-17 03:52:00 
Re: -1%N = -1
Thomas Richter <thor@[  2008-04-17 11:36:17 
Re: -1%N = -1
Marco Manfredini <ok_n  2008-04-17 11:41:52 
Re: -1%N = -1
Francis Glassborow <fr  2008-04-17 11:43:14 
Re: -1%N = -1
guinness.tony@[EMAIL PROT  2008-04-17 11:41:28 
Re: -1%N = -1
"Maarten Kronenburg&  2008-04-17 12:03:20 
Re: -1%N = -1
Seungbeom Kim <musiphi  2008-04-17 16:15:48 
Re: -1%N = -1
"Maarten Kronenburg&  2008-04-17 19:33:50 
Re: -1%N = -1
OuFeRRaT <ouferrat@[EM  2008-04-18 17:14:06 
Re: -1%N = -1
John G Harris <news0@[  2008-04-19 02:01:59 
Re: -1%N = -1
Thomas Richter <thor@[  2008-04-19 18:12:30 
Re: -1%N = -1
peter koch larsen <pet  2008-04-20 15:46:04 
Re: -1%N = -1
"Maarten Kronenburg&  2008-04-20 15:49:33 
Re: -1%N = -1
John G Harris <news0@[  2008-04-20 15:49:40 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 8:32:48 CDT 2008.