From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.70.96.239 with SMTP id dv15mr25144337pdb.3.1411831293325; Sat, 27 Sep 2014 08:21:33 -0700 (PDT) X-Received: by 10.140.20.246 with SMTP id 109mr256970qgj.0.1411831293278; Sat, 27 Sep 2014 08:21:33 -0700 (PDT) Path: border1.nntp.dca1.giganews.com!nntp.giganews.com!h15no8056119igd.0!news-out.google.com!i10ni38qaf.0!nntp.google.com!k15no112344qaq.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sat, 27 Sep 2014 08:21:33 -0700 (PDT) In-Reply-To: <7ab81f91-af1f-4fb1-8aef-c7f692e22f38@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=87.91.37.131; posting-account=hya6vwoAAADTA0O27Aq3u6Su3lQKpSMz NNTP-Posting-Host: 87.91.37.131 References: <7ab81f91-af1f-4fb1-8aef-c7f692e22f38@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <72db10f1-7e12-4f8c-8ee5-c2bdce727c09@googlegroups.com> Subject: Re: Integers and Mathematical Correctness From: vincent.diemunsch@gmail.com Injection-Date: Sat, 27 Sep 2014 15:21:33 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: number.nntp.dca.giganews.com comp.lang.ada:189186 Date: 2014-09-27T08:21:33-07:00 List-Id: > > 1. It is not consistent with modular types : > >=20 >=20 > > A =3D (A/B)*B + (A rem B) is not true for modular types. So modular = types >=20 > > should not have a "rem" operator. >=20 >=20 >=20 > Actually, I question whether you're correct here. Can you provide an exa= mple of a modular type, and values A and B of that type (B /=3D 0), such th= at >=20 > (A/B)*B + (A rem B) /=3D A ? >=20 > (using the Ada definitions of the operators)? I may be missing something= , but I don't think it's possible. >=20 >=20 > -- Adam Hi Adam, I tried to create a small example to illustrate. Let's try, and please tell= me if I am wrong.=20 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 processo= r will need to process 10 computations and it remains 2 computations that w= e give to processor n=B04. We do have : 42 =3D (42 / 4) *4 + (42 rem 4) =3D 10*4 + 2; Now we will use a ring buffer to store our computations, instead of a simpl= e array from 1 ... 42. So the buffer has 1024 entries. The 42 computations = are located from location n=B0 1023 - 29 to location n=B0 12. We still have 42 computations : =20 =20 Last - First + 1 =3D 11 - (1023 -29) + 1 =3D -982 =3D=3D 42 mod 1024. =20 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 = =3D 10 and (-982) rem 4 =3D 2.=20 And finally : ((-982) / 4 ) * 4 + ((-982) rem 4) =3D 42 =3D=3D -982 mod 102= 4. But clearly 42 /=3D -982 from the point of view of integers, even if in= Ada (-982) =3D 42 would return true for modular types defined as mod 1024. So you are right : that means that A =3D (A/B)*B + (A rem B) holds providing that we understand it the Ada way, which is something like = : =20 A mod 1024 =3D=3D Round((A mod 1024) / (B mod 1024))* (B mod 1024) + ((A mo= d 1024) rem (B mod 1024)) This implies that : (-982) / 4 =3D 10 that I found really ugly and with very little mathematical sense. And the famous -A / B =3D A / -B =3D - (A/B) which is given as the main = argument to use "/" instead of "div" is completely false for modular intege= rs in Ada... since for exemple -982 / 4 =3D 42 / 4 =3D 10 982 / -4 =3D 982 / 1020 =3D 0 -( 982 / 4) =3D -245 =3D 779 Now, on the other hand,=20 A =3D (A div B)*B + A mod B, =20 is always true :=A0 a) for integers : -982 =3D -246*4 + 2, but I recognize that A div B =3D -98= 2 div 4 =3D -246 is of no use for my little problem !! b) for modular integers (mod 1024), if you set that (-982) div 4 =3D 10, an= d (-982) =3D 42 ! I am not shocked that (-982) div 4 =3D 10 neither than 98= 2 div (-4) =3D 0, because the "div" operator is specific and there is no po= ssible confusion with rational division. So finally I change my mind, the problem is not the "rem" operator for modu= lar 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 ration= al types (think of 4 + 1/3 : is it 5/3 or 4 ?).