Repository navigation
fix(maths): correct float rounding in num_digits_fast for large integers #15754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,14 @@ def num_digits_fast(n: int) -> int: | |
| 4 | ||
| >>> num_digits_fast(10**15) | ||
| 16 | ||
| >>> num_digits_fast(10**16 - 1) | ||
| 16 | ||
| >>> num_digits_fast(10**16) | ||
| 17 | ||
| >>> num_digits_fast(10**50 - 1) | ||
| 50 | ||
| >>> num_digits_fast(-(10**16 - 1)) | ||
| 16 | ||
| >>> num_digits_fast(0) | ||
| 1 | ||
| >>> num_digits_fast(-1) | ||
|
|
@@ -60,7 +68,16 @@ def num_digits_fast(n: int) -> int: | |
| """ | ||
| if not isinstance(n, int): | ||
| raise TypeError("Input must be an integer") | ||
| return 1 if n == 0 else math.floor(math.log10(abs(n)) + 1) | ||
| if n == 0: | ||
| return 1 | ||
|
|
||
| abs_n = abs(n) | ||
| digits = math.floor(math.log10(abs_n) + 1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The correction below makes this safe: I compared the function with len(str(abs(n))) for 10k - 1, 10k and 10**k + 1 for every k up to 599, and for 200,000 random values with up to 300 digits, with no mismatches. One small thought: floor(log10(abs_n)) + 1 and floor(log10(abs_n) + 1) can differ by float rounding right before a power of ten, so the code relies entirely on the correction. A one-line comment saying so would help readers. |
||
| if abs_n < 10 ** (digits - 1): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a short comment above the correction explaining why it exists? For example: "log10 on a float can be off by one near powers of ten, since ints above 2**53 lose precision when converted to float; correct it exactly with integer comparisons." The explanation is easy to lose later, and without it the next reader may remove the check as redundant. |
||
| digits -= 1 | ||
| elif abs_n >= 10**digits: | ||
| digits += 1 | ||
| return digits | ||
|
|
||
|
|
||
| def num_digits_faster(n: int) -> int: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new doctests look good, including 1050 - 1 and the negative case. One more worth adding is 1015 - 1 (expected 15): in my check the plain floor(log10(n) + 1) formula is already wrong from that value onward, so it is the first value the correction step actually has to fix. Also 10**15 itself is covered by the existing doctest, which is a good neighbour for it.