comp.lang.ada
 help / color / mirror / Atom feed
From: Martin <martin.dowie@btopenworld.com>
Subject: Re: Saturation arithmetic woes.
Date: Thu, 30 Jul 2009 04:51:15 -0700 (PDT)
Date: 2009-07-30T04:51:15-07:00	[thread overview]
Message-ID: <b3bb887d-4428-400f-a420-5912910965db@k30g2000yqf.googlegroups.com> (raw)
In-Reply-To: 11or4h.tfh.ln@hunter.axlog.fr

On Jul 30, 10:09 am, Jean-Pierre Rosen <ro...@adalog.fr> wrote:
> 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 (ro...@adalog.fr)
> Visit Adalog's web site athttp://www.adalog.fr

Temp_Right = -1 ?

Cheers
-- Martin




      reply	other threads:[~2009-07-30 11:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-29 17:03 Saturation arithmetic woes xorque
2009-07-29 17:13 ` Martin
2009-07-29 18:14 ` Jeffrey R. Carter
2009-07-29 19:14 ` Niklas Holsti
2009-07-29 19:39   ` xorque
2009-07-30  9:09   ` Jean-Pierre Rosen
2009-07-30 11:51     ` Martin [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox