是的”数学地板(x) “和”int(x)“在Python中对正实数产生不同的结果?

2024-04-26 23:35:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我检查了pythonshell上的几个示例,它们似乎给出了相同的数字。但在一个程序中,如果一组大的数应该是近似的,它们显然会产生不同的结果。在

我想写一个小程序,模拟物体在矩形平面上的运动。为此,我必须编写一个名为“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()方法和随机生成器实现了它。”随机随机范围". 在

在解决方案中,教师使用数学地板()函数与随机发生器随机。随机():

^{pr2}$

令人惊讶的是,这两段代码产生了完全不同的结果。我想知道为什么会这样。int()和floor()对正数的影响应该是相同的,两个随机函数似乎产生了相似的数字。在


Tags: theinposselfanreturndefinteger
3条回答

考虑以下示例:

>>> 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将从它的角度找出如何使它“工作”,尽管它不一定会给您想要的结果。在

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

你们的方法有一个很大的不同。random.randrange返回一个整数,并对位置调用int()。你在处理整数。 同时,random.random() * something返回一个float,math.floor也返回一个float。你的老师一直在用彩车。在

这至少是int和{}之间的区别,但我不知道为什么这会产生完全不同的结果。你能再具体一点吗?在

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

在“CleanTileAttosition”中

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

在“GetNumCleanedFiles”中

^{pr2}$

此代码似乎允许一个磁贴被多次清理。这不是您的教师代码所做的,因为互动程序只能设置为“True”一次。在

(顺便说一下,自从随机随机范围返回一个整数。。整数转换不应执行任何操作!)在

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

相关问题 更多 >