____tz_zs

1、OverflowError: integer division result too large for a float

在 Python 3 中,int/int 将返回一个 float。而 Python3 中的 int 类型是长整型,可以在计算中处理任意大的整数,而 float 类型有范围,所以下方例子中,极大整数的除法计算造成了溢出错误。

big_int = 10 ** 400 + 23
print(big_int / 2)
# OverflowError: integer division result too large for a float

2、python3 中的 int 类型是长整型,理论可以是任意大的整数。

sys.getsizeof()方法可以看 int 占了几位。如下,使用 400 位数字的输出情况

import sys# long_int_str = "1111111111111111111111111111111111"
long_int_str = "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"print(f"字符串长度:{len(long_int_str)}")
print(f"字符串内存使用:{sys.getsizeof(long_int_str)}")
print(f"int内存使用:{sys.getsizeof(int(long_int_str))}")
"""
字符串长度:34
字符串内存使用:83
int内存使用:40字符串长度:400
字符串内存使用:449
int内存使用:204
"""

3、最大的浮点数 sys.float_info.max

在 Python 3 中,number / 10将尝试返回一个float. 但是,浮点值在 Python 中不能有任意大的大小,如果number很大,OverflowError将会引发。

import sysprint(sys.float_info)
# sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)print(sys.float_info.max)  # 1.7976931348623157e+308

4、其他延伸 sys.maxsize

import sysprint(sys.maxsize)  # 9223372036854775807

《The Python Standard Library》
An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 231 - 1 on a 32-bit platform and 263 - 1 on a 64-bit platform.

In Python, what is sys.maxsize?
Python3 可以在计算中处理任意大的整数。任何大到不能用 64 位容纳(或任何底层硬件限制)的整数都在软件中处理。因此,Python 3 没有 sys.maxint 常量。

5、用//将两个整数的相除,获取整数

两个整数的整数除法不会将两个参数都转换为浮点数然后相除,避开了 float 。

big_int = 10 ** 400 + 23
# print(big_int / 2)
# OverflowError: integer division result too large for a float
print(big_int // 2)
# 5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011

具体见另一篇 https://tzzsmaster.blog.csdn.net/article/details/121020236

参考

Why are the results of integer division and converting to an int after division different for large numbers?
How to manage division of huge numbers in Python?

python OverflowError: integer division result too large for a float相关推荐

  1. RuntimeError: Integer division of tensors using div or / is no longer supported, and in a future rel

    RuntimeError: Integer division of tensors using div or / is no longer supported, and in a future rel ...

  2. ZeroDivisionError: integer division or modulo by zero解决记录

    运行代码时,出现ZeroDivisionError: integer division or modulo by zero问题,具体如下: Traceback (most recent call la ...

  3. pandas下boxplot报ZeroDivisionError: integer division or modulo by zero

    最近在pandas下画一个箱图boxplot,但是运行起来总是报: ZeroDivisionError: integer division or modulo by zero错误,百思不得其解. 后来 ...

  4. Python:实现integer partition整数分区算法(附完整源码)

    Python:实现integer partition整数分区算法 def partition(m: int) -> int:memo: list[list[int]] = [[

  5. java: integer number is too large

    今天想定义一个类常量,结果如下面那样定义,确报错了. error is: Integer number too large public static final Long STARTTIME = 1 ...

  6. python an integer is required_Python TypeError: an integer is required (got type tuple)

    Python TypeError: an integer is required (got type tuple) 关注:256  答案:2  mip版 解决时间 2021-01-12 12:36 提 ...

  7. ZeroDivisionError: integer division or modulo by zero

    这里的错误就是由于数据集太小. # 2. Split into train / validation partitionsn_val = int(len(dataset) * val_percent) ...

  8. 解决ZeroDivisionError: integer division or modulo by zero问题

    在刷题判断素数时候,出现了这个问题. 以下是部分代码: 报错: 错误原因: 上述代码的 for 循环取值从 0 到 int(math.sqrt) ,在下一行 if 语句 m%i 时就出了问题, 因为分 ...

  9. ZeroDivisionError:Integer division or modulo by zero

    docker环境下,多GPU训练 方式:采用nvidia-docker创建容器 另: 在用sudo无法解决sh文件的pemission denied问题时,采用bash替代sudo

最新文章

  1. View及ViewGroup的事件分发及传递(一)
  2. 综合一2017.5.17
  3. mysql 语句怎样修饰约束_MySQL中的约束,添加约束,删除约束,以及一些其他修饰讲解...
  4. 详解操作系统中虚拟内存与物理内存的关系
  5. EnvironmentError: [WinError 5] 拒绝访问
  6. shell 脚本空行造成 :not found make.sh:
  7. Freemarker输出$和html标签等特殊符号
  8. Python实现C代码统计工具(一)
  9. 转 Still,yet和already的用法
  10. 给Hexo主题博客加入百度站点统计
  11. mysql jion on 三表_MySQL 三表连接(join)
  12. QT概念详解及开发入门简介
  13. 数字 IC 笔试面试必考点(8)时钟偏差以及时钟抖动
  14. 记一次feign调用报错:feign.codec.DecodeException: Error while extracting response for type [java...
  15. Wpf关于列表动态刷新的问题
  16. ZZUNOJ 1008 美元和人民币
  17. Android9.0以上系统安装Edxposed
  18. 董老师又双叒叕送书啦,20本《Python数据分析、挖掘与可视化》
  19. Flutter学习指南:UI布局和控件,作为Android开发者
  20. 【总结】斜杠/和反斜杠\ 的区别

热门文章

  1. 科技产业的时尚产品——苹果公司
  2. macOS spacebar安装笔记
  3. 该死!B 站上这些 Python 视频真香!
  4. spring cloud 文件服务器,SpringBoot集成MinIO文件服务器
  5. 【安利】看 Jabref 如何帮你高效管理文献
  6. linux 开发设计软件,在Linux平台下开发和设计聊天软件
  7. 计算机应用基础有没有笔试,计算机应用基础笔试试题及答案
  8. 电商设计和平面设计哪个好,他们之间的区别是什么?
  9. 佛山“禅城区智能卡云”入选国家示范项目
  10. 清华大学计算机学院科研经费,太烧钱!985工程大学年度科研经费统计,看了数据吓一跳!...