Repository navigation
fix(conversions): use integer arithmetic in decimal_to_octal - #15800
Open
shreyanshj10 wants to merge 1 commit into
Open
shreyanshj10 wants to merge 1 commit into
shreyanshj10 wants to merge 1 commit into
Conversation
decimal_to_octal built its result with floating-point helpers: math.pow(10, counter) for the digit position and math.floor(num / 8) for the division. Both lose precision once the values leave the exact-integer range of a double. The smallest input that came out wrong is 2**53 + 7, where the conversion of num to a double rounds up and the quotient chain gains a digit; math.pow also goes inexact from 10**23 and raises OverflowError past 10**308. Use 10**counter and num //= 8 instead, which keeps everything in Python's arbitrary-precision integers and drops the math import, and extend the doctests to cover the three inputs that previously failed.
mindaugl
approved these changes
Nov 24, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change
decimal_to_octalbuilds its result with floating-point helpers --math.pow(10, counter)forthe digit position and
math.floor(num / 8)for the division -- so it breaks as soon as the valuesleave the exact-integer range of a double. The existing doctest compares against
oct()but onlyup to 512, so none of this was covered.
Three things go wrong:
num / 8is float division, so anynumthat is not exactly representable gives a wrongquotient. The smallest input this gets wrong is
2**53 + 7:float(2**53 + 7)rounds up to2**53 + 8, so the first division yields one too many anddecimal_to_octal(2**53 + 7)returned
0o400000000000000017instead of0o400000000000000007. Nothing below2**53canfail, since every integer there is exactly representable and dividing a double by 8 is exact.
math.pow(10, counter)is inexact from10**23on, sodecimal_to_octal(8**23)returned0o100000000000000008388608instead of0o100000000000000000000000.math.powoverflows a double past10**308, sodecimal_to_octal(8**309)raisedOverflowError: math range error.Using
10**counterandnum //= 8keeps everything in Python's arbitrary-precision integers andfixes all three. That leaves
mathunused, so the import goes, andint(octal)is no longerneeded now that
octalcannot be a float -- that and the "removes trailing '.0'" comment wereleftovers from the old scientific-notation fix in #1482.
I added one doctest per failure mode, using the exact boundary value for the first so the limit is
documented. All three fail on the current code and pass with this change, and the existing doctest
and the rest of
conversions/are unaffected. I also compared the function againstoct()over17,255 inputs -- small values, powers of 2, 8 and 10, every integer either side of
2**53, andrandom integers up to 399 bits: the current code disagrees on 10,693 of them and this version on
none. As a second check, the fixed output also matches what this repo's own
decimal_to_any(n, 8)returns.Checklist