Skip to content

Commit

Permalink
Merge pull request #733 from pv/fix-human-float
Browse files Browse the repository at this point in the history
BUG: fix util.human_float to format negative or non-finite numbers
  • Loading branch information
pv authored Sep 8, 2018
2 parents d069dc4 + bc6c4f2 commit 6301e18
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion asv/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ def human_float(value, significant=3, truncate_small=None, significant_zeros=Fal
"""
if value == 0:
return "0"
elif math.isinf(value) or math.isnan(value):
return "{}".format(value)
elif value < 0:
sign = "-"
value = -value
else:
sign = ""

logv = math.log10(value)
magnitude = int(math.floor(logv)) + 1
Expand All @@ -127,7 +134,7 @@ def human_float(value, significant=3, truncate_small=None, significant_zeros=Fal
else:
fmt = "{{0:.{0}f}}".format(num_digits)

formatted = fmt.format(value)
formatted = sign + fmt.format(value)

if not significant_zeros and '.' in formatted and 'e' not in fmt:
formatted = formatted.rstrip('0')
Expand Down
11 changes: 11 additions & 0 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ def test_human_float():
("0", 0.001, 2, 0),
("0", 0.001, 2, 1),
("0.001", 0.001, 2, 2),

# non-finite
("inf", float('inf'), 1),
("-inf", -float('inf'), 1),
("nan", float('nan'), 1),

# negative
("-1", -1.2345, 1),
("-0.00100", -0.001, 3, None, True),
("-0", -0.001, 2, 1),
("-0.001", -0.001, 2, 2),
]

for item in items:
Expand Down

0 comments on commit 6301e18

Please sign in to comment.