Skip to content

Commit

Permalink
Fix #928: check .center when subtracting positions
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-rhodes committed Feb 4, 2024
1 parent 0a91609 commit 73cf31f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ Next version
* For convenience, geoids like :data:`~skyfield.toposlib.wgs84` have a
new attribute :data:`~skyfield.toposlib.Geoid.polar_radius`.

* You can no longer subtract two positions unless they have the same
``.center``. Otherwise, a ``ValueError`` is raised. This check has
always been performed when you subtract vector functions, but it was
missing from the position subtraction routine.

v1.47 — 2024 January 13
-----------------------

Expand Down
5 changes: 5 additions & 0 deletions skyfield/positionlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ def __repr__(self):

def __sub__(self, body):
"""Subtract two ICRF vectors to produce a third."""
if self.center != body.center:
raise ValueError(
"you can only subtract two vectors"
" if they both start at the same center"
)
p = self.position.au - body.position.au
if self.velocity is None or body.velocity is None:
v = None
Expand Down
4 changes: 4 additions & 0 deletions skyfield/tests/test_positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def test_subtraction():
assert tuple(p.position.au) == (9, 18, 27)
assert tuple(p.velocity.au_per_d) == (36, 45, 54)

p1.center = 1
with assert_raises(ValueError):
p0 - p1

def test_separation_from_on_scalar():
p0 = ICRF((1, 0, 0))
p1 = ICRF((0, 1, 0))
Expand Down

0 comments on commit 73cf31f

Please sign in to comment.