Skip to content

Commit a0b2ef5

Browse files
Fix incorrect doctest and improve log handling in num_digits functions (#13850)
Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent e2ada02 commit a0b2ef5

1 file changed

Lines changed: 4 additions & 13 deletions

File tree

‎maths/number_of_digits.py‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def num_digits(n: int) -> int:
2121
...
2222
TypeError: Input must be an integer
2323
"""
24-
2524
if not isinstance(n, int):
2625
raise TypeError("Input must be an integer")
2726

@@ -59,22 +58,15 @@ def num_digits_fast(n: int) -> int:
5958
...
6059
TypeError: Input must be an integer
6160
"""
62-
6361
if not isinstance(n, int):
6462
raise TypeError("Input must be an integer")
65-
66-
if n == 0:
67-
return 1
68-
69-
abs_n = abs(n)
70-
digits = math.floor(math.log10(abs_n)) + 1
71-
return digits + 1 if 10**digits <= abs_n else digits
63+
return 1 if n == 0 else math.floor(math.log10(abs(n)) + 1)
7264

7365

7466
def num_digits_faster(n: int) -> int:
7567
"""
7668
Find the number of digits in a number.
77-
abs() is used for negative numbers
69+
abs() is used for negative numbers.
7870
7971
>>> num_digits_faster(12345)
8072
5
@@ -91,7 +83,6 @@ def num_digits_faster(n: int) -> int:
9183
...
9284
TypeError: Input must be an integer
9385
"""
94-
9586
if not isinstance(n, int):
9687
raise TypeError("Input must be an integer")
9788

@@ -100,14 +91,14 @@ def num_digits_faster(n: int) -> int:
10091

10192
def benchmark() -> None:
10293
"""
103-
Benchmark multiple functions, with three different length int values.
94+
Benchmark multiple functions with three different integer sizes.
10495
"""
10596
from collections.abc import Callable
10697

10798
def benchmark_a_function(func: Callable, value: int) -> None:
10899
call = f"{func.__name__}({value})"
109100
timing = timeit(f"__main__.{call}", setup="import __main__")
110-
print(f"{call}: {func(value)} -- {timing} seconds")
101+
print(f"{call}: {func(value)} -- {timing:.6f} seconds")
111102

112103
for value in (262144, 1125899906842624, 1267650600228229401496703205376):
113104
for func in (num_digits, num_digits_fast, num_digits_faster):

0 commit comments

Comments
 (0)