Skip to content

Commit 5b8cc8a

Browse files
committed
Fix tests that change the context (#92)
* Fix context-modifying tests to reset context at tearDown * Fix doctests that assumed double precision (cherry picked from commit e676476)
1 parent e4c7fc7 commit 5b8cc8a

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

bigfloat/core.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,16 +1016,17 @@ def pos(x, context=None):
10161016
temporary increase in precision, back to the current context. For
10171017
example::
10181018
1019-
>>> from bigfloat import precision
1020-
>>> pow(3, 20) + 1.234 - pow(3, 20) # inaccurate due to precision loss
1021-
BigFloat.exact('1.2340002059936523', precision=53)
1022-
>>> with precision(100): # compute result with extra precision
1023-
... x = pow(3, 20) + 1.234 - pow(3, 20)
1019+
>>> from bigfloat import pos, pow, precision
1020+
>>> pow(3, 60) + 1.234 - pow(3, 60) # inaccurate due to precision loss
1021+
BigFloat.exact('1.23400115966796875000000000000000000', precision=113)
1022+
>>> with precision(200): # compute result with extra precision
1023+
... x = pow(3, 60) + 1.234 - pow(3, 60)
10241024
...
10251025
>>> x
1026-
BigFloat.exact('1.2339999999999999857891452847980', precision=100)
1026+
BigFloat.exact('1.2339999999999999857891452847979962825775146484375000000000000', precision=200)
10271027
>>> pos(x) # round back to original precision
1028-
BigFloat.exact('1.2340000000000000', precision=53)
1028+
BigFloat.exact('1.23399999999999998578914528479799628', precision=113)
1029+
10291030
10301031
"""
10311032
return _apply_function_in_current_context(
@@ -1770,23 +1771,23 @@ def atan2(y, x, context=None):
17701771
>>> inf = BigFloat('inf')
17711772
17721773
>>> print(atan2(+0.0, -0.0)) # pi
1773-
3.1415926535897931
1774+
3.14159265358979323846264338327950280
17741775
>>> print(atan2(+0.0, +0.0)) # 0
17751776
0
17761777
>>> print(atan2(+0.0, negative)) # pi
1777-
3.1415926535897931
1778+
3.14159265358979323846264338327950280
17781779
>>> print(atan2(+0.0, positive)) # 0
17791780
0
17801781
>>> print(atan2(positive, 0.0)) # pi / 2
1781-
1.5707963267948966
1782+
1.57079632679489661923132169163975140
17821783
>>> print(atan2(inf, -inf)) # 3*pi / 4
1783-
2.3561944901923448
1784+
2.35619449019234492884698253745962710
17841785
>>> print(atan2(inf, inf)) # pi / 4
1785-
0.78539816339744828
1786+
0.785398163397448309615660845819875699
17861787
>>> print(atan2(inf, finite)) # pi / 2
1787-
1.5707963267948966
1788+
1.57079632679489661923132169163975140
17881789
>>> print(atan2(positive, -inf)) # pi
1789-
3.1415926535897931
1790+
3.14159265358979323846264338327950280
17901791
>>> print(atan2(positive, +inf)) # 0
17911792
0
17921793
@@ -2333,8 +2334,8 @@ def ceil(x, context=None):
23332334
the current context. Note that the rounding step means that it's possible
23342335
for the result to be smaller than ``x``. For example::
23352336
2336-
>>> x = 2**100 + 1
2337-
>>> ceil(2**100 + 1) >= x
2337+
>>> x = 2**1000 + 1
2338+
>>> ceil(2**1000 + 1) >= x
23382339
False
23392340
23402341
One way to be sure of getting a result that's greater than or equal to

bigfloat/test/test_bigfloat.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
# ... and functions
5555
IEEEContext, precision,
5656

57-
# set current context
57+
# get and set current context
58+
getcontext,
5859
setcontext,
5960

6061
# flags
@@ -190,8 +191,13 @@ class PoorObject(object):
190191

191192
class BigFloatTests(unittest.TestCase):
192193
def setUp(self):
194+
self._original_context = getcontext()
193195
setcontext(DefaultTestContext)
194196

197+
def tearDown(self):
198+
setcontext(self._original_context)
199+
del self._original_context
200+
195201
def test_version(self):
196202
self.assertIsInstance(__version__, str)
197203

@@ -1908,8 +1914,13 @@ def test_divide_by_zero(self):
19081914

19091915
class ABCTests(unittest.TestCase):
19101916
def setUp(self):
1917+
self._original_context = getcontext()
19111918
setcontext(DefaultTestContext)
19121919

1920+
def tearDown(self):
1921+
setcontext(self._original_context)
1922+
del self._original_context
1923+
19131924

19141925
def mpfr_set_str2(rop, s, base, rnd):
19151926
"""Set value of rop from the string s, using given base and rounding mode.

0 commit comments

Comments
 (0)