Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix integer division for duration #59

Merged
merged 2 commits into from
Apr 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 2 additions & 20 deletions src/genpy/rostime.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"""

import sys
import warnings

if sys.version > '3':
long = int
Expand Down Expand Up @@ -395,15 +394,7 @@ def __floordiv__(self, val):
:returns: :class:`Duration` divided by val - a :class:`Duration` if divided by a number, or a number if divided by a duration
"""
t = type(val)
if t in (int, long):
warnings.warn(
'The Duration.__floordiv__(integer) function is ill-defined. '
'The floor operation is applied independently on the seconds as well as the nanoseconds. '
'For rounding down to the nearest second use a float divisor. '
'For true division use the operator `/` (which requires `from __future__ import division` in Python 2) or `operator.truediv` instead.',
category=RuntimeWarning)
return Duration(self.secs // val, self.nsecs // val)
elif t == float:
if t in (float, int, long):
return Duration.from_sec(self.to_sec() // val)
elif isinstance(val, Duration):
return self.to_sec() // val.to_sec()
Expand All @@ -416,17 +407,8 @@ def __div__(self, val):
:param val: division factor ``int/float``, or :class:`Duration` to divide by
:returns: :class:`Duration` divided by val - a :class:`Duration` if divided by a number, or a number if divided by a duration
"""
# unlike __floordiv__, this uses true div for float arg.
# PEP 238
t = type(val)
if t in (int, long):
warnings.warn(
'The Duration.__div__(integer) function is ill-defined. '
'The floor operation is applied independently on the seconds as well as the nanoseconds. '
'For true division use a float divisor or `from __future__ import division` in Python 2 or `operator.truediv` instead.',
category=RuntimeWarning)
return Duration(self.secs // val, self.nsecs // val)
elif t == float:
if t in (float, int, long):
return Duration.from_sec(self.to_sec() / val)
elif isinstance(val, Duration):
return self.to_sec() / val.to_sec()
Expand Down
17 changes: 4 additions & 13 deletions test/test_genpy_rostime.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,27 +418,18 @@ def test_Duration(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
self.assertEquals(Duration(4), Duration(8) // 2)
self.assertEqual(len(w), 1)
self.assert_(issubclass(w[-1].category, RuntimeWarning))
self.assertEquals(Duration(4), Duration(8) // 2.)
self.assertEqual(len(w), 1)
self.assertEquals(Duration(4), Duration(9) // 2)
self.assertEqual(len(w), 2)
self.assert_(issubclass(w[-1].category, RuntimeWarning))
self.assertEquals(Duration(4), Duration(9) // 2.)
self.assertEqual(len(w), 2)
self.assertEqual(len(w), 0)
self.assertEquals(Duration(4, 2), Duration(8, 4) / 2)
v = Duration(4, 2) - (Duration(8, 4) / 2.)
self.assert_(abs(v.to_nsec()) < 100)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
self.assertEquals(Duration(4, 2), Duration(8, 4) // 2)
self.assertEqual(len(w), 1)
self.assert_(issubclass(w[-1].category, RuntimeWarning))
self.assertEquals(Duration(4, 2), Duration(9, 5) // 2)
self.assertEqual(len(w), 2)
self.assert_(issubclass(w[-1].category, RuntimeWarning))
self.assertEquals(Duration(4, 0), Duration(8, 4) // 2)
self.assertEquals(Duration(4, 0), Duration(9, 5) // 2)
v = Duration(4, 2) - (Duration(9, 5) // 2.)
self.assert_(abs(v.to_nsec()) < 100)
self.assertEqual(len(w), 2)
self.assertEqual(len(w), 0)
4 changes: 2 additions & 2 deletions test/test_genpy_rostime_truediv.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def test_Duration(self):
v = Duration(4, 2) - (Duration(8, 4) / 2.)
self.assert_(abs(v.to_nsec()) < 100)

self.assertEquals(Duration(4, 2), Duration(8, 4) // 2)
self.assertEquals(Duration(4, 2), Duration(9, 5) // 2)
self.assertEquals(Duration(4, 0), Duration(8, 4) // 2)
self.assertEquals(Duration(4, 0), Duration(9, 5) // 2)
v = Duration(4, 2) - (Duration(9, 5) // 2.)
self.assert_(abs(v.to_nsec()) < 100)