comp.lang.ada
 help / color / mirror / Atom feed
From: vincent.diemunsch@gmail.com
Subject: Re: Integers and Mathematical Correctness
Date: Sat, 27 Sep 2014 08:21:33 -0700 (PDT)
Date: 2014-09-27T08:21:33-07:00	[thread overview]
Message-ID: <72db10f1-7e12-4f8c-8ee5-c2bdce727c09@googlegroups.com> (raw)
In-Reply-To: <7ab81f91-af1f-4fb1-8aef-c7f692e22f38@googlegroups.com>

> > 1. It is not consistent with modular types :
> > 
> 
> >    A = (A/B)*B + (A rem B) is not true for modular types. So modular types
> 
> >    should not have a "rem" operator.
> 
> 
> 
> Actually, I question whether you're correct here.  Can you provide an example of a modular type, and values A and B of that type (B /= 0), such that
> 
>    (A/B)*B + (A rem B) /= A ?
> 

> (using the Ada definitions of the operators)?  I may be missing something, but I don't think it's possible.
> 
> 
>                              -- Adam

Hi Adam,

I tried to create a small example to illustrate. Let's try, and please tell me if I am wrong. 

First suppose that you have 4 processors on which you wish to dispatch some piece of work. Imagine that you have 42 simple computations. Each processor will need to process 10 computations and it remains 2 computations that we give to processor n°4. We do have :

     42 = (42 /  4) *4 + (42 rem 4) = 10*4 + 2;

Now we will use a ring buffer to store our computations, instead of a simple array from 1 ... 42. So the buffer has 1024 entries. The 42 computations are located from location n° 1023 - 29 to location n° 12.
We still have 42 computations :  
    
     Last - First + 1 =  11 - (1023 -29) + 1 = -982 == 42 mod 1024.
    
I see that Ada / Gnat correct the modular value to put in 0 .. 1023 before applying the divide operators
/ and rem. So we get the correct result with modular types :  (-982) / 4 = 10 and (-982) rem 4 = 2. 
And finally : ((-982) / 4 ) * 4 + ((-982) rem 4) = 42 == -982 mod 1024.  But clearly 42 /= -982 from the point of view of integers, even if in Ada (-982) = 42 would return true for modular types defined as mod 1024.

So you are right : that means that

      A = (A/B)*B + (A rem B)

holds providing that we understand it the Ada way, which is something like :  

A mod 1024 == Round((A mod 1024) / (B mod 1024))* (B mod 1024) + ((A mod 1024) rem (B mod 1024))

This implies that :
     (-982) / 4 = 10
that I found really ugly and with very little mathematical sense.

And the famous  -A / B  = A / -B = - (A/B)  which is given as the main argument to use "/" instead of "div" is completely false for modular integers in Ada...
   since for exemple -982 /  4 =    42 / 4      = 10
                                982 / -4 = 982 / 1020 = 0
                            -( 982 /  4) = -245 = 779

Now, on the other hand, 

      A = (A div B)*B + A mod B,  

is always true : 
a) for integers : -982 = -246*4 + 2, but I recognize that A div B = -982 div 4 = -246 is of no use for my little problem !!
b) for modular integers (mod 1024), if you set that (-982) div 4 = 10, and (-982) = 42 ! I am not shocked that (-982) div 4 = 10 neither than 982 div (-4) = 0, because the "div" operator is specific and there is no possible confusion with rational division.

So finally I change my mind, the problem is not the "rem" operator for modular type but clearly the use of "/" instead of "div" for modular types. And for signed integers I still would prefer to be able to write Integer (A/B) than simply A/B for the reason that it is simpler in the context of rational types (think of 4 + 1/3 : is it 5/3 or 4 ?).


  reply	other threads:[~2014-09-27 15:21 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
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 [this message]
     [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