Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes "math.floor(x)" and "int(x)" produce different results for positive real numbers in Python?
    primarykey
    data
    text
    <p>I have checked a few examples on the Python shell and they seem to spit out same numbers. But in a program where large set of numbers are supposed to be approximated, they apparently produce different results.</p> <p>I am trying to write a little program that simulates movement of an object on a rectangular plane. For that, I had to write a class named "RectangularRoom" that takes in a width and a height and creates a grid:</p> <pre><code>class RectangularRoom(object): """ A RectangularRoom represents a rectangular region containing clean or dirty tiles. A room has a width and a height and contains (width * height) tiles. At any particular time, each of these tiles is either clean or dirty. """ def __init__(self, width, height): """ Initializes a rectangular room with the specified width and height. Initially, no tiles in the room have been cleaned. width: an integer &gt; 0 height: an integer &gt; 0 """ self.width = width self.height = height self.room_coordinates = [] for m in range(self.width): for n in range(self.height): self.room_coordinates.append((m,n)) self.cleaned = [] def cleanTileAtPosition(self, pos): """ Mark the tile under the position POS as cleaned. Assumes that POS represents a valid position inside this room. pos: a Position """ self.cleaned.append((int(pos.getX()), int(pos.getY()))) def isTileCleaned(self, m, n): """ Return True if the tile (m, n) has been cleaned. Assumes that (m, n) represents a valid tile inside the room. m: an integer n: an integer returns: True if (m, n) is cleaned, False otherwise """ assert type (m)== int and type (n) == int return (m,n) in self.cleaned def getNumTiles(self): """ Return the total number of tiles in the room. returns: an integer """ return self.width*self.height def getNumCleanedTiles(self): """ Return the total number of clean tiles in the room. returns: an integer """ return len(self.cleaned) def getRandomPosition(self): """ Return a random position inside the room. returns: a Position object. """ return Position (random.randrange(0 , self.width), random.randrange(0 , self.height)) def isPositionInRoom(self, pos): """ Return True if pos is inside the room. pos: a Position object. returns: True if pos is in the room, False otherwise. """ return (int(pos.getX()), int(pos.getY())) in self.room_coordinates </code></pre> <p>As you see I implemented it using the int() method and the random generator "random.randrange".</p> <p>In the solution, the teacher has implemented this class using the math.floor() function and the random generator random.random():</p> <pre><code>class RectangularRoom(object): """ A RectangularRoom represents a rectangular region containing clean or dirty tiles. A room has a width and a height and contains (width * height) tiles. At any particular time, each of these tiles is either clean or dirty. """ def __init__(self, width, height): """ Initializes a rectangular room with the specified width and height. Initially, no tiles in the room have been cleaned. width: an integer &gt; 0 height: an integer &gt; 0 """ self.width = width self.height = height self.tiles = {} for x in range(self.width): for y in range(self.height): self.tiles[(x, y)] = False def cleanTileAtPosition(self, pos): """ Mark the tile under the position POS as cleaned. Assumes that POS represents a valid position inside this room. pos: a Position """ x = math.floor(pos.getX()) y = math.floor(pos.getY()) self.tiles[(x, y)] = True def isTileCleaned(self, m, n): """ Return True if the tile (m, n) has been cleaned. Assumes that (m, n) represents a valid tile inside the room. m: an integer n: an integer returns: True if (m, n) is cleaned, False otherwise """ return self.tiles[(m, n)] def getNumTiles(self): """ Return the total number of tiles in the room. returns: an integer """ return self.width * self.height def getNumCleanedTiles(self): """ Return the total number of clean tiles in the room. returns: an integer """ return sum(self.tiles.values()) def getRandomPosition(self): """ Return a random position inside the room. returns: a Position object. """ return Position(random.random() * self.width, random.random() * self.height) def isPositionInRoom(self, pos): """ Return True if pos is inside the room. pos: a Position object. returns: True if pos is in the room, False otherwise. """ return ((0 &lt;= pos.getX() &lt; self.width) and (0 &lt;= pos.getY() &lt; self.height)) </code></pre> <p>Surprisingly, these two chunks of code produce completely different results. I want to know why this happens. int() and floor() should have the same effect on a positive number and the two random functions seem to produce similar numbers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload