How many times can you divide 24**36 by 2 until it becomes imprecise?

Question:

How many times can you divide 24**36 by 2 until it becomes imprecise? I had this question on one of my quizzes in a python course, and I’m curious as to what the correct answer is. (It’s already submitted so no cheating here)

I wrote this small bit of code in python to calculate how many times you can evenly divide the number by two until it becomes a decimal number.

count = 0
remainder = 0
num = 24**36
while remainder == 0:
    remainder = num % 2
    if remainder == 0:
        num /= 2
        count += 1
print(count)

I assumed the answer had something to do with floating point imprecision, but I’m not sure what is classified as "imprecise" here. The code above prints 113, since by the 113th division it will become a decimal number with .5 at the end, and I claimed that number is imprecise. Is this approach correct?

Asked By: complicated

||

Answers:

n = 24**36
i = 0
while True:
    a,b = divmod(n,2)
    if a == 1:
        break
    if b > 0:
        break
    i += 1
    n = a
print(i)

I used the built-in divmod that gives back the result of the division and the remainder. Then I simply looped until the remainder was 1 while counting the number of divisions in i.

The output is 108.

You can calculate this without code even:

(24**36)
(2*12)**36  -> 36
(2*6)**36   -> 72
(2*3)**36   -> 108
3**36       -> not divisible by 2 without remainder
Answered By: Gábor Fekete

Never ever use floating point arithmetics for integer values, specialy in Python! In other languages, you may have to use multiprecision library for large values, but Python integer are multiprecision integers…

Now 24 ** 36 = (3 * 2**3) ** 36 = (3 ** 36) * (2 ** 108)

So you can divide it 108 times by 2, and you will get integer values.

At step 109, you will get a floating point value. If you ask math.log2(3**36) in Python, you get 57.058… meaning the it will need more that the maximum precision of a IEE 754 double precision floating point (what Python floats are), because it is only 54 bits.

So the answer is 108.

Answered By: Serge Ballesta

Python has two division operators: / and //, so "divide by 2" is ambiguous.

2436 is 336×2108, and 336 is a little bigger than 257, which means that it requires 58 bits for a precise representation. Since the expression 24**36 is an integer and Python stores integer in an arbitrary precision format, the result can be accurately represented as a Python integer. Floating point numbers, however, are kept as doubles, which (on common architectures) consist of a 53-bit significand and an 11 bit exponent (of the power of two which the significand is multiplied by). In other words, floating point numbers have 53 bits of precision. There’s no problem with the exponent (108 is a lot smaller than the maximum exponent), but you can’t shoehorn a number which needs 58 bits of precision into a precise floating point representation.

The / operator always produces a floating point result. So 24**36/2 (which also requires 58 bits of precision) is already imprecise; you can’t divide by two even once, if you use the / operator.

The // operator always produces an integer result. So 24**36//2 is still an integer, and is still precise. So you can divide by two using // a total of 108 times before you’ve divided off the 2108 factor, leaving an odd number. The next division will then truncate the remainder.

Answered By: rici
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.