comp.lang.ada
 help / color / mirror / Atom feed
From: Jean-Pierre Rosen <rosen@adalog.fr>
Subject: Re: Saturation arithmetic woes.
Date: Thu, 30 Jul 2009 11:09:49 +0200
Date: 2009-07-30T11:09:49+02:00	[thread overview]
Message-ID: <11or4h.tfh.ln@hunter.axlog.fr> (raw)
In-Reply-To: <4a709faf$0$6282$4f793bc4@news.tdc.fi>

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



  parent reply	other threads:[~2009-07-30  9:09 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 [this message]
2009-07-30 11:51     ` Martin
replies disabled

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