"math.floor(x)" 和 "int(x)" 在 Python 中对正实数产生不同的结果吗?



我在Python shell上检查了几个例子,它们似乎吐出了相同的数字。但是在一个需要近似大量数字的程序中,它们显然会产生不同的结果。

我正在试着写一个小程序来模拟一个物体在矩形平面上的运动。为此,我必须编写一个名为"RectangularRoom"的类,它接受宽度和高度,并创建一个网格:

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 > 0
    height: an integer > 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

正如你所看到的,我使用int()方法和随机生成器"random.randrange"来实现它。

在解决方案中,老师使用math.floor()函数和随机生成器random.random()实现了这个类:

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 > 0
    height: an integer > 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 <= pos.getX() < self.width)
            and (0 <= pos.getY() < self.height))

令人惊讶的是,这两段代码产生完全不同的结果。我想知道为什么会这样。Int()和floor()对于正数应该具有相同的效果,并且这两个随机函数似乎产生相似的数字。

好吧,我不知道你的问题的答案-我怀疑他们返回完全相同的东西。然而,我认为你的解决方案有一个问题,可能会解释输出:

在"cleanTileAtPosition"

self.cleaned.append((int(pos.getX()), int(pos.getY())))

In 'getNumCleanedTiles'

return len(self.cleaned)

这段代码似乎允许一个tile被清理多次。这不是你的老师代码所做的,因为瓷砖只能设置为"True"一次。

(顺便说一下,因为随机。Randrange返回一个整数。整数转换不应该做任何事情!)

编辑:另外-我认为类型差异值得考虑。但是你所有的类型都是int型,所以这应该不是问题。

你们的方法有一个很大的不同。random.randrange返回一个整数,在位置上调用int()。你在处理整数。同时,random.random() * something返回一个浮点数,math.floor也返回一个浮点数。你的老师一直在和花车打交道。

这至少是intmath.floor之间的差异,但我不知道为什么这会产生完全不同的结果。你能说得更具体一点吗?

考虑以下示例:

>>> a = 1.2
>>> b = int(a)
>>> c = math.floor(a)
>>> b
1
>>> c
1.0
>>> type(c)
<type 'float'>
>>> type(a)
<type 'float'>
>>> type(b)
<type 'int'>

尽管int()和floor()对正整数做"同样的事情",但结果是不同的类型。这可能会导致各种不同的交互,这取决于您稍后对它们进行的操作。Python将从它的角度找出如何使它"工作",尽管它不一定会给你想要的结果。

一般来说,当你在编程中做任何数学运算时,你都希望你使用的类型保持一致,这样你就不必担心类型转换和提升以及所有那些讨厌的东西。

用"整数"代替数学。尽管如此,Python在浮点数<->整数比较中有足够的黑魔法,因此这个差异在上面的代码中应该没有问题。然而,这两个版本的代码都带有明显的Java或c++"重音",这使得它比简单的Python代码要复杂得多。差异很可能出现在代码的其他点上,而不是在这次四舍五入上。例如-尝试使用random.randint而不是获得浮点数,然后四舍五入,以。

开始

最新更新