comp.lang.ada
 help / color / mirror / Atom feed
From: "chris.danx" <chris.danx@ntlworld.com>
Subject: Re: Integers and Mathematical Correctness
Date: Thu, 1 Nov 2001 17:10:41 -0000
Date: 2001-11-01T17:10:41+00:00	[thread overview]
Message-ID: <_lfE7.52002$a14.6154112@news6-win.server.ntlworld.com> (raw)
In-Reply-To: 1f26o22.1xfvwvo111pfi4N%csampson@inetworld.net


"Charles Sampson" <csampson@inetworld.net> wrote in message
news:1f26o22.1xfvwvo111pfi4N%csampson@inetworld.net...
> chris.danx <chris.danx@ntlworld.com> wrote:
>
> > Hi,
> >
> > To most this may seem insignificant but I'm more mathematically minded
and I
> > just found something out.  Many programming languages don't implement
> > division properly!
> >
> > For example -3/2 should be -2 not -1, with a remainder 1 not -1.
> >
> > I'm trying to implement integer division the "proper" way (according to
> > Euclid), just for fun (and to provide a mathematically correct package
for
> > anyone who gets annoyed by this).
>
>      I'm not sure how you determined that the "proper" definition of
> integer division is to return floor (a/b), where "/" is mathematical
> division.  The definition used by most programming languages preserves
> the very useful property -(a/b) = (-a)/b = a/(-b), where this time "/"
> represents integer division.
>
>      Please explain the Euclid reference.  I don't have any History of
> Mathematics books available, but I don't remember that Euclid had nega-
> tive numbers to work with.

According to Euclid, the "division algorithm" (which isn't an algorithm)
asserts that for a/b

a = kb + r

where r >= 0
      k <- Integers


[For anyone unfamiliar with Haskell or (possibly) other functional languages
"<-" reads as "is a member of"].

So for -4 / 3

-4 = 3k + r

r > 0, so k = -2

-4 = 3(-2) + r
-4 = -6 + r
r  = 2

however in Ada (and others) this is,

-4/3 = -1

-4 = 3k + r
-4 = 3(-1) + r
-4 = -3 + r
r  = -1

Which is wrong!  r can never be negative according to Euclid.

Tons on the division algorithm can be found on the net,
http://www.utm.edu/research/primes/glossary/DivisionAlgorithm.html is one
such resource but I'm having trouble finding any history.  I don't know if
Euclid had negatives, but the Division Algorithm works for negatives.

This page has something on it (coding it in C),

http://www.csclub.uwaterloo.ca/~mpslager/compsci/GCD.html#Division%20Algorit
hm


Bye,
Chris





  parent reply	other threads:[~2001-11-01 17:10 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-10-31 20:27 Integers and Mathematical Correctness chris.danx
2001-10-31 21:21 ` David C. Hoos
2001-10-31 22:16   ` chris.danx
2001-10-31 22:47     ` David C. Hoos
2001-10-31 22:55       ` chris.danx
2001-10-31 23:16         ` Matthew Heaney
2001-10-31 21:42 ` Mark Johnson
2001-11-01 18:57   ` Mark Johnson
2001-11-01 14:32 ` Wes Groleau
2001-11-01 16:18   ` wilhelm.spickermann
2001-11-01 16:48   ` chris.danx
2001-11-01 15:45 ` Charles Sampson
2001-11-01 16:20   ` Marin David Condic
2001-11-03 17:02     ` Richard Riehle
2001-11-05 14:47       ` Marin David Condic
2001-11-06  3:53         ` Eric G. Miller
2001-11-06  4:28           ` James Rogers
2001-11-06  6:06             ` peter
2001-11-06 14:48               ` James Rogers
2001-11-06 15:54                 ` Marin David Condic
2001-11-07  3:44             ` Eric G. Miller
2001-11-01 17:10   ` chris.danx [this message]
2001-11-01 17:52     ` Chad Robert Meiners
2001-11-01 19:02       ` chris.danx
2001-11-01 17:57     ` Wes Groleau
2001-11-03 14:57     ` Charles Sampson
2001-11-01 16:11 ` Charles Lindsey
2001-11-01 18:40   ` Wilhelm Spickermann
2001-11-01 19:18   ` chris.danx
2001-11-02  1:37     ` Steven Deller
2014-09-26  9:07       ` vincent.diemunsch
2014-09-26 16:38         ` Niklas Holsti
2014-09-26 16:58           ` AdaMagica
2014-09-26 17:51             ` Adam Beneschan
2014-09-27  9:01               ` AdaMagica
2014-09-27 10:15                 ` AdaMagica
2014-09-27 16:32                 ` Niklas Holsti
2014-09-27 16:49                   ` Jeffrey Carter
2014-09-27 18:52                     ` Niklas Holsti
2014-09-27 18:54                   ` Adam Beneschan
2014-09-27 19:07                     ` Adam Beneschan
     [not found]                 ` <3489504a-f82b-4fec-8a6c-7cb91854dd1e@googlegroups.com>
2014-09-27 19:21                   ` AdaMagica
2014-09-27 11:44           ` gautier_niouzes
2014-09-26 16:41         ` Adam Beneschan
2014-09-26 16:46         ` Adam Beneschan
2014-09-27 15:21           ` vincent.diemunsch
     [not found]             ` <34da5a39-9fa3-4e8e-a3f9-98f61a4ebcc7@googlegroups.com>
2014-09-28  7:47               ` Dmitry A. Kazakov
2014-09-29 14:58                 ` Adam Beneschan
2014-09-29 16:25                   ` Dmitry A. Kazakov
2014-10-01 19:48                   ` vincent.diemunsch
2014-10-02 11:10                     ` G.B.
2001-11-01 18:08 ` Tucker Taft
2001-11-01 18:54 ` David Starner
2001-11-01 21:44   ` Wilhelm Spickermann
2001-11-02 12:52 ` chris.danx
  -- strict thread matches above, loose matches on Subject: below --
2001-10-31 22:42 Beard, Frank
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox