Re: SDL-News: time conversion


Subject: Re: SDL-News: time conversion
From: Rick Reed TSE (rickreed#tseng.co.uk)
Date: Fri Dec 15 2000 - 16:50:25 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 -----

Anthony Rouaux at rouaux#st2e.com wrote on 15/12/2000 08:29:

> I am looking for a way to convert a type time data to a type integer data.

The notation for Time (and Duration) is

('0':'9')+ OR ('0':'9')* '.' ('0':'9')+

examples being 1 27, 55, 2.5, 7.333, .4321

Strictly according to the SDL semantics this cannot be done (in a reasonable
way). The sorts Duration and Time are mappings from the Real sort. The Real
sort can represent any value of the form Float(i/j) where i and j are
Integers. Integers are of unlimited size.

Therefore Real and Duration and Time are each a disjoint set of Rational
numbers. These can be to any precision.

There are mathematical methods (such as the Cantor diagonalisation) for
converting a Rational to an Integer, but these are neither practical nor
useful in this case.

Of course, in reality Integers will be of limited precision and there will
be a limit to the precision of the Time sort.

If you are prepared to loose the fractional part of the Time value, it is
possible to find an Integer, ti, such that for a Time value t:

(t - (Float(ti) * <<TYPE Duration>>1 ) >= 0)
AND (t - (Float(ti) * <<TYPE Duration>>1 ) <= 1) = true

However, to find such an Integer would require a searching method. Obviously
it would be possible to try every Integer in turn, but this would be
inefficient. A better way would be to use an iterative search (left as an
exercise for the reader!).

It seems therefore that currently the only practical way to tackle this
conversion is to know how Time is encoded for a particular application, and
use a conversion procedure written in another language.

I conclude, therefore that it would be useful if SDL had a built in
operator:

Float: Duration -> Real;

such that Float(<<TYPE Duration>>d) = <<TYPE Real>> r
if the spelling(d) == spelling(r)

for example Float(<<TYPE Duration>>12.34) is equivalent to <<TYPE
Real>>12.34

then it would be possible to covert a time value to an Real or Integer:

DCL t Time, r Real, i Integer;

r := Float( t - <<TYPE Time>>0 );

i := Fix(Float( t - <<TYPE Time>>0 ) );

Moreover, I would expect the implementation of this operator to be very
simple as I expect that Time values are actually implemented in exactly the
same way as Reals

--
Rick Reed - rickreed#tseng.co.uk
Tel:+44 1455 55 96 55 Fax:+44 1455 55 96 58 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