Python 2.5 time.time() as decimal
<p>Is it possible to receive the output of time.time() in Python 2.5 as a Decimal?</p>

<p>If not (and it has to be a float), then is it possible to guarantee that inaccuracy will always be more than (rather than less than) the original value. In other words:</p>

<pre><code>&gt;&gt;&gt; repr(0.1)
'0.10000000000000001' # More than 0.1 which is what I want

&gt;&gt;&gt; repr(0.99)
'0.98999999999999999' # Less than 0.99 which is unacceptable
</code></pre>

<p>Code example:</p>

<pre><code>import math, time

sleep_time = 0.1

while True:
  time_before = time.time()
  time.sleep(sleep_time)
  time_after = time.time()
  time_taken = time_after - time_before
  assert time_taken &gt;= sleep_time, '%r &lt; %r' % (time_taken, sleep_time)
</code></pre>

<p><strong>EDIT:</strong></p>

<p>Now using the following (which does not fail in testing but could still theoretically fail):</p>

<pre><code>import time
from decimal import Decimal

def to_dec(float_num):
  return Decimal('%2f' % float_num)

sleep_time = to_dec(0.1)

while True:
  time_before = to_dec(time.time())
  time.sleep(float(sleep_time))
  time_after = to_dec(time.time())
  time_taken = time_after - time_before
  assert time_taken &gt;= sleep_time, '%r &lt; %r' % (time_taken, sleep_time)
  print 'time_taken (%s) &gt;= sleep_time (%s)' % (time_taken, sleep_time)
</code></pre>
