comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org>
Subject: Re: Modify value - type duration
Date: Wed, 05 Nov 2008 22:47:59 GMT
Date: 2008-11-05T22:47:59+00:00	[thread overview]
Message-ID: <zopQk.422057$yE1.185550@attbi_s21> (raw)
In-Reply-To: <7ef68540-6df1-4e47-b425-ab08c03f2db9@p31g2000prf.googlegroups.com>

Andreas.Schmidl@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;

-- 
Jeff Carter
"You a big nose have it."
Never Give a Sucker an Even Break
107



  reply	other threads:[~2008-11-05 22:47 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 [this message]
2008-11-06  1:03   ` Adam Beneschan
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