python - Python 中的 x%(1e9 + 7) 和 x%(10**9 + 7) 不同吗?如果是,为什么?

标签 python python-3.x floating-point

对于整数 x,x % (10 ** 9 + 7)x % (1e9 + 7)经过几次迭代后给出了不同的结果。为了重现这个结果,我正在分享我的解决方案 LeetCode #576. Out of Boundary Paths .如果我更改 return ans % (10 ** 9 + 7),我的解决方案不会通过 94 个测试用例中的 5 个至 return ans % (1e9 + 7) (意识到这花了我一个小时)。
请注意,此解决方案比某个天才家伙提出的单行解决方案要长得多 here .但是,如果我们更改% (10 ** 9 + 7),他的解决方案也会出现同样的问题。至 % (1e9 + 7) .
我玩了一下 python 编译器,注意到 1e9 给出了一个浮点文字。所以在我看来,这种特性是由浮点运算的“怪异”引起的。但我仍然不明白小数点后的零如何导致差异。为什么会出现这种差异?
不复制,差异可以在这里找到:https://www.diffchecker.com/PyKQCElB
要重现,这是我的解决方案:


class Solution:
    def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
        if maxMove == 0:
            return 0
        current_state = [[0 for _ in range(n)] for _ in range(m)]
        next_state = [[0 for _ in range(n)] for _ in range(m)]
        current_state[startRow][startColumn] = 1
        ans = self.count_ways(m, n, current_state)
        
        k = 1
        while k < maxMove:
            # print("CS:", current_state)
            for i in range(m):
                for j in range(n):
                    next_state[i][j] = 0
                    if i != 0:
                        next_state[i][j] += current_state[i-1][j]
                    if i!= m-1:
                         next_state[i][j] += current_state[i+1][j]
                    if j != 0:
                        next_state[i][j] += current_state[i][j-1]
                    if j != n-1:
                        next_state[i][j] += current_state[i][j+1]
            
            current_state, next_state = next_state, current_state
            ans += self.count_ways(m, n, current_state)
            
            # print("NS:", current_state)
            # print("k:{},ans:{}".format(k, int(ans % 10 ** 9 + 7)))            
            # print("k:{},ans:{}".format(k, int(ans % 1e9 + 7)))            

            k += 1
            
        # return ans % (1e9 + 7)  # This is giving incorrect results.
        return ans % (10 ** 9 + 7)  # This works fine.
        
    def count_ways(self, m, n, grid):
        ways = 0
        for i in range(m):
            for j in [0, n-1]: # Checking left and right strips of a grid.
                ways += grid[i][j]
                 
        for j in range(n):
            for i in [0, m-1]: # Checking top and bottom strips of a grid.
                ways += grid[i][j]
                
        # This will automatically add corner boxes twice.
        
        return ways

编辑:使用这个测试用例(对 findPaths 的参数,按顺序):
36
5
50
15
3

最佳答案

But I still don't understand how a zero after decimal point can cause difference.


小数点在哪里不重要。它在漂浮!

Why is this difference arising?


因为 Python 中的浮点数是通常的硬件数,这意味着它们的存储和精度有限:
>>> int(123123123123123123123.0)
123123123123123126272
#                ^^^^ different!
但是 Python 中的整数具有无限的存储空间和精度(“bignum”):
>>> int(123123123123123123123)
123123123123123123123
所以:
>>> 123123123123123123123 % 10**9
123123123

>>> 123123123123123123123 % 1e9
123126272.0
在第二种情况下,双方都转换为浮点数,因为其中之一是。

关于python - Python 中的 x%(1e9 + 7) 和 x%(10**9 + 7) 不同吗?如果是,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67438654/

相关文章:

python 3 : for loop syntax with dictionary

Python - 检测值/字符串列表是否是日期、时间、日期时间或两者都不是

Python3 + 诅咒 : How to press "q" for ending program immediately?

c++ - 禁止隐式 `unsigned` 到 `double` 转换

C- 浮点精度

c - 如何确定 float 尾数的最大正基数 10 值?

python - 在 pandas 数据框上使用 apply 时传递值的形状错误

python while循环用户输入

python - 属性错误 : 'Ui_MainWindow' object has no attribute 'setCentralWidget'

python - 无法解析网页中的不同产品链接