bug report: [ #447945 ] time.time() is not non-decreasing
zooko at zooko.com
zooko at zooko.com
Sat Aug 4 18:53:42 EDT 2001
I have committed a cleaned up version of the IncreasingTimer which does less
floating point arithmetic and which contains comments showing exactly what
I don't know about floating point.
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/~checkout~/mojonation/evil/common/timeutil.py?content-type=text/plain
The relevant excerpt of the source is appended.
Regards,
Zooko
class IncreasingTimer:
def __init__(self):
self.lasttime = time.time() # This stores the most recent answer that we returned from `time()'.
self.delta = 0 # We add this to the result from the underlying `time.time()'.
# How big of an increment do we need to add in order to make the new float greater than the old float?
trye = 1.0
while (self.lasttime + trye) > self.lasttime:
olde = trye
trye = trye / 2.0
self._TIME_EPSILON = olde
def time(self):
t = time.time() + self.delta
lasttime = self.lasttime
if t <= lasttime:
self.delta = self.delta + (lasttime - t) + self._TIME_EPSILON
t = lasttime + self._TIME_EPSILON
# XXX if you were sure that you could generate a bigger float in one pass, you could change
# this `while' to an `if' and optimize out a test.
while t <= lasttime:
# We can get into here only if self._TIME_EPSILON is too small to make the time float "tick over" to a new higher value.
# So we (permanently) double self._TIME_EPSILON.
# XXX is doubling epsilon the best way to quickly get a minimally bigger float?
self._TIME_EPSILON = self._TIME_EPSILON * 2.0
# Delta, having smaller magnitude than t, can be incremented by more than t was incremented. (Up to the old epsilon more.) That's OK.
self.delta = self.delta + self._TIME_EPSILON
t = t + self._TIME_EPSILON
self.lasttime = t
return t
More information about the Python-list
mailing list