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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,583275b6950bf4e6 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-05-30 12:08:57 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-post-02!sn-post-01!supernews.com!corp.supernews.com!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: Saturated Math Date: Fri, 30 May 2003 14:10:06 -0500 Organization: Posted via Supernews, http://www.supernews.com Message-ID: References: <3ECFF541.1010705@attbi.com> <3ED0B820.5050603@noplace.com> <3ED2096F.3020800@noplace.com> <3ED353BE.40605@noplace.com> <3ED4A323.3000909@noplace.com> <3ED5E8DE.8070308@noplace.com> <3ED7437E.5060607@noplace.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Newsreader: Microsoft Outlook Express 4.72.3612.1700 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3719.2500 X-Complaints-To: abuse@supernews.com Xref: archiver1.google.com comp.lang.ada:38138 Date: 2003-05-30T14:10:06-05:00 List-Id: Marin David Condic wrote in message <3ED7437E.5060607@noplace.com>... >Note that you'd also have some problems with assignment because Ada >doesn't provide a means of overriding it. This is also the area where >saturation would be the *most* useful. > >Consider that I want to compute some position for an actuator. I don't >really care if the computation itself is done with infinite precision. >What I care about is the *result* being limited to within the bounds of >the actuator. So what I need is saturation on assignment and it would >also be handy to be able to easily mix it with constants and other >compatible types. We've done it with home-brewed packages, but it just >seems to be more clumsy than if it were just a feature of the language. I think that's rather different than what others have been talking about. Saturation on operations is easy to implement cleanly, assignment much less so. But Ada does have a way to override assignment, so that's doable with a carefully crafted package. The question would be whether the overhead was essessive. That of course would depend on the implementation and on the number of objects of this type that was declared. private with Ada.Finalization; generic type Literal_Type is range <>; -- Use for literals, defines range of type. package Marins_Saturated_Math is type Saturated_Type is private; -- Operations: function "+" (Right : Saturated_Type) return Saturated_Type; function "-" (Right : Saturated_Type) return Saturated_Type; function "+" (Left, Right : Saturated_Type) return Saturated_Type; ... -- Rest of the binary operations here. -- Operations to give literals: function "+" (Right : Literal_Type) return Saturated_Type; function "-" (Right : Literal_Type) return Saturated_Type; function "+" (Left : Saturated_Type; Right : Literal_Type) return Saturated_Type; ... -- Rest of the binary operations here. private type Saturated_Type is new Ada.Finalization.Controlled with record Value : Literal_Type'Base; -- May exceed the range, but is forced back on assignment. end record; procedure Adjust (Object : in out Saturated_Type); -- Initialize, Finalize not needed. end Marins_Saturated_Math; And the implementation of Adjust would look like: procedure Adjust (Object : in out Saturated_Type) is begin if Object.Value > Literal_Type'Last then Object.Value := Literal_Type'Last; elsif Object.Value < Literal_Type'First then Object.Value := Literal_Type'First; end if; end Adjust; Writing this package would be a pain, but it only has to be done once. Also note that since this type doesn't override Finalize, a compiler could eliminate all of the overhead of a controlled type. I don't think that any compilers currently do that, but it is the sort of optimization that compiler vendors do add for good customers. (The Adjust to call is always known at compile-time, as long as 'Class is never used, which it won't be for this type.) Randy.