Skip to content

Commit

Permalink
Fixing errounous rounding in Duration. Fix docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
yck011522 committed Apr 12, 2024
1 parent 314bfce commit 5782eb7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
46 changes: 33 additions & 13 deletions src/compas_fab/robots/time_.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,38 @@


class Duration(Data):
"""Duration consists of two values: seconds (float) and nanoseconds (int).
The total number of seconds is the sum of these values.
The decimal portion of the secs variable is converted to an integer and added to nsecs.
"""Duration is used to accurately describe the passage of time.
It consists of seconds and nanoseconds, the total duration is the sum of the two values.
Attributes
Parameters
----------
secs: float
Float representing number of seconds.
secs: int or float
Integer representing number of seconds.
If a float is passed, the integer portion is assigned to secs and
the decimal portion of the secs variable is converted and added to nsecs.
nsecs: int
Integer representing number of nanoseconds.
Attributes
----------
seconds: float, read-only
Returns the total duration as floating-point seconds.
Examples
--------
>>> d = Duration(2, 5e8)
>>> d.seconds
2.5
>>> d = Duration(2.6, 0)
>>> d.seconds
2.6
>>> d = Duration(2.6, 5e8)
>>> d.secs
3
>>> d.nsecs
100000000
>>> d.seconds
3.1
"""

def __init__(self, secs, nsecs):
Expand All @@ -30,6 +52,11 @@ def __init__(self, secs, nsecs):
self.secs = int(quotient)
self.nsecs = int(remainder * sec_to_nano_factor) + int(nsecs)

# If nsecs is greater than 1 second, add the remainder back to secs
if self.nsecs >= sec_to_nano_factor:
self.secs += 1
self.nsecs -= int(sec_to_nano_factor)

def __str__(self):
return "Duration({!r}, {!r})".format(self.secs, self.nsecs)

Expand All @@ -47,13 +74,6 @@ def __ne__(self, other):

@property
def seconds(self):
"""Returns the duration as floating-point seconds.
Returns
-------
float
Floating-point seconds
"""
return self.secs + 1e-9 * self.nsecs

@property
Expand Down
2 changes: 2 additions & 0 deletions tests/robots/test_duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def test_ctor_takes_sec_as_float():

def test_sec_remainder_add_to_nsec():
d = Duration(2.6, 5e8)
assert d.secs == 3
assert d.nsecs == 1e8
assert d.seconds == 3.1


Expand Down

0 comments on commit 5782eb7

Please sign in to comment.