Skip to content

Commit ca1e193

Browse files
KennyKenny
authored andcommitted
integer math Durations
If you specify your interval as asynccp.time.Duration.of_nanoseconds() there will be no unit conversion math done on the interval provided by your code. Also reduces the quantity of floating point math in other places to avoid cascading inaccuracies more than necessary.
1 parent 03eec62 commit ca1e193

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

asynccp/loop.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ class ScheduledTask:
5959
def change_rate(self, frequency):
6060
""" Update the task rate to a new frequency. Float hz or asynccp.time.Duration interval """
6161
if isinstance(frequency, asynccp.time.Duration):
62-
hz = frequency.as_frequency()
62+
self._nanoseconds_per_invocation = frequency.as_nanoseconds()
6363
else:
6464
hz = frequency
65-
self._nanoseconds_per_invocation = (1 / hz) * 1000000000
65+
self._nanoseconds_per_invocation = 1000000000 / hz
6666

6767
def stop(self):
6868
""" Stop the task (does not interrupt a currently running task) """

asynccp/time.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,48 @@
22
class Duration:
33
@staticmethod
44
def of_hours(count):
5-
return Duration(count * 60 * 60 * 1000000)
5+
return Duration(count * 60 * 60 * 1000000000)
66

77
@staticmethod
88
def of_minutes(count):
9-
return Duration(count * 60 * 1000000)
9+
return Duration(count * 60 * 1000000000)
1010

1111
@staticmethod
1212
def of_seconds(count):
13-
return Duration(count * 1000000)
13+
return Duration(count * 1000000000)
1414

1515
@staticmethod
1616
def of_milliseconds(count):
17-
return Duration(count * 1000)
17+
return Duration(count * 1000000)
1818

1919
@staticmethod
2020
def of_microseconds(count):
2121
return Duration(count)
2222

23-
def __init__(self, microseconds):
24-
self._microseconds = int(microseconds)
23+
def __init__(self, nanoseconds):
24+
self._nanoseconds = int(nanoseconds)
2525

2626
def __add__(self, other):
27-
return Duration(self._microseconds + other._microseconds)
27+
return Duration(self._nanoseconds + other._nanoseconds)
2828

2929
def __sub__(self, other):
30-
return Duration(self._microseconds - other._microseconds)
30+
return Duration(self._nanoseconds - other._nanoseconds)
3131

3232
def __neg__(self):
33-
return Duration(-1 * self._microseconds)
33+
return Duration(-1 * self._nanoseconds)
3434

3535
def as_frequency(self):
3636
"""returns this duration as a frequency interval in HZ"""
37-
return 1000000.0 / self._microseconds
37+
return 1000000000.0 / self._nanoseconds
3838

3939
def as_seconds(self):
40-
return self._microseconds / 1000000.0
40+
return self._nanoseconds / 1000000000.0
4141

4242
def as_milliseconds(self):
43-
return self._microseconds / 1000.0
43+
return self._nanoseconds / 1000000.0
4444

4545
def as_microseconds(self):
46-
return self._microseconds
46+
return self._nanoseconds / 1000.0
4747

4848
def as_nanoseconds(self):
49-
return self._microseconds * 1000
49+
return self._nanoseconds

0 commit comments

Comments
 (0)