views:

370

answers:

4
import math
t=raw_input()
k=[]
a=0
for i in range(0,int(t)):
    s=raw_input()
    b=1
    c=1
    a=int(s)
    if a==0:
        continue
    else:
        d=math.atan(float(1)/b) + math.atan(float(1)/c)
        v=math.atan(float(1)/a)
        print v
        print d
        print float(v)
        print float(d)
        while():
            if float(v)== float(d):
                break
            b=b+1
            c=c+1
            d=math.atan(float(1)/float(b)) + math.atan(float(1)/float(c))
            print d
        k.append(int(b)+int(c))

for i in range(0,int(t)):
    print k[i]

as it's very evident float(v) != float(d) till b becomes 2 and c becomes 3.

+6  A: 

Your while loop tests on an empty tuple, which evaluates to False. Thus, the statements within the while loop will never execute:

If you want your while loop to run until it encounters a break statement, do this:

while True:
    if (some_condition):
        break
    else:
        # Do stuff...
Triptych
if float(v)== float(d): breakits this part thats getting executed !!
mekasperasky
oops it got executed ..!! thanks but true aint a keyword!!
mekasperasky
True and False are python keywords, not true or false.
Triptych
Minor correction: while does not have an "empty condition", there is no such thing in Python. "while ():" tests empty tuple "()" and, yes, empty tuple evaluates to false. Python has the same behavior for "while []:", "while '':", "while 0:", etc.
Constantin
A: 

Well, it didn't reach the break point. The problem is that while() does not loop at all. To do an infinite loop, do while (1): (since the while condition must evaluate to true. Here's a working (cleaned up) sample.

import math
t = raw_input()
k = []
a = 0.0
for i in range(0,int(t)):
    s = float(raw_input())
    b = 1.0
    c = 1.0
    a= float(s)
    if a == 0:
        continue
    else:
        d = math.atan(1.0/b) + math.atan(1.0/c)
        v = math.atan(1.0/a)
        print v
        print d
        while True:
            if v == d:
                print 'bar'
                break
            b += 1
            c += 1
            d = math.atan(1.0/b) + math.atan(1.0/c)
            print d
        k.append(int(b)+int(c))

for i in range(0,int(t)):
    print k[i]
mikl
FWIW: "while True" is preferable to "while (1)" in Python.
David Zaslavsky
Oh, right, fixed :)
mikl
+2  A: 

If is very dangerous to make comparsisons like float(a)==float(b) since float variables have no exact representation. Due to rounding errors you may not have identic values.

Even 2*0.5 may not be equal 1. You may use the following:

if abs(float(a)-float(b)) < verySmallValue:
codymanix
+2  A: 

http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

Floating point math is not exact. Simple values like 0.2 cannot be precisely represented using binary floating point numbers, and the limited precision of floating point numbers means that slight changes in the order of operations can change the result. Different compilers and CPU architectures store temporary results at different precisions, so results will differ depending on the details of your environment. If you do a calculation and then compare the results against some expected value it is highly unlikely that you will get exactly the result you intended. In other words, if you do a calculation and then do this comparison: if (result == expectedResult)

then it is unlikely that the comparison will be true. If the comparison is true then it is probably unstable – tiny changes in the input values, compiler, or CPU may change the result and make the comparison be false.

Dustin Getz