Skip to content

Commit c410d51

Browse files
authored
fix formatting of positive numbers with negative exponents (#590)
* correct the formatting for positive numbers with negative exponents and add additional test * add case for negative exponent truncation, add guard for the mantissa * delete unnecessary else statement
1 parent 14786ff commit c410d51

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

bayes_opt/logger.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def _format_number(self, x: float) -> str:
8282
result = ""
8383
width = self._default_cell_size
8484
# Keep negative sign, exponent, and as many decimal places as possible
85-
if "-" in s:
85+
if x < 0:
8686
result += "-"
8787
width -= 1
8888
s = s[1:]
@@ -96,8 +96,6 @@ def _format_number(self, x: float) -> str:
9696
width -= dot_pos
9797
if width > 0:
9898
result += s[dot_pos : dot_pos + width]
99-
else:
100-
result += s[:width]
10199
if "e" in s:
102100
result += end
103101
result = result.ljust(self._default_cell_size)

tests/test_logger.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,14 @@ def test_format_number():
8787
assert len(formatted) == logger._default_cell_size
8888
assert formatted == "1.234e+13"
8989

90-
# Test negative scientific notation truncation
90+
sci_float = 1.11111111e-5
91+
formatted = logger._format_number(sci_float)
92+
assert formatted == "1.111e-05"
93+
94+
sci_float_neg = -1.11111111e-5
95+
formatted = logger._format_number(sci_float_neg)
96+
assert formatted == "-1.11e-05"
97+
9198
sci_float = -12345678901234.5678901234
9299
formatted = logger._format_number(sci_float)
93100
assert len(formatted) == logger._default_cell_size

0 commit comments

Comments
 (0)