Skip to content

Commit

Permalink
Merge pull request #19 from ngoldbaum/round
Browse files Browse the repository at this point in the history
 implement __round__ for unyt_quantity
  • Loading branch information
Nathan Goldbaum authored Jun 21, 2018
2 parents 324eb85 + 261e669 commit e681fa0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ unyt began life as a submodule of yt named yt.units.

It was separated from yt.units as its own package in 2018.

1.0.4 (2018-08-08)
------------------

* Expand installation instructions
* Mention paper and arxiv submission in the readme.

1.0.3 (2018-06-06)
------------------

Expand Down
3 changes: 3 additions & 0 deletions unyt/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,9 @@ def __new__(cls, input_scalar, input_units=None, registry=None,
raise RuntimeError("unyt_quantity instances must be scalars")
return ret

def __round__(self):
return type(self)(round(float(self)), self.units)


def _validate_numpy_wrapper_units(v, arrs):
if not any(isinstance(a, unyt_array) for a in arrs):
Expand Down
12 changes: 12 additions & 0 deletions unyt/tests/test_unyt_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1906,3 +1906,15 @@ def test_creation():
unyt_quantity('hello', 'cm')
with pytest.raises(RuntimeError):
unyt_quantity(np.array([1, 2, 3]), 'cm')


def test_round():
from unyt import km

assert_equal(round(3.3*km), 3.)
assert_equal(round(3.5*km), 4.)
assert_equal(round(3*km), 3)
assert_equal(round(3.7*km), 4)

with pytest.raises(TypeError):
round([1, 2, 3]*km)

0 comments on commit e681fa0

Please sign in to comment.