Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8348205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:37:24+00:00 2026-06-09T07:37:24+00:00

Python’s divmod function works properly, and it is almost what I want. However, its

  • 0

Python’s divmod function works properly, and it is almost what I want. However, its behavior with non-integer numbers needs to be slightly different for an operation that needs to be performed. When running the following code, you might see what it is that is trying to be done.

>>> function = divmod
>>> from math import pi
>>> function(pi * pi, pi) == (pi, 0)
False
>>> 

How can function be defined above such that the final expression evaluates to True, not False? If anyone can figure out how to get (pi, 0) instead of (3.0, 0.4448...), that would be the answer.

Edit 1: Now for a more complicated example, the following code should yield [3, 2, 1, 3, 2, 1].

>>> x = 1 * pi ** 5 + \
        2 * pi ** 4 + \
        3 * pi ** 3 + \
        1 * pi ** 2 + \
        2 * pi ** 1 + \
        3 * pi ** 0
>>> digits = []
>>> while x:
        x, y = function(x, pi)
        digits.append(y)


>>> digits
[0.3989191524449005, 0.2212554774328268, 2.309739581793931, 0.1504440784612413,
2.858407346410207, 1.0]
>>> 

Edit 2: The following shows code that works fine except that it has unexpected but valid output.

import math

def convert_dec_to_pi(number):
    digits = get_pi_digits(number)
    digits, remainder = correct_pi_digits(digits)
    return make_pi_string(digits, remainder)

def get_pi_digits(number):
    digits = []
    while number:
        number, digit = divmod(number, math.pi)
        digits.append(digit)
    digits.reverse()
    return digits

def correct_pi_digits(digits):
    last = len(digits) - 1
    for index, digit in enumerate(digits):
        if index < last and digit % 1 != 0:
            a, b = get_digit_options(digit, digits[index + 1])
            digits[index:index+2] = a if 0 <= a[1] < math.pi else b
    digit, remainder = divmod(digits[-1], 1)
    digits[-1] = digit
    return digits, remainder

def get_digit_options(digit, next_digit):
    a, b = math.floor(digit), math.ceil(digit)
    if a not in range(4):
        return (b, (digit - b) * math.pi + next_digit), None
    if b not in range(4):
        return (a, (digit - a) * math.pi + next_digit), None
    c, d = ((a, (digit - a) * math.pi + next_digit),
            (b, (digit - b) * math.pi + next_digit))
    return (c, d) if digit - a < 0.5 else (d, c)

def make_pi_string(digits, remainder):
    return '{} base \u03C0 + {} base 10'.format(
        ''.join(str(int(d)) for d in digits), remainder)

The following function can be used to reverse the operation and check the results.

import re

def convert_pi_to_dec(string):
    match = re.search('^(\\d+) base \u03C0 \\+ (0\\.\\d+) base 10$', string)
    if not match:
        raise ValueError()
    digits, remainder = match.groups()
    return sum(int(x) * math.pi ** y for y, x in enumerate(reversed(digits))) \
           + float(remainder)

The following code does not raise an AssertionError, so it is evident that everything is working fine.

for n in range(1, 36):
    value = convert_dec_to_pi(n)
    print(value)
    assert convert_pi_to_dec(value) == n

So then this brings me to the following example. The output can be converted back without a problem, but one would expect something slightly different.

>>> convert_dec_to_pi(math.pi * math.pi)
'30 base π + 0.44482644031997864 base 10'
>>> convert_pi_to_dec(_) == math.pi * math.pi
True
>>> 

The string should have been 100 base π + 0.0 base 10. The output is accurate but not “proper” at this point.

Edit 3: The following example may provide some extra insight into what I am after. After running a loop with different powers of π, I would expect all outputs to be 10... base π + 0.0 base 10 in their form. The results are different from this as shown below.

>>> for power in range(20):
    print(convert_dec_to_pi(math.pi ** power))


