Skip to content

Commit 7289f3b

Browse files
committed
sig_fig tests for 0, inf, NaN
1 parent 2fdf73a commit 7289f3b

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

probscale/probscale.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,25 @@ def _sig_figs(cls, x, n, expthresh=5, forceint=False):
8888
8989
"""
9090

91+
# return a string value unaltered
92+
if isinstance(x, str) or x == 0.0:
93+
out = str(x)
94+
9195
# check on the number provided
92-
if x is not None and not np.isinf(x) and not np.isnan(x):
96+
elif x is not None and not np.isinf(x) and not np.isnan(x):
9397

9498
# check on the _sig_figs
9599
if n < 1:
96100
raise ValueError("number of sig figs (n) must be greater than zero")
97101

98-
# return a string value unaltered
99-
if isinstance(x, str):
100-
out = x
101-
102102
elif forceint:
103103
out = '{:,.0f}'.format(x)
104104

105105
# logic to do all of the rounding
106-
elif x != 0.0:
106+
else:
107107
order = np.floor(np.log10(np.abs(x)))
108108

109-
if -1.0 * expthresh <= order <= expthresh:
109+
if (-1.0 * expthresh <= order <= expthresh):
110110
decimal_places = int(n - 1 - order)
111111

112112
if decimal_places <= 0:
@@ -121,9 +121,6 @@ def _sig_figs(cls, x, n, expthresh=5, forceint=False):
121121
fmt = '{0:.%de}' % decimal_places
122122
out = fmt.format(x)
123123

124-
else:
125-
out = str(round(x, n))
126-
127124
# with NAs and INFs, just return 'NA'
128125
else:
129126
out = 'NA'

probscale/tests/test_probscale/test_probscale.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ def test_baseline(self):
8181
nt.assert_equal(self.fmt._sig_figs(self.x, 3), self.known_3)
8282
nt.assert_equal(self.fmt._sig_figs(self.x, 4), self.known_4)
8383

84+
def test_string(self):
85+
nt.assert_equal(self.fmt._sig_figs('1.23', 3), '1.23')
86+
87+
def test_na_inf(self):
88+
nt.assert_equal(self.fmt._sig_figs(np.nan, 3), 'NA')
89+
nt.assert_equal(self.fmt._sig_figs(np.inf, 3), 'NA')
90+
91+
def test_zero(self):
92+
nt.assert_equal(self.fmt._sig_figs(0, 3), '0')
93+
8494
def test_trailing_zeros(self):
8595
nt.assert_equal(self.fmt._sig_figs(self.x, 8), self.known_8)
8696

0 commit comments

Comments
 (0)