0

I have a problem with if loop. In the script I need to check if difference between i and i+1 row is equal to 0.6 and if yes, then the program should calculate grad[i] value. The problem is that in third circle, even if the condition is fulfilled, calculations grad[i] aren't performed.

This is part of my code:

data = np.loadtxt(files[p])
N=len(data)
print "N: ", N, "\n"
for i in range(0,N-1):
    print "i: ", i
    j=i+1
    Fi[i]=data[i,1]
    errFi[i]=data[i,2]
    Fj[j]=data[j,1]
    errFj[j]=data[j,2]
    t1=data[i,0]
    t2=data[j,0]
    dt=(t2-t1)
    print "dt:", dt
    print Fj[j]-Fi[i]
    if dt == 0.6:
        print " dt =", dt
        if Fi[i] < Fj[j]:
            r[i]=1
            grad[i]=(Fj[j]-Fi[i]) / dt
        else:
            r[i]=-1
            grad[i]=(Fi[i]-Fj[j]) / dt

        print " grad[",i,"]=",grad[i] 

and the result in console is:

i:  0
dt: 0.6
0.148645
dt = 0.6
grad[ 0 ]= 0.247741666667

i:  1
dt: 0.6
0.061069
dt = 0.6
grad[ 1 ]= 0.101781666667

i:  2
dt: 0.6
-0.009578

i:  3
dt: 0.6
0.078995

i:  4
dt: 0.6
0.069982
CDspace
  • 2,639
  • 18
  • 30
  • 36
megan_17
  • 3
  • 2
  • 2
    Floating point is approximate. Just because it prints as `0.6` doesn't mean it's exactly equal to `0.6`. – Barmar Jan 29 '18 at 22:34
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Barmar Jan 29 '18 at 22:35
  • Try using `round(dt, 1) == 0.6` instead. Like @Barmar said, your `dt` will likely not going to be 0.6 sharp. Also, read about floats including the link posted. – atru Jan 29 '18 at 22:48
  • Thank you very much @Barmar and @atru! With `round()` everything works fine! – megan_17 Jan 30 '18 at 09:28

1 Answers1

0

As mentioned in the comments, you are dealing with floating points, which aren't exact. Try working with

data = 100 * np.loadtxt(files[p])

And compare dt to 60 instead. Your value of grad (in this linear computation) should end up ok automatically.

If somehow that doesn't work for you, you can after multiplying with 100 also use round() on dt

niCk cAMel
  • 869
  • 1
  • 10
  • 26
  • [Round](https://docs.scipy.org/doc/numpy/reference/generated/numpy.round_.html) allows you to round to any decimal you want. No need for multiplication of input or any other similar tricks, otherwise useful 20 years ago. – atru Jan 29 '18 at 22:54
  • @atru don't you need to import math for round? – niCk cAMel Jan 29 '18 at 23:00
  • The OP is already using numpy. This round is a part of numpy. Also, anything wrong with importing math? I guess in case you want the least amount of modules possible it would be ok. – atru Jan 29 '18 at 23:08
  • `round` has been a builtin in python for some time, anyway - at least since 2.4. – Nathan Vērzemnieks Jan 29 '18 at 23:09
  • @NathanVērzemnieks hmm... Good to know. I'll leave the answer here for downvoting then. – niCk cAMel Jan 29 '18 at 23:11
  • @atru numpy... True (doh)! Otherwise, yes, it's just a minimalistic POV – niCk cAMel Jan 29 '18 at 23:13
  • Why not edit and just add what was said? I used to do it too until I learned better. I'll delete all the comments if you edit your post. – atru Jan 29 '18 at 23:13
  • Removed the "outdated".. :) It is though.. It used to be the way to go in C for me. I used it even in Matlab back when I was a true chicken still half-way in the egg. – atru Jan 29 '18 at 23:15
  • @atru I don't get why you haven't moved your answer from the comments to an answer. You post the answer, and I'll upvote. I'm just glad I learned that round is built in nowadays – niCk cAMel Jan 29 '18 at 23:15
  • You can do it, I answered something similar recently, I can skip this one ;) – atru Jan 29 '18 at 23:16
  • @atru Haha. But yes, my daily work is in embedded C so multiplication and division in the right order is critical. Matlab? I think Matlab autocompletes to `round()` if you try multiplication of this sort LOL – niCk cAMel Jan 30 '18 at 08:30