From bc6c4f225f290f5bd7e40c38946b1f6fd15cf2f6 Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Sat, 8 Sep 2018 00:40:46 +0200 Subject: [PATCH] BUG: fix util.human_float to format negative or non-finite numbers --- asv/util.py | 9 ++++++++- test/test_util.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/asv/util.py b/asv/util.py index c7a58e76c..42688aff6 100644 --- a/asv/util.py +++ b/asv/util.py @@ -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 @@ -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') diff --git a/test/test_util.py b/test/test_util.py index 364bcf286..ccd88326b 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -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: