comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Modify value - type duration
Date: Wed, 5 Nov 2008 17:03:50 -0800 (PST)
Date: 2008-11-05T17:03:50-08:00	[thread overview]
Message-ID: <6f0d9434-9489-4946-b29f-60957c83858c@w1g2000prk.googlegroups.com> (raw)
In-Reply-To: zopQk.422057$yE1.185550@attbi_s21

On Nov 5, 2:47 pm, "Jeffrey R. Carter"
<spam.jrcarter....@spam.acm.org> 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



  reply	other threads:[~2008-11-06  1:03 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-05 21:50 Modify value - type duration Andreas.Schmidl
2008-11-05 22:47 ` Jeffrey R. Carter
2008-11-06  1:03   ` Adam Beneschan [this message]
2008-11-06  1:57     ` Jeffrey R. Carter
2008-11-06 21:14       ` Keith Thompson
2008-11-07  1:33       ` Randy Brukardt
2008-11-07 11:10         ` Ludovic Brenta
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox