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!postnews.google.com!k30g2000yqf.googlegroups.com!not-for-mail From: Martin Newsgroups: comp.lang.ada Subject: Re: Saturation arithmetic woes. Date: Thu, 30 Jul 2009 04:51:15 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <1a16b458-0201-4320-9787-2836ed58f991@e27g2000yqm.googlegroups.com> <4a709faf$0$6282$4f793bc4@news.tdc.fi> <11or4h.tfh.ln@hunter.axlog.fr> NNTP-Posting-Host: 20.133.0.8 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1248954675 1705 127.0.0.1 (30 Jul 2009 11:51:15 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 30 Jul 2009 11:51:15 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: k30g2000yqf.googlegroups.com; posting-host=20.133.0.8; posting-account=g4n69woAAACHKbpceNrvOhHWViIbdQ9G User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:7444 Date: 2009-07-30T04:51:15-07:00 List-Id: On Jul 30, 10:09=A0am, Jean-Pierre Rosen wrote: > Niklas Holsti a =E9crit : > > >> package body Saturation is > > >> =A0 function "+" > >> =A0 =A0 (Left =A0: Saturated_Type; > >> =A0 =A0 =A0Right : Saturated_Type) return Saturated_Type > >> =A0 is > >> =A0 =A0 Temp_Left =A0: constant Discrete_Type :=3D Discrete_Type (Left= ); > >> =A0 =A0 Temp_Right : constant Discrete_Type :=3D Discrete_Type (Right)= ; > >> =A0 begin > >> =A0 =A0 if Temp_Left + Temp_Right > Discrete_Type'Last then > > > Depending on the actual type associated with Discrete_Type (and assumin= g > > you use the predefined "+" for that type), you risk getting an overflow > > on Temp_Left + Temp_Right. If you have overflow checks disabled (as Gna= t > > 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: > =A0 =A0if Temp_Left > Discrete_Type'Last - Temp_Right then > > No overflow, ever! Of course, you need a "-" operator, and I assume it > is consistent with "+"... > -- > --------------------------------------------------------- > =A0 =A0 =A0 =A0 =A0 =A0J-P. Rosen (ro...@adalog.fr) > Visit Adalog's web site athttp://www.adalog.fr Temp_Right =3D -1 ? Cheers -- Martin