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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,a9b0810d3106d9b8 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!2g2000vbl.googlegroups.com!not-for-mail From: Martin Newsgroups: comp.lang.ada Subject: Re: Fun with C Date: Sun, 17 Apr 2011 01:29:45 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <27cf3992-4132-4483-9110-adc7a089cd4a@e8g2000vbz.googlegroups.com> <8762qdyk6y.fsf@ludovic-brenta.org> <4daa9411$0$7658$9b4e6d93@newsspool1.arcor-online.net> NNTP-Posting-Host: 86.168.51.141 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1303028986 14918 127.0.0.1 (17 Apr 2011 08:29:46 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 17 Apr 2011 08:29:46 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: 2g2000vbl.googlegroups.com; posting-host=86.168.51.141; posting-account=g4n69woAAACHKbpceNrvOhHWViIbdQ9G User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1,gzip(gfe) Xref: g2news2.google.com comp.lang.ada:19801 Date: 2011-04-17T01:29:45-07:00 List-Id: On Apr 17, 8:17=A0am, Georg Bauhaus wrote: > On 4/16/11 11:12 PM, Ludovic Brenta wrote: > > > "Nasser M. Abbasi" =A0writes: > >> $ gcc -Wall =A0t.c > >> $ ./a.out > >> R =3D 33554416 > > > So R =3D 2**25 - 16, the correct answer is -16, so the actual result is > > the correct one except for bit 24, which is 1 when it should be zero. > > > Out of curiosity, is this actual result a predictable consequence of th= e > > language definition, or is it undefined behavior? > > Why, it is the same in Ada. > > with Ada.Text_IO; use Ada.Text_IO; > with Ada.Unchecked_Conversion; > > procedure Conv is > > =A0 =A0 =A0type UInt is mod 2**32; > =A0 =A0 =A0type Int is range -2**31 .. 2**31 -1; > > =A0 =A0 =A0function To_UInt is new Ada.Unchecked_Conversion(Int, UInt); > =A0 =A0 =A0N: UInt :=3D 128; > =A0 =A0 =A0I: Int :=3D -2048; > =A0 =A0 =A0R : Int; > begin > =A0 =A0 =A0R :=3D Int(To_UInt(I) / N); > =A0 =A0 =A0Put_Line ("R =3D" & Int'Image(R)); > end; Yes, the Ada version is _much_ more obvious and understandable But I suspect the _intent_ is of the original code is (probably): with Ada.Text_IO; use Ada.Text_IO; procedure Conv is type UInt is mod 2**32; type Int is range -2**31 .. 2**31 -1; N: UInt :=3D 128; I: Int :=3D -2048; R : Int; begin R :=3D I / Int (N); Put_Line ("R =3D" & Int'Image(R)); end; -- Martin