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: a07f3367d7,6fabd104d18f3943,start X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!news.albasani.net!not-for-mail From: Dirk Herrmann Newsgroups: comp.lang.ada Subject: conversions between fixed-point types Date: Fri, 18 Sep 2009 23:35:31 +0200 Organization: albasani.net Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net GCR1WXQhq6fuZvcCs/lt0gkv9KdNONwnyfdN+Ul6oZqUUKuzwxUglWqGZsYErNRuzGlfLb9DjhABEibarZEG2sA+zK8ptFb+drt5oH/xEViJMW3Xi9QH6yj5ks3PBQa2 X-Complaints-To: abuse@albasani.net NNTP-Posting-Date: Fri, 18 Sep 2009 21:35:31 +0000 (UTC) X-User-ID: vKOco1AMvwSgS9j0tNbLFgsrXHNK99ZPUySCXa0ssDe3dRBEOTWp49MK/8XXu1XRWOqMJyFaeucagFz3S25J1w== Cancel-Lock: sha1:Kf0YdbPOBBQmCzLeTsnbYIwl4yE= User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090706) X-NNTP-Posting-Host: ZjNrS/lFJFN6l3EdaZg3GOnRa7mp+HtTJXzb+GCsbmc= Xref: g2news2.google.com comp.lang.ada:8386 Date: 2009-09-18T23:35:31+02:00 List-Id: Hi, I am currently trying to learn about Ada's fixed-point types, and there are some points that I couldn't find any statement about, or which I simply could not understand. (Background: At my company there is a lot of code that makes heavy use of fixed-point calculations in a proprietary language. This language offers special support for fixed-point arithmetics. I am investigating whether Ada would be a better choice, and thus the fixed-point handling is important. Note that the decision to use fixed-point arithmetics is due to the fact that the target processors do not have a floating point unit - and this will remain this way for some time.) The first set of questions I have is about conversions between different fixed-point types. I realized that with GNAT for the following example types type FpA is delta 0.5 range -10.0 .. +10.0; for FpA'Small use 0.5; type FpB is delta 0.4 range -10.0 .. +10.0; for FpB'Small use 0.4; conversions from FpA to FpB work as follows: FpA FpB -1.5 ---> -1.2 -1.0 ---> -0.8 -0.5 ---> -0.4 -0.0 ---> -0.0 +0.5 ---> +0.4 +1.0 ---> +0.8 +1.5 ---> +1.2 That is, the conversion is performed similar to a truncation (always choosing the closest value towards zero). However, in principle there are other possibilities how the conversion could be done: truncation floor ceiling round -1.5 ---> -1.2 -1.6 -1.2 -1.6 -1.0 ---> -0.8 -1.2 -0.8 -1.2 -0.5 ---> -0.4 -0.8 -0.4 -0.4 0.0 ---> 0.0 0.0 0.0 0.0 +0.5 ---> +0.4 +0.4 +0.8 +0.4 +1.0 ---> +0.8 +0.8 +1.2 +1.2 +1.5 ---> +1.2 +1.2 +1.6 +1.6 First question: There does not seem to be any definite statement in the reference manual of Ada 2005 about how exactly the conversion has to be performed. All of the above (and probably more options) seem to be legal, and GNAT just chooses truncation. Is that right, or have I already missed something? I have not even found a statement that a compiler has to be consistent with respect to the strategy it chooses. It might be possible that for different conversions within the same program different strategies are chosen. Regarding GNAT, I could not find any statement in the GNAT documentation about whether GNAT will do it the same way throughout. Second question: Is there any way to control how the conversion is performed? Maybe I have missed something and the language itself offers some possibility? Is there, for example, some attribute S'Round(X : some fixed point type) that rounds X to the nearest value of S? I could not find anything like that. Or, if this is not the case, do any libraries exist that provide generic conversion functions to achieve some or all of the "truncate", "floor", "ceil" or "round" behaviours? As I mentioned at the top of the mail, any solutions that would require conversions between fixed-point and floating-point to implement these conversions would probably not be acceptable for our systems. Thanks in advance, Dirk Herrmann (email address gladly given on request)