"local variable 'x' referenced before assignment" when data become larger
<p>I just wrote a code to decode something. When I try some small number, it works well. However, when I try some big number, it throws a wrong.</p>

<p>The thing what I would like to decode is this:
<a href="https://i.stack.imgur.com/Q1pGa.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Q1pGa.jpg" alt="enter image description here"></a></p>

<pre><code>def decode(n):
    if n == 0:
        return (0, 0)
    circle = int(sqrt(n) / 2 + 0.5)
    largest = (2 * circle + 1) ** 2 - 1
    distance = largest - n
    quotient = distance // (2 * circle)
    remainder = distance % (2 * circle)

    if quotient == 0:
        x = circle- remainder
        y = - circle
    if quotient == 1:
        x = -circle
        y = -circle + remainder
    if quotient == 2:
        x = -circle + remainder
        y = circle
    if quotient == 3:
        x = circle
        y = circle - remainder
    return (x, y)

print(decode(2070)) #(23, 23)    
print(decode(204019576686482721392)) #(7141771, 7141771081)
print(decode(142656302882002193830320)) 
#Traceback (most recent call last):
#  File line 51, in &lt;module&gt;
#    print(decode(142656302882002193830320))
#  File line 47, in decode
#    return (x, y)
#UnboundLocalError: local variable 'x' referenced before assignment
</code></pre>

<p>I also have tried n from 1-14, all the result is right.</p>

<p>I just found the reason that I should provide another <code>int()</code> in sqrt(n).</p>

<pre><code>circle = int(int(sqrt(n) / 2) + 0.5)
</code></pre>

<p>Thanks everyone.</p>