random - Does "math.floor(x)" and "int(x)" produce different results for positive real numbers in Python? -


i have checked few examples on python shell , seem spit out same numbers. in program large set of numbers supposed approximated, apparently produce different results.

i trying write little program simulates movement of object on rectangular plane. that, had write class named "rectangularroom" takes in width , height , creates grid:

class rectangularroom(object): """ rectangularroom represents rectangular region containing clean or dirty tiles.  room has width , height , contains (width * height) tiles. @ particular time, each of these tiles either clean or dirty. """ def __init__(self, width, height):     """     initializes rectangular room specified width , height.      initially, no tiles in room have been cleaned.      width: integer > 0     height: integer > 0     """     self.width = width     self.height = height     self.room_coordinates = []     m in range(self.width):         n in range(self.height):             self.room_coordinates.append((m,n))     self.cleaned = [] def cleantileatposition(self, pos):     """     mark tile under position pos cleaned.      assumes pos represents valid position inside room.      pos: position     """     self.cleaned.append((int(pos.getx()), int(pos.gety())))  def istilecleaned(self, m, n):     """     return true if tile (m, n) has been cleaned.      assumes (m, n) represents valid tile inside room.      m: integer     n: integer     returns: true if (m, n) cleaned, false otherwise     """     assert type (m)== int , type (n) == int     return (m,n) in self.cleaned  def getnumtiles(self):     """     return total number of tiles in room.      returns: integer     """     return self.width*self.height  def getnumcleanedtiles(self):     """     return total number of clean tiles in room.      returns: integer     """     return len(self.cleaned)  def getrandomposition(self):     """      return random position inside room.      returns: position object.     """     return position (random.randrange(0 , self.width), random.randrange(0 , self.height))  def ispositioninroom(self, pos):     """     return true if pos inside room.      pos: position object.     returns: true if pos in room, false otherwise.     """     return (int(pos.getx()), int(pos.gety())) in self.room_coordinates 

as see implemented using int() method , random generator "random.randrange".

in solution, teacher has implemented class using math.floor() function , random generator random.random():

class rectangularroom(object): """ rectangularroom represents rectangular region containing clean or dirty tiles.  room has width , height , contains (width * height) tiles. @ particular time, each of these tiles either clean or dirty. """ def __init__(self, width, height):     """     initializes rectangular room specified width , height.      initially, no tiles in room have been cleaned.      width: integer > 0     height: integer > 0     """     self.width = width     self.height = height     self.tiles = {}     x in range(self.width):         y in range(self.height):             self.tiles[(x, y)] = false  def cleantileatposition(self, pos):     """     mark tile under position pos cleaned.      assumes pos represents valid position inside room.      pos: position     """     x = math.floor(pos.getx())     y = math.floor(pos.gety())     self.tiles[(x, y)] = true  def istilecleaned(self, m, n):     """     return true if tile (m, n) has been cleaned.      assumes (m, n) represents valid tile inside room.      m: integer     n: integer     returns: true if (m, n) cleaned, false otherwise     """     return self.tiles[(m, n)]  def getnumtiles(self):     """     return total number of tiles in room.      returns: integer     """     return self.width * self.height  def getnumcleanedtiles(self):     """     return total number of clean tiles in room.      returns: integer     """     return sum(self.tiles.values())  def getrandomposition(self):     """     return random position inside room.      returns: position object.     """     return position(random.random() * self.width,                     random.random() * self.height)  def ispositioninroom(self, pos):     """     return true if pos inside room.      pos: position object.     returns: true if pos in room, false otherwise.     """     return ((0 <= pos.getx() < self.width)             , (0 <= pos.gety() < self.height)) 

surprisingly, these 2 chunks of code produce different results. want know why happens. int() , floor() should have same effect on positive number , 2 random functions seem produce similar numbers.

well, don't know answer question - suspect return exact same thing. however, think there problem solution might explain output:

in 'cleantileatposition'

self.cleaned.append((int(pos.getx()), int(pos.gety()))) 

in 'getnumcleanedtiles'

return len(self.cleaned) 

this code seems allow tile cleaned more once. not teachers code does, tile can set 'true' once.

(by way, since random.randrange returns integer.. integer conversion should nothing!)

edit: - think type difference worth considering. types 'ints' shouldn't problem.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -