[M3devel] ROUND vs. FLOOR in ThreadPThread.m3 nano vs. micro time conversions?
Jay K
jay.krell at cornell.edu
Fri Apr 9 12:52:09 CEST 2010
Why does UTimeFromTime use FLOOR but ToNTime uses ROUND?
PROCEDURE ToNTime (n: LONGREAL; VAR ts: Utime.struct_timespec) =
BEGIN
ts.tv_sec := TRUNC(n);
ts.tv_nsec := ROUND((n - FLOAT(ts.tv_sec, LONGREAL)) * 1.0D9);
END ToNTime;
TYPE UTime = Utime.struct_timeval;
PROCEDURE UTimeFromTime (time: Time.T): UTime =
VAR floor := FLOOR(time);
BEGIN
RETURN UTime{floor, FLOOR(1.0D6 * (time - FLOAT(floor, LONGREAL)))};
END UTimeFromTime;
I've changed this code to pass doubles to C and do the conversion
there. To reduce/eliminate use of cloned headers.
For ROUND I'm using C99:
/* C99, very portable */
long lround(double);
long long llround(double);
#ifdef __CYGWIN__
#define ROUND lround
#else
#define ROUND llround
#endif
typedef double FloatSeconds, LONGREAL;
typedef struct timeval MicrosecondsStruct_t;
#define FLOAT(value, type) ((type)value)
#define FLOOR floor
static
MicrosecondsStruct_t*
FloatSecondsToNanosecondsStruct(LONGREAL n,
MicrosecondsStruct_t* ts)
{
ts->tv_sec = (time_t)n;
ts->tv_nsec = ROUND((n - FLOAT(ts->tv_sec, LONGREAL)) * 1.0E9);
return ts;
}
static
MicrosecondsStruct_t*
FloatSecondsToMicrosecondsStruct(FloatSeconds timeout,
MicrosecondsStruct_t* utime)
{
LONGINT sec = FLOOR(timeout);
utime->tv_sec = (time_t)sec;
utime->tv_usec = (suseconds_t)FLOOR(1.0E6 * (timeout - FLOAT(sec, LONGREAL)));
return utime;
}
Just arbitrarily a little more correct in the nano case vs. the micro case?
Matters little? Would matter more for micro due to lower resolution?
Thanks,
- Jay
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20100409/a396ffc5/attachment-0001.html>
More information about the M3devel
mailing list