0

When ever I am trying to do the following in python(compiler that comes in built in mac) 6.0%1.0 , I am getting 0.999999999999 . How to get the answer as 0.0

Here is the code :-

values=30.24
alist=list()
boolean=0
count=0
print 'start'
while(boolean==0):
        print 'before entering',values
        print 'values%1' , (values%1)
        if(values%(1.0)==0.0):
            boolean=1;
        alist.append(int(values))
        count=count+1;
        svalues=(values)-(alist[count-1])
        print svalues,alist[count-1],count
        values=svalues
        if(values==0.0):
            boolean=1;
        else:
            values=1/(values)
print 'end'
Sid ABS
  • 59
  • 1
  • 11
  • 1
    What should I do , if I need the answer of moduls to be 0.0 – Sid ABS Oct 15 '15 at 14:16
  • What are you trying to test for by computing %1.0? – Bathsheba Oct 15 '15 at 14:48
  • It's not for testing , I had to solve a problem. where number needed to rounded of and then, the remaining decimal part needs to reciprocated and the process goes on. Example:- 30.24 a=30(rounding) b=1/0.24 – Sid ABS Oct 15 '15 at 14:51
  • 1
    I ask since I suspect you're testing if a floating point is an integer. There are more computationally stable techniques available for that. But please read the duplicate first. – Bathsheba Oct 15 '15 at 14:52
  • It's not for testing , I had to solve a problem. where number needed to rounded of and then, the remaining decimal part needs to reciprocated and the process goes on. Example:- 30.24 b=30.24 a=30(rounding) b=1/0.24 b=4.16666667 a=4(round) b=1/0.1666667,b=6.0 a=6(round) Here, even though I am getting a=6.0 but b-a (6.0-6), I am getting the answer as 0.999 – Sid ABS Oct 15 '15 at 15:00

0 Answers0