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 14 of 14 Topic 9512 of 9983
Post > Topic >>

Re: -1%N = -1

by John G Harris <news0@[EMAIL PROTECTED] > Apr 20, 2008 at 03:49 PM

On Fri, 18 Apr 2008 at 17:14:06, in comp.lang.c++.moderated, OuFeRRaT
wrote:
>The definition of the remainder:
>
>If a and d are integers, with d non-zero, then a remainder is an
>integer r such that a = qd + r for some integer q, and with 0 ˜ |r| < |
>d|.

Dividing a by b to give quotient q and remainder r is expected to give a
result that obeys the equation
  a = b*q + r

Now, if the numbers are Natural numbers, {0, 1, 2, ...}, then when
a < b there is only one solution, q = 0, r = a. If a >= b then there is
more than one solution. We want a rule that picks solutions that are
clean and convenient. The rule is
  b is non-zero and r < b.
Luckily this rule always picks a single answer for q and r. Application
areas where a different rule is desirable appear to be rare.

But if the numbers are Integers, { ... -2, -1, 0, 1, 2, ... }, then
there is an infinite number of solutions in every case. A rule that
picks clean and convenient solutions will make r close to zero, but
different application areas have different needs for the sign of r and
can have cases where r = 0 is not allowed.

For instance, a distance to the left of 275 cm will translate to -2 m
followed by -75 cm.

For instance, a payment of 275 cents will translate, relative to you, to
-3 dollar bills followed by +25 cents change.

It wouldn't be sensible to decree that one of these calculations is
wrong. Indeed, picking the most appropriate rules is the essence of
applied mathematics.


>This is the correct mathematical definition, as explained in the first
>week of any basic maths course, this is why I used the word
>"incorrect".

It's normal practice to give beginning students a simplified definition.
Only later, when they have more experience, are they told about all the
complications.


>I didn't say it was a bad idea that in C++ (and possibly other
>programming languages) they decided to make another definition for the
>integer division and the remainder , but I would like to know why.

It's very simple : different areas of applied mathematics need different
definitions.


>Thomas, "chip manufacturers aren't certain either which definition to
>pick for the modulo operation." Has it anything to do with the
>efficiency of the operation or it's that they haven't asked a
>mathematician?
  <snip>

It may be that the algorithm runs faster, or maybe they've consulted
their Fortran compiler team who want the Fortran benchmarks to run as
fast as possible.


  John
-- 
John Harris

      [ 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 Sun Sep 7 3:48:30 CDT 2008.