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

深的

对于整数x,x % (10 ** 9 + 7)并且x % (1e9 + 7)都给人一种几次迭代后不同的结果。为了重现这个结果,我分享了我的LeetCode #576解决方案越界路径如果我更改return ans % (10 ** 9 + 7)return ans % (1e9 + 7)(意识到这花了我一个小时),我的解决方案将不会通过 94 个测试用例中的 5 个

请注意,此解决方案比此处某个天才家伙提出的单行解决方案要长得多然而,同样的问题,他的解决方案出现,如果我们改变% (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
橡子

但我仍然不明白小数点后的零如何导致差异。

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

为什么会出现这种差异?

因为 Python 中的浮点数是通常的硬件数,这意味着它们的存储和精度有限:

>>> int(123123123123123123123.0)
123123123123123126272
#                ^^^^ different!

但是 Python 中的整数具有无限的存储空间和精度(“bignum”):

>>> int(123123123123123123123)
123123123123123123123

所以:

>>> 123123123123123123123 % 10**9
123123123

>>> 123123123123123123123 % 1e9
123126272.0

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

RedHat Enterprise Linux 6.6、6.7和7.x中的Python和Berkeley DB版本

为什么 x = 7 < 5 ; print(x) 在 python 中给出错误?

array_key_exists 和 ?? 在 php 7.x 中

Python,文件(1)-为什么数字[7,8,9,10,12,13,27]和范围(0x20,0x100)用于确定文本与二进制文件

卡扣中的unity7和x11有什么区别

“&0x7fffffff”在“ int(time.time()* 1000.0)和0x7FFFFFFF”中是什么意思

什么意思&0x40和<< 7

NodeJs Buffer.toString()在7.x和8.x版本中获得不同的结果(模块加密)

为什么在CentOS 6和CentOS 7之间的/ proc / cpuinfo中得到不同的cpu标志?

为什么type(x)和print(type(x)在python中显示略有不同的结果?

如何在Windows 7中运行python 2和3?

如何在Windows 7中运行python 2和3?

Tomcat 7x为什么不要求在web.xml中配置WSServletContextListener和WSServlet来进行Jax-ws服务部署?

为什么在第7和8行中强制转换有效,但在第9行中却无效?

为什么Redhat在centos 7中不包含python 3?

如何在Mac OS X和Debian上使用SysLogHandler或syslog从Python登录到syslog(7)

pip如何在Windows 7 x64和python 2.7上安装pylzma

如何将文本字段和标签放在7x3的网格中?

在OS X中的Java 7和8之间切换

netbeans 7.x中的源代码字和行计数?

如何在Sitecore 7.x和Solr 4.7中使用Glass Mapper 3.3

为什么s7 =“ hello”,'world'; Python中的print(s7)发出('hello','world')?

PhpStorm 7/8/9中的项目和结构导航拆分

Xcode 7和IOS 9中SpriteKit的问题

“df['质量']] = [1 if x>=7 else 0 for x in df['quality']]”和下面给出的扩展内衬有什么区别?

getDeclaredMethods()在Java 7和Java 8中的行为不同

android 7 中图标和主要活动的不同标签

当参数0x7和0x18时此程序的输出是什么

要从系列中打印答案-> x ^ 3-x ^ 5 + x ^ 7-x ^ 9 +