1 base π + 0.0 base 10
10 base π + 0.0 base 10
30 base π + 0.44482644031997864 base 10
231 base π + 0.8422899173517213 base 10
2312 base π + 0.6461318165449161 base 10
23122 base π + 0.029882968108176033 base 10
231220 base π + 0.0938801130760924 base 10
2312130 base π + 0.7397595138779653 base 10
23121302 base π + 0.3240230542211062 base 10
231213021 base π + 0.017948446735832846 base 10
2312130210 base π + 0.05638670840988885 base 10
23121302100 base π + 0.17714406890720072 base 10
231213021000 base π + 0.5565145054551264 base 10
2312130133130 base π + 0.6366321966964654 base 10
23121301331302 base π + 3.9032618162071486e-05 base 10
231213013313020 base π + 0.00012262302157861615 base 10
2312130133123211 base π + 0.24905356925301847 base 10
23121301331232110 base π + 0.7824248909895828 base 10
231213013312321102 base π + 0.4580601707952492 base 10
2312130133123211021 base π + 0.4390387422112354 base 10
>>> convert_pi_to_dec('2312130133123211021 base π + 0.4390387422112354 base 10')
2791563949.5978436
>>> convert_pi_to_dec('10000000000000000000 base π + 0.0 base 10')
2791563949.5978436
>>> 

Also shown is how the last two strings are equivalent, yet the output should have been in the form of the second string. I find it fascinating that the difference between 10000000000000000000 base π and 2312130133123211021 base π is 0.4390387422112354 base 10, but that difference has a large influence over the representation. The output should have been as shown below.

1 base π + 0.0 base 10
10 base π + 0.0 base 10
100 base π + 0.0 base 10
1000 base π + 0.0 base 10
10000 base π + 0.0 base 10
100000 base π + 0.0 base 10
1000000 base π + 0.0 base 10
10000000 base π + 0.0 base 10
100000000 base π + 0.0 base 10
1000000000 base π + 0.0 base 10
10000000000 base π + 0.0 base 10
100000000000 base π + 0.0 base 10
1000000000000 base π + 0.0 base 10
10000000000000 base π + 0.0 base 10
100000000000000 base π + 0.0 base 10
1000000000000000 base π + 0.0 base 10
10000000000000000 base π + 0.0 base 10
100000000000000000 base π + 0.0 base 10
1000000000000000000 base π + 0.0 base 10
10000000000000000000 base π + 0.0 base 10

Is there something that I am missing, is there a solution to this problem, or should this be considered a fool’s errand?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:37:26+00:00Added an answer on June 9, 2026 at 7:37 am

    You’re looking for an algorithm to determine a non-integer base representation of a floating-point number.

    Wikipedia describes a greedy algorithm due to Rényi and Frougny; here’s an attempt at an implementation:

    from math import log, floor
    def expansion(x, b):
        k = int(floor(log(x) / log(b)))
        d, r = divmod(x / float(b ** k), 1)
        digits = [int(d)]
        for _ in range(k):
            d, r = divmod(b * r, 1)
            digits.append(int(d))
        def rest(b, d, r):
            while r:
                d, r = divmod(b * r, 1)
                yield int(d)
        return digits, rest(b, d, r)
    

    This gives the lexicographically initial expansion; you can get the lexicographically terminal expansion with a little fiddling:

    def expansion(x, b, greedy=True):
        if not greedy:
            m = (floor(b) / (b - 1)) - 1
        k = int(floor(log(x) / log(b)))
        d, r = divmod(x / float(b ** k), 1)
        if not greedy and r < m:
            d, r = d - 1, r + 1
        digits = [int(d)]
        for _ in range(k):
            d, r = divmod(b * r, 1)
            if not greedy and r < m:
                d, r = d - 1, r + 1
            digits.append(int(d))
        def rest(d, r):
            while r:
                d, r = divmod(b * r, 1)
                if not greedy and r < m:
                    d, r = d - 1, r + 1
                yield int(d)
        return digits, rest(d, r)
    

    Unfortunately this still won’t quite work, as OP’s expansion is non-greedy in the first digit but greedy in the last digit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Python) Given two numbers A and B. I need to find all nested groups
Python beginner here, I have a list of lists and want to refer to
Python's file.read() function won't read anything. It always returns '' no matter what's inside
Python has re.escape() if you want to match an arbitrary string literal. In my
Python works on multiple platforms and can be used for desktop and web applications,
Python's module 'random' has a function random.choice random.choice(seq) Return a random element from the
Python 3.2 documentation refers to Collin Winter's functional module which contains function compose :
Python newbie here: I'm writing a market simulation in Python using Pysage, and want
Python docs mention this word a lot and I want to know what it
Python 3 really complicated the whole file reading process, when you have a binary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.