十进制模块中的重要数字 [英] Significant figures in the decimal module

查看:177
本文介绍了十进制模块中的重要数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我决定尝试通过编写一些python脚本来解决我的物理作业。我遇到的一个问题是,有意义的数字似乎并不总能正确显示出来。例如,这样可以正确处理重要的数字:

  from decimal import Decimal 
>>>十进制('1.0')+十进制('2.0')
十进制(3.0)



<但是这并不是:

 >>>十进制('1.00')/十进制('3.00')
十进制(0.3333333333333333333333333333)



<所以有两个问题:


  1. 我是对的,这不是预期的有效位数,或者我需要刷在有意义的数字数学?

  2. 有没有办法做到这一点,而不必手动设置小数精度?当然,我敢肯定,我可以使用numpy来做到这一点,但我只是想知道是否有办法用好奇的十进制模块做到这一点。

$ b

解决方案

更改小数工作精度为2位是不是是一个好主意,除非你绝对只会执行一个单一的操作。

您应该始终以比重要性更高的精度进行计算,并且只能围绕最终结果进行计算。如果执行一个长序列的计算,并在每个步骤中将有效数字取整,则错误将会累积。十进制模块不知道任何特定的操作是一个长序列还是最终结果,所以它假设它不应该超过必要的范围。理想情况下,它会使用无限精度,但是这太昂贵了,所以Python开发人员解决了28位数字。



一旦你到达最终结果,你可能想要是量化:
$ b $ pre
>>>(十进制('1.00')/十进制('3.00'))。量化(十进制(0.001 ))
十进制(0.333)

您必须手动跟踪重要性。如果你想自动显示跟踪,你应该使用区间算术。有一些Python可用的库,包括 pyinterval mpmath (它支持任意精度)。使用小数库实现区间运算也很简单,因为它支持定向舍入。



您也可以阅读十进制算术常见问题解答:十进制算术重要性算术?


So I've decided to try to solve my physics homework by writing some python scripts to solve problems for me. One problem that I'm running into is that significant figures don't always seem to come out properly. For example this handles significant figures properly:

from decimal import Decimal
>>> Decimal('1.0') + Decimal('2.0')
Decimal("3.0")

But this doesn't:

>>> Decimal('1.00') / Decimal('3.00')
Decimal("0.3333333333333333333333333333")

So two questions:

  1. Am I right that this isn't the expected amount of significant digits, or do I need to brush up on significant digit math?
  2. Is there any way to do this without having to set the decimal precision manually? Granted, I'm sure I can use numpy to do this, but I just want to know if there's a way to do this with the decimal module out of curiosity.

解决方案

Changing the decimal working precision to 2 digits is not a good idea, unless you absolutely only are going to perform a single operation.

You should always perform calculations at higher precision than the level of significance, and only round the final result. If you perform a long sequence of calculations and round to the number of significant digits at each step, errors will accumulate. The decimal module doesn't know whether any particular operation is one in a long sequence, or the final result, so it assumes that it shouldn't round more than necessary. Ideally it would use infinite precision, but that is too expensive so the Python developers settled for 28 digits.

Once you've arrived at the final result, what you probably want is quantize:

>>> (Decimal('1.00') / Decimal('3.00')).quantize(Decimal("0.001"))
Decimal("0.333")

You have to keep track of significance manually. If you want automatic significance tracking, you should use interval arithmetic. There are some libraries available for Python, including pyinterval and mpmath (which supports arbitrary precision). It is also straightforward to implement interval arithmetic with the decimal library, since it supports directed rounding.

You may also want to read the Decimal Arithmetic FAQ: Is the decimal arithmetic ‘significance’ arithmetic?

这篇关于十进制模块中的重要数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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