用浮点数字符串格式化的Python精度 [英] Python precision in string formatting with float numbers

查看:446
本文介绍了用浮点数字符串格式化的Python精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么通过格式化一个包含float值的字符串,这个最后一个的精度不被尊重。即:

 '%f'%38.2551994324 


$ b

返回:

  '38 .255199'

(4个符号丢失!)

目前我解决了指定:

 '%。10f'%38.2551994324 

返回'38 .2551994324'如预期的那样...但我应该真的强制手动多少十进制数字我想要的?有没有办法简单地告诉python保持所有这些?! (我应该怎么做,例如,如果我不知道我的数字有多少小数?)

解决方案

但我真的应该手动强制手动输入多少个十进制数字吗?是的。

即使指定了10个十进制数字,你仍然不能打印所有的数字。无论如何,浮点数都不具备这种精度,它们大多是十进制数的近似值(它们实际上是二进制分数的总和)。试试这个:

 >>>格式(38.2551994324, .32f ')
'38 .25519943239999776096738060005009'



格式化一个浮点数(与'%f '%number '{:f}'。format(number) format(number,'f') ),会显示一个默认的小数位数。这与使用 str()(或'%s'%number 格式(数字)格式(数字),基本上使用 str() code>),只有默认包含的小数位数不同;在使用 str()时,3.2之前的Python版本使用12位数字作为整数



如果您希望您的理性数字计算能够使用特定的<精确的个数字,则不要使用浮点数。使用 decimal.Decimal 类型



  • 十进制是基于一个浮点模型,必须有一个最重要的指导原则 - 计算机必须提供一个算术运算,就像人们在学校学习的算术一样。 - 摘自十进制算术规范。


  • 十进制数可以精确表示。相比之下,像 1.1 2.2 这样的数字在二进制浮点中没有精确的表示。最终用户通常不会像使用二进制浮点一样将 1.1 + 2.2 显示为 3.3000000000000003




I don't understand why, by formatting a string containing a float value, the precision of this last one is not respected. Ie:

'%f' % 38.2551994324

returns:

'38.255199'

(4 signs lost!)

At the moment I solved specifying:

'%.10f' % 38.2551994324

which returns '38.2551994324' as expected… but should I really force manually how many decimal numbers I want? Is there a way to simply tell to python to keep all of them?! (what should I do for example if I don't know how many decimals my number has?)

解决方案

but should I really force manually how many decimal numbers I want? Yes.

And even with specifying 10 decimal digits, you are still not printing all of them. Floating point numbers don't have that kind of precision anyway, they are mostly approximations of decimal numbers (they are really binary fractions added up). Try this:

>>> format(38.2551994324, '.32f')
'38.25519943239999776096738060005009'

there are many more decimals there that you didn't even specify.

When formatting a floating point number (be it with '%f' % number, '{:f}'.format(number) or format(number, 'f')), a default number of decimal places is displayed. This is no different from when using str() (or '%s' % number, '{}'.format(number) or format(number), which essentially use str() under the hood), only the number of decimals included by default differs; Python versions prior to 3.2 use 12 digits for the whole number when using str().

If you expect your rational number calculations to work with a specific, precise number of digits, then don't use floating point numbers. Use the decimal.Decimal type instead:

  • Decimal "is based on a floating-point model which was designed with people in mind, and necessarily has a paramount guiding principle – computers must provide an arithmetic that works in the same way as the arithmetic that people learn at school." – excerpt from the decimal arithmetic specification.

  • Decimal numbers can be represented exactly. In contrast, numbers like 1.1 and 2.2 do not have exact representations in binary floating point. End users typically would not expect 1.1 + 2.2 to display as 3.3000000000000003 as it does with binary floating point.

这篇关于用浮点数字符串格式化的Python精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