python输出中文加数字_python在decim后自动添加数字

这篇博客探讨了Python中浮点数表示不精确的原因,即二进制浮点数无法精确表示某些十进制分数,导致计算结果存在微小误差。文中提到,这是由于计算机内部使用IEEE-754双精度浮点数格式存储导致的,并建议通过乘以10的幂再转换为整数来减少这种误差。此外,文章还指出,对于日常计算,这种误差通常不会造成实质性影响。
摘要由CSDN通过智能技术生成

据我所知,只有-73.81632995609999是唯一一个断开的数字,而且它的降幅很小。这是一个有很好文档记录的问题,它是由于Python如何显示浮点数而产生的。从python website:On a typical machine running Python, there are 53 bits of precision available for a Python float, so the value stored internally when you enter the decimal number 0.1 is the binary fraction

0.00011001100110011001100110011001100110011001100110011010 which is close to, but not exactly equal to, 1/10.

It’s easy to forget that the stored value is an approximation to the original decimal fraction, because of the way that floats are displayed at the interpreter prompt. Python only prints a decimal approximation to the true decimal value of the binary approximation stored by the machine. If Python were to print the true decimal value of the binary approximation stored for 0.1, it would have to display

>>> 0.1

0.1000000000000000055511151231257827021181583404541015625

更具体地说,关于小表示错误:Representation error refers to the fact that some (most, actually) decimal fractions cannot be represented exactly as binary (base 2) fractions. This is the chief reason why Python (or Perl, C, C++, Java, Fortran, and many others) often won’t display the exact decimal number you expect:

>>> 0.1 + 0.2

0.30000000000000004

Why is that? 1/10 and 2/10 are not exactly representable as a binary fraction. Almost all machines today (July 2010) use IEEE-754 floating point arithmetic, and almost all platforms map Python floats to IEEE-754 “double precision”. 754 doubles contain 53 bits of precision, so on input the computer strives to convert 0.1 to the closest fraction it can of the form J/2**N where J is an integer containing exactly 53 bits.

简而言之,这种修改是因为Python如何存储数字。你可以试着乘以10^n并将这些值存储为整数,然后在需要计算时进行除法。如果您在进行简单的计算,python所产生的微小差异不会对这些计算产生实质性的影响。希望这有帮助。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值