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,1038abdfae2ba73 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!w1g2000prk.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Modify value - type duration Date: Wed, 5 Nov 2008 17:03:50 -0800 (PST) Organization: http://groups.google.com Message-ID: <6f0d9434-9489-4946-b29f-60957c83858c@w1g2000prk.googlegroups.com> References: <7ef68540-6df1-4e47-b425-ab08c03f2db9@p31g2000prf.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1225933431 30309 127.0.0.1 (6 Nov 2008 01:03:51 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 6 Nov 2008 01:03:51 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: w1g2000prk.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:8318 Date: 2008-11-05T17:03:50-08:00 List-Id: On Nov 5, 2:47 pm, "Jeffrey R. Carter" wrote: > Andreas.Schm...@googlemail.com wrote: > > function cutIt(var : duration) return duration is > > > tempvar : integer; > > > begin > > tempvar := integer(var); > > return (var - duration(tempvar)); > > > end cutIt; > > > But this isn't a solution. Converting to integer with integer(...) > > round the value. > > Example: > > 12.345 ---> 12 > > 12.854 ---> 13 > > > Is there any other more elegant and functional solution in Ada? > > Back in the good old days of Ada 83, we'd subtract 0.5 from the value before > converting to an integer type. > > Note that a Duration may contain a value that cannot be converted to Integer. > > These days, you can use an appropriate floating-point type and the 'Truncation > attribute function to get the integer part: > > function Cut_It (Value : in Duration) return Duration is > type Big is digits System.Max_Digits; > begin -- Cut_It > return Value - Duration (Big'Truncation (Big (Value) ) ); > end Cut_It; Hmmm ... now I wonder why there isn't a Truncation attribute for fixed- point types? Maybe nobody before Andreas ever thought they'd need one. Might be useful. (Also Floor, Ceiling.) A possible problem with the floating-point conversion solution is that there may be cases where you lose accuracy---if, e.g., the largest floating-point type and fixed-point systems supported on the machine are both 64 bits. Since a floating-point number needs room for an exponent, it's possible to define a fixed-point type with more bits in the integer part than can be represented in the mantissa of a floating- point type, e.g. type Pathological is delta 0.25 range -(2.0**61) .. (2.0**61-0.25); although I don't think that would be a problem with the Duration type. -- Adam