Skip to content
Open
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
25 changes: 15 additions & 10 deletions geodepy/angles.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

"""

from math import radians
from math import inf, radians


class DECAngle(float):
Expand Down Expand Up @@ -571,6 +571,14 @@ def ddm(self):
return gon2ddm(self.gon_angle)


def _round_with_carry(value, n):
rounded = round(value, n)
if 60 <= rounded < inf:
carry, remainder = divmod(rounded, 60)
return int(carry), remainder
return 0, rounded


class DMSAngle(object):
"""
Class for working with angles in Degrees, Minutes and Seconds format
Expand Down Expand Up @@ -712,10 +720,9 @@ def __str__(self):
)

def __round__(self, n=None):
if self.positive:
return DMSAngle(self.degree, self.minute, round(self.second, n))
else:
return -DMSAngle(self.degree, self.minute, round(self.second, n))
carry, second = _round_with_carry(self.second, n)
degree, minute = divmod(self.minute + carry, 60)
return DMSAngle(self.degree + degree, minute, second, positive=self.positive)

def __mod__(self, other):
return dec2dms(self.dec() % other)
Expand Down Expand Up @@ -931,10 +938,8 @@ def __str__(self):
return "-" + str(self.degree) + " " + str(self.minute)

def __round__(self, n=None):
if self.positive:
return DDMAngle(self.degree, round(self.minute, n))
else:
return DDMAngle(-self.degree, -round(self.minute, n))
degree, minute = _round_with_carry(self.minute, n)
return DDMAngle(self.degree + degree, minute, positive=self.positive)

def __mod__(self, other):
return dec2ddm(self.dec() % other)
Expand Down Expand Up @@ -1035,7 +1040,6 @@ def dec2hp(dec):
:rtype: float
"""
minute, second = divmod(abs(dec) * 3600, 60)
degree, minute = divmod(minute, 60)

# floating point precision is 13 places for the variable 'dec' where values
# are between 256 and 512 degrees. Precision improves for smaller angles.
Expand All @@ -1044,6 +1048,7 @@ def dec2hp(dec):
if round(second, 9) == 60:
second = 0
minute += 1
degree, minute = divmod(minute, 60)

# to avoid precision issues with floating point operations
# a string will be built to represent a sexagesimal number and then converted to float
Expand Down
23 changes: 23 additions & 0 deletions geodepy/tests/test_angles.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,24 @@ def test_DMSAngle(self):
self.assertEqual(DMSAngle(ex), dms_exs[num])
self.assertEqual(ex, str(dms_exs[num]))

def test_dms_round_carries(self):
for positive in (True, False):
for minute, expected in ((12, (0, 13, 0)), (59, (1, 0, 0))):
with self.subTest(positive=positive, minute=minute):
angle = DMSAngle(0, minute, 59.9999, positive=positive)
result = round(angle, 2)
self.assertEqual(
(result.degree, result.minute, result.second), expected
)
self.assertEqual(result.positive, positive)

def test_ddm_round_carries(self):
for positive in (True, False):
with self.subTest(positive=positive):
result = round(DDMAngle(0, 59.9999, positive=positive), 2)
self.assertEqual((result.degree, result.minute), (1, 0))
self.assertEqual(result.positive, positive)

def test_DDMAngle(self):
# Test DDMAngle Methods
for num, ex in enumerate(ddm_exs):
Expand Down Expand Up @@ -541,6 +559,11 @@ def test_dec2hp(self):
self.assertAlmostEqual(hp, dec2hp(dec), 13)
self.assertAlmostEqual(-hp, dec2hp(-dec), 13)

def test_dec2hp_carries(self):
for sign in (1, -1):
with self.subTest(sign=sign):
self.assertEqual(dec2hp(sign * (1 - 1e-13)), sign * 1.0)

def test_dec2hpa(self):
for num, ex in enumerate(dec_exs):
self.assertEqual(dec2hpa(ex), hpa_exs[num])
Expand Down