-1

I have an if statement within a for loop which runs through a number values of x and calculates a value of h based on the x. I have included the code below. At time zero (t=0.0) xa=-0.0 and xb =0.0.

The problem arrises when x=0. At x=0 I want the code to follow the first if statement as x is less than or equal to 0. However it doesnt it goes to the last if statement and carries that out. Now my thinking is that the problem is something to do with x= 0 and xa=-0.0 and python doesn't like this?

If anyone could shed any light on the subject as to why this occurs and if there is a solution to the problem or if indeed there is a different problem, that would be great.

__author__="ahe"
__date__ ="$05-Aug-2014 11:22:44$"

import numpy as np
import matplotlib.pyplot as plt 
import math 
import sys
from math import sqrt  
import decimal
import pylab 

nx, ny = (100,100)
x5 = np.linspace(-2000,2000,nx)
y5 = np.linspace(0,600,ny)
xv,yv = np.meshgrid(x5,y5)
x = np.arange(-2000.0,2001.0,1.0)
print len (x)

h0=0.03
g=9.81
t=0.0

term1=4.0/(9.0*g)
term2=math.sqrt(g*h0)
print 'term1=',term1,'term2=',term2 

xa=-term2*t
xb=term2*2*t

h=np.zeros(len(x))

for i in range (len(x)):
   if x[i]<=xa:
      h[i]=h0
   elif (xa<x[i]<xb):
      h[i]=term1*((term2-(x[i]/(t*2.0)))**2.0)
   else:
      h[i]=0


print 'xa=',xa,'xb=',xb

h1=np.zeros(len(x))

f = open(r'C:\opentelemac\bluetang\examples\telemac2d\ritter\4D0.i3s', 'r')
while True:
   line = f.readline()
   if line[0] not in [':','#']: break
ran = int(line.split()[0])
length = np.zeros(ran)
wse =  np.zeros(ran)
for i in range (ran):
   fields = f.readline().split()
   length[i] = float(fields[0])
   wse[i] = float(fields[2])
   all =[length[i],wse[i]]

print x[1995:2005]
print h[1995:2005]



plt.figure(2)
plt.plot(length,h, marker='o', linestyle='--')
plt.plot(length,wse)
plt.legend(['Analytical solution_0','Model_0'], loc='upper right')
plt.show()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user3771983
  • 37
  • 2
  • 9
  • 1
    Are you sure you have exactly `0` and `-0.0`? – user2357112 Aug 11 '14 at 09:08
  • 1
    Welcome to the wonderful world of floating point. I suspect that the xa variable, "-0.0", is an actual value that is close to zero, but is a minuscule amount less than zero. For example, '-0.00000000000000000000000000001" and the debugger and python print statements just render it as "-0.0". Hence, the expression `0 <= xa` isn't actually true. – selbie Aug 11 '14 at 09:12
  • Try debugger and see, what is value of `x[i] - xa`. It is definitely not zero. – Jan Vlcinsky Aug 11 '14 at 09:13
  • Ok thanks for the swift replies, that makes sense. I will try the debugger. Is there a way of estbalishign what the number is exactly rather than the rounded version of the number? – user3771983 Aug 11 '14 at 09:13
  • http://stackoverflow.com/a/11950951/674039 – wim Aug 11 '14 at 09:17
  • Ok when i = 2000 (x=0). x[i] - xa = 0.0. Thoughts @JanVlcinsky – user3771983 Aug 11 '14 at 09:17
  • possible duplicate of [How should I do floating point comparison?](http://stackoverflow.com/questions/4915462/how-should-i-do-floating-point-comparison) – WBAR Aug 11 '14 at 09:23
  • Ive resolved the issue now with the help of the floating point comparison link. Im new to python and was searching for a different problem, thanks for all your help. – user3771983 Aug 11 '14 at 13:28

1 Answers1

2

Actually it is true

Python 2.7.4 (default, Sep 26 2013, 03:20:26) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 0 <= -0.0
True

So you probably did not diagnose your problem correctly.

Bartosz Marcinkowski
  • 6,651
  • 4
  • 39
  • 69
  • @user3771983 Bartosz told it very well - you shall invest some effort into how to diagnose problems. Very good debugger intro is at [PMotW - pdb](http://pymotw.com/2/pdb/index.html). – Jan Vlcinsky Aug 11 '14 at 09:17