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,76e8d825615718a X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder1.cambriumusenet.nl!feed.tweaknews.nl!138.195.8.3.MISMATCH!news.ecp.fr!nuzba.szn.dk!pnx.dk!eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Ada.Containers Hash function for a set of small integers Date: Mon, 26 Apr 2010 22:05:05 +0100 Organization: A noiseless patient Spider Message-ID: References: <50701baa-7c05-450c-a42d-c699516ddc00@t14g2000prm.googlegroups.com> <0590cf17-12ea-401d-9dc3-02365139b37e@p35g2000prf.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Mon, 26 Apr 2010 21:05:07 +0000 (UTC) Injection-Info: news.eternal-september.org; posting-host="KCXegvZb5vh43D+f3BR6Ew"; logging-data="14433"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18z9swDsI5Hqo6OVu9VGpb/d6DWsMJvvIc=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (darwin) Cancel-Lock: sha1:oQPQAnH7/AEvCYQ8VP1rjVJ8lMs= sha1:6WB9ZNScTYTzjOCre0VxcPqmT2M= Xref: g2news1.google.com comp.lang.ada:10220 Date: 2010-04-26T22:05:05+01:00 List-Id: Robert A Duff writes: > Simon Wright writes: > >> type M is mod 2**31; >> Result : M := 0; >> begin >> Result := Result xor Integer'Pos (I.A); > >> and, before anyone rushes to check it out, yes, it fails with >> negative values and I'm about to file a bug against it. > > I don't think that's a compiler bug. Conversion of negative numbers > to modular raises Constraint_Error. I think that's a language design > flaw, but not everyone agrees with me. The 'Mod attribute works > around the problem. No, I meant a bug against my code generator! On the other hand, I found some strange behaviour with GNAT GPL 2009 (and GCC 4.5.0) while investigating possible solutions: ------------------------------------------------------------------------ with Ada.Text_IO; use Ada.Text_IO; procedure Odd_Warning is procedure Inner (X : Integer) is type M is mod 2**31; Result : M; begin begin Result := 0; Result := Result xor (Integer'Pos (X) mod 2**30); -- No compiler warning, but raises exception exception when others => Put_Line ("exception a"); end; begin Result := 0; Result := Result xor (Integer'Pos (X) mod 2**31); -- odd_warning.adb:20:53: warning: mod with zero divisor -- odd_warning.adb:20:53: warning: "Constraint_Error" will be -- raised at run time exception when others => Put_Line ("exception b"); end; begin Result := 0; Result := Result xor M (Integer'Pos (X) mod 2**31); -- No warning (and executes as I had expected) Put_Line ("result:" & M'Image (Result)); exception when others => Put_Line ("exception c"); end; end Inner; begin Inner (-1); end Odd_Warning; ------------------------------------------------------------------------ The second block's compiler warning is strange, especially the part about the zero divisor. The Aonix ObjectAda 7.2 compiler gives a very similar warning referring to 95LRM4.5.5(22), and gives an error at the third block (95LRM4.9(35)). I think my route will be via unchecked conversion, if I don't decide that it's a feature - in 10 years of use no one has needed to use a negative value as a component of a hash.