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: 103376,9bb56e94a4c5bb5e X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!newsfeed.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!elnk-nf2-pas!elnk-pas-nf1!newsfeed.earthlink.net!cyclone.socal.rr.com!cyclone2.kc.rr.com!news2.kc.rr.com!twister.socal.rr.com.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada Subject: Re: How unchecked conversion works? References: <0NSFd.2$Lz6.0@dfw-service2.ext.ray.com> From: Keith Thompson Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) Cancel-Lock: sha1:odEUuCvD2ObfmCASCmkZKJKwAds= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Fri, 14 Jan 2005 22:27:39 GMT NNTP-Posting-Host: 66.91.240.168 X-Complaints-To: abuse@rr.com X-Trace: twister.socal.rr.com 1105741659 66.91.240.168 (Fri, 14 Jan 2005 14:27:39 PST) NNTP-Posting-Date: Fri, 14 Jan 2005 14:27:39 PST Organization: RoadRunner - West Xref: g2news1.google.com comp.lang.ada:7780 Date: 2005-01-14T22:27:39+00:00 List-Id: Mark H Johnson writes: > None wrote: > >> well, I try to explain. I have those types for example >> type fixed is delta 0.03 range -90.0..90.0 >> for fixed'size use 16 >> and >> type inte is range -32768..32767 >> for inte'size use 16 >> and a new function that renames unchecked_conversion to come from >> fixed to inte (probably an inefficient way, but I have to do this >> way), so I'd like to predict on paper which value of inte type >> corresponds to 89.09 for example: what kind of calculus should I do? >> This depends on something else? Thanks again! There's no reason to expect that Unchecked_Conversion would be inefficient. It's generally implemented as a no-op that overrides type checking. > The specific answer will likely be compiler specific so you may need > to write a test application to verify the result. Let me see if I can > explain why... > > One choice - the compiler chooses to represent value as 1 == your > delta value (0.03) and scale everything appropriately. Then 90.0 is > represented by 3000 (decimal) since > 3000 * 0.03 == 90.0 > So you have a range of values +/- 3000 when you do the unchecked > conversion. Pretty straight forward implementation for your problem. > > Another choice - the compiler chooses to make 'Small of your datatype > not equal to 'Delta of your data type (this IS allowed in ARM > 3.5.9). Since you did not request a decimal fixed point value, 'Small > will be a power of two smaller than 'Delta. If my arithmetic is > correct, that could be something like 1/64 (0.015625) or even smaller > (e.g., 1/256). In this case, the value could be mapped as follows: > Siiiiiiiffffffff > where S is the sign bit, iiiiiii is the 7 bit integer value and > ffffffff is the 8 bit fraction. I assume you can do the appropriate > arithmetic to interpret this form of fixed point value. Actually, the language specifies that the "small" value for type fixed will *not* be 0.03. RM95 3.5.9p8 (with underscores to denote italics) says: The set of values of a fixed point type comprise the integral multiples of a number called the _small_ of the type. For a type defined by an ordinary_fixed_point_definition (an _ordinary_ fixed point type), the _small_ may be specified by an attribute_definition_clause (see 13.3); if so specified, it shall be no greater than the _delta_ of the type. If not specified, the _small_ of an ordinary fixed point type is an implementation-defined power of two less than or equal to the _delta_. So Fixed'Small is guaranteed to be a power of two no larger than 0.03. The most likely value is probably 1.0/64.0 (0.015625) (since 1.0/32.0 is too large). The "delta" is the precision you ask for; the "small" is the precision you get. You can find out what the actual "small" is by printing the value of Fixed'Small. If you really want Fixed'Small to be 0.03, you can specify it: for Fixed'Small use 0.03; Assuming Fixed'Small = 1.0/64.0, the value 89.09 can't be represented exactly. You'll get either 89.078125 (5701.0/64.0) or 89.093750 (5702.0/64.0). Doing an Unchecked_Conversion of that value to Inte should give you either 5701 or 5702. You may be able to achieve the same affect by dividing by Fixed'Small: F : constant Fixed := 89.09; I : constant Inte := Inte(F / Fixed'Small); A quick experiment shows that this works, but I'm not sure that it's guaranteed. There may be some rounding and/or overflow issues. (It's been a while; I don't remember the details.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this.