Skip to content

fix(conversions): use integer arithmetic in decimal_to_octal - #15800

Open
shreyanshj10 wants to merge 1 commit into
TheAlgorithms:masterfrom
shreyanshj10:fix-decimal-to-octal-precision
Open

shreyanshj10 wants to merge 1 commit into
TheAlgorithms:masterfrom
shreyanshj10:fix-decimal-to-octal-precision

Conversation

@shreyanshj10

Copy link
Copy Markdown

Describe your change

decimal_to_octal builds its result with floating-point helpers -- math.pow(10, counter) for
the digit position and math.floor(num / 8) for the division -- so it breaks as soon as the values
leave the exact-integer range of a double. The existing doctest compares against oct() but only
up to 512, so none of this was covered.

Three things go wrong:

  • num / 8 is float division, so any num that is not exactly representable gives a wrong
    quotient. The smallest input this gets wrong is 2**53 + 7: float(2**53 + 7) rounds up to
    2**53 + 8, so the first division yields one too many and decimal_to_octal(2**53 + 7)
    returned 0o400000000000000017 instead of 0o400000000000000007. Nothing below 2**53 can
    fail, since every integer there is exactly representable and dividing a double by 8 is exact.
  • math.pow(10, counter) is inexact from 10**23 on, so decimal_to_octal(8**23) returned
    0o100000000000000008388608 instead of 0o100000000000000000000000.
  • math.pow overflows a double past 10**308, so decimal_to_octal(8**309) raised
    OverflowError: math range error.

Using 10**counter and num //= 8 keeps everything in Python's arbitrary-precision integers and
fixes all three. That leaves math unused, so the import goes, and int(octal) is no longer
needed now that octal cannot be a float -- that and the "removes trailing '.0'" comment were
leftovers 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 against oct() over
17,255 inputs -- small values, powers of 2, 8 and 10, every integer either side of 2**53, and
random 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.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants