[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to stay in fixnum range?

From: Panicz Maciej Godek
Subject: Re: How to stay in fixnum range?
Date: Wed, 21 Jan 2009 10:54:11 +0100

>> The problem may appear when I add some number to the
>> return value of get-internal-real-time -- what if, during addition,
>> the scheme fixnum limit is exceeded and the variable becomes
>> a bignum?
> It becomes a bignum.  Why is that a problem?
> (Serious question, because the best solution may depend on why this
> actually matters...)

I expect that, after reaching the maximum count (the range of int), the timer
twists back to zero, instead of growing boundlessly.

In that case, the following code may fail:

(define (for-ticks-pass-time-left ticks operation)
  (let ((total-ticks (+ ticks (get-internal-real-time)))
        (elapsed-ticks 0)
        (current-ticks 0)
        (processed-ticks 0))
    (while #t
      (set! current-ticks (get-internal-real-time))
      (set! elapsed-ticks (+ elapsed-ticks (- current-ticks processed-ticks)))
      (set! processed-ticks current-ticks)
      (if (>= elapsed-ticks total-ticks)
      (operation (- total-ticks elapsed-ticks)))
    (- elapsed-ticks total-ticks)))

When the internal counter twists back to zero between the
(get-internal-real-time) in the let assignment and in the
while loop, the total-ticks becomes very large, and the
current-ticks is small, so it may take very very long time
for the loop to execute

If all the operations were performed in the modulo
arithmetics (as they are in low level languages), the
problem would solve automatically.

Obviously I could use the modulo division in all the assignments,
but that would cause additional unnecessary overhead.

Probably the best solution would be to implement the
function in C.

Best regards

reply via email to

[Prev in Thread] Current Thread [Next in Thread]