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 13:35:42 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!arclight.uoregon.edu!wn13feed!wn12feed!worldnet.att.net!204.127.198.204!attbi_feed4!attbi.com!sccrnsc03.POSTED!not-for-mail From: tmoran@acm.org Newsgroups: comp.lang.ada Subject: Re: Saturated Math References: <3ED7437E.5060607@noplace.com> X-Newsreader: Tom's custom newsreader Message-ID: NNTP-Posting-Host: 12.234.13.56 X-Complaints-To: abuse@attbi.com X-Trace: sccrnsc03 1054326829 12.234.13.56 (Fri, 30 May 2003 20:33:49 GMT) NNTP-Posting-Date: Fri, 30 May 2003 20:33:49 GMT Organization: AT&T Broadband Date: Fri, 30 May 2003 20:33:49 GMT Xref: archiver1.google.com comp.lang.ada:38153 Date: 2003-05-30T20:33:49+00:00 List-Id: >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 In that case, a Limit function seems more straightforward: subtype Color_Brightness is integer range 0 .. 255; function Limit(V : Integer) return Color_Brightness is begin if V < Color_Brightness'first then return Color_Brightness'first; elsif V > Color_Brightness'last then return Color_Brightness'last; else return V; end if; end Limit; X,Y,Z : Color_Brightness; ... X := Limit(X+Y-Z); Granted it's a little extra writing, but with inlining and a compiler that doesn't generate an unnecessary range check, it should be nearly costless at run time. It gets around non-associative arithmetic questions. If you forget the function call, ie, X :=X+Y-Z; then you raise an exception instead of saturating, so at least you know there's a problem. In MMX the saturating arithmetic is tied up with SIMD vector operations, so the hardware doesn't help for simple variables.