Subject: Re: SDL-News: Integer to Time casting
From: Rick Reed TSE (rickreed#tseng.co.uk)
Date: Tue Jul 29 2003 - 11:14:51 GMT
Become an SDL Forum Society member <http://www.sdl-forum.org/Society/members.htm>
The originator of this message is responsible for its content.
-----From Rick Reed TSE <rickreed#tseng.co.uk> to sdlnews -----
Margarita de Cabo Pérez at marcab#yllera.tel.uva.es wrote on 25/07/2003
19:06:
> Hi,
>
> I would like to transform an integer value into a Duration value, and
> viceversa. I understand
> that Duration is in fact a real type, but I can put my integer into a real
> an
> used the last like Duration. I doing the following:
>
> dcl tiempoQueda Duration;
> dcl tiempo Integer;
>
> tiempo:=fix(tiempoQueda);
> What I have to do?
> I'm working with Cinderella.
>
> Marga
Dear Marga,
Multiply is defined for Duration:
"*" (Duration, Real) -> Duration;
"*" (Real, Duration) -> Duration;
and float is defined for Real
float(Integer) -> Real;
so you should be able to write
tiempoQueda := 1*float(tiempo)
or
tiempoQueda := float(tiempo)*1;
I have not tried this and you may need some qualifiers such as
tiempoQueda := <<TYPE Real>>float(tiempo)*<<TYPE Duration>>1;
to make sure the expression is unambiguous.
This converts an the integer value into a Duration value.
Fix is not defined for Duration so
fix(tiempoQueda)
is not a valid expression, so your assignment above is not correct.
It seems to me this is an oversight.
SDL could be extended with
fix(Duration) -> Integer;
float(Duration) -> Real;
although
real(Duration) -> Real;
would be just as useful.
With the current language definition, you would have to write an algorithm
to find the appropriate Integer value. For example,
PROCEDURE fixDuration( d Duration) -> Integer;
{ DCL negative Boolean := false;
DCL i Integer := 0;
IF (d < 0) { negative := true; d := -d;};
LOOP (i, <<TYPE duration>>1*(i+1) < d , i + 1 );
IF (negative) i := -i;
RETURN i;
}
But obviously if the actual durations are more than just a few units this
would be quite inefficient.
tiempo:=CALL fixDuration(tiempoQueda);
should then do the conversion (If I got the algorithm right -- I have not
check the grammar or the logic).
-- Rick Reed - rickreed#tseng.co.uk Tel:+44 15394 88462 Mob.:+44 7970 50 96 50--End text from Rick Reed TSE <rickreed#tseng.co.uk> to sdlnews --- For extra SDL Forum Society benefits join at <http://www.sdl-forum.org/Society/members.htm>
This archive was generated by hypermail 2a23 : Thu May 09 2013 - 16:05:49 GMT