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: a07f3367d7,a97feceab5b57877 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!grolier!oleane.net!oleane!hunter.axlog.fr!nobody From: Jean-Pierre Rosen Newsgroups: comp.lang.ada Subject: Re: Saturation arithmetic woes. Date: Thu, 30 Jul 2009 11:09:49 +0200 Organization: Adalog Message-ID: <11or4h.tfh.ln@hunter.axlog.fr> References: <1a16b458-0201-4320-9787-2836ed58f991@e27g2000yqm.googlegroups.com> <4a709faf$0$6282$4f793bc4@news.tdc.fi> NNTP-Posting-Host: mailhost.axlog.fr Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: s1.news.oleane.net 1248937404 16217 195.25.228.57 (30 Jul 2009 07:03:24 GMT) X-Complaints-To: abuse@oleane.net NNTP-Posting-Date: Thu, 30 Jul 2009 07:03:24 +0000 (UTC) User-Agent: Thunderbird 2.0.0.22 (Windows/20090605) In-Reply-To: <4a709faf$0$6282$4f793bc4@news.tdc.fi> Xref: g2news2.google.com comp.lang.ada:7441 Date: 2009-07-30T11:09:49+02:00 List-Id: Niklas Holsti a �crit : >> package body Saturation is >> >> function "+" >> (Left : Saturated_Type; >> Right : Saturated_Type) return Saturated_Type >> is >> Temp_Left : constant Discrete_Type := Discrete_Type (Left); >> Temp_Right : constant Discrete_Type := Discrete_Type (Right); >> begin >> if Temp_Left + Temp_Right > Discrete_Type'Last then > > Depending on the actual type associated with Discrete_Type (and assuming > you use the predefined "+" for that type), you risk getting an overflow > on Temp_Left + Temp_Right. If you have overflow checks disabled (as Gnat > has by default) the sum may "wrap around" and seem to be less than > Discrete_Type'Last, giving you the wrong result. If you have overflow > checks enabled, you get a Constraint_Error instead of > Discrete_Type'Last. Ditto for underflow and Discrete_Type'First. > But of course, the simplest way to check is to write: if Temp_Left > Discrete_Type'Last - Temp_Right then No overflow, ever! Of course, you need a "-" operator, and I assume it is consistent with "+"... -- --------------------------------------------------------- J-P. Rosen (rosen@adalog.fr) Visit Adalog's web site at http://www.adalog.fr