如何在Python中使用精确的浮点运算? [英] How do I use accurate float arithmetic in Python?

查看:154
本文介绍了如何在Python中使用精确的浮点运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中使用浮点运算来解决这个问题。
我正在求解的等式如下:

$ $ $ $ $ $ $ $ $ $($) -m))** b + a *((xm)* 110.0 *(1-m))** c
a是一个非常大的正数(十万)
b是1.0000002
c是0.9999998

当我在Excel中执行此操作时,获得了准确的结果,但是当我在python我得到完全不准确的结果。

每个独立部分的结果都是一样的,直到我乘以-a和a。
So((xm)<110.0 (1-m))** b和((xm)<110.0 与他们的excel计算值相同,但是当他们乘以大数时,他们完全改变。



我该怎么做?我必须使用不同的语言吗?这个问题只是在python中,或者它是所有语言?



编辑:它是在Excel中完全相同的公式在Python中。一模一样。数字也是一样的,直到我乘以a和-a。那么他们都是5点的东西。 Excel中的答案在x = 0.5和m = 0.265时为0.47左右,而python的答案为-0.67左右。很难理解你在找什么精度,但是我认为你也可以看一下十进制模块 -



float数据类型:

Decimal是基于一个浮点
模型,该模型是以
为基础设计的,必须有
最重要的指导原则 -
电脑必须提供一个算术
,其工作方式与人们在
学校学习的
算术相同。 - 摘自小数
算术规范。



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

正确性转化为
算术。在十进制浮点中,
0.1 + 0.1 + 0.1 - 0.3正好等于零。在二进制浮点中,
的结果是5.5511151231257827e-017。
尽管接近零,差异
阻止了可靠的平等测试,并且
差异可以累积。对于这个
的原因,在具有
严格等式不变式的
会计应用程序中,decimal是首选。

十进制模块包含
的重要位置的概念,所以
1.30 + 1.20是2.50。尾随零保持显示重要性。这

货币应用的惯例。对于
乘法,教科书
方法使用
被乘数中的所有数字。例如,1.3 * 1.2
为1.56,而1.30 * 1.20则为1.5600


I'm having this issue in python with float arithmetic. The equation I'm solving goes as follows:

-a *((x-m)*110.0*(1-m))**b +a*((x-m)*110.0*(1-m))**c
a is a really large positive number (in the hundred thousands)
b is 1.0000002
c is 0.9999998

When I do this in excel I get accurate results but when I do it in python I get completely inaccurate results.

The results are exactly the same for each separate part until I multiply by -a and a. So ((x-m)110.0(1-m))**b and ((x-m)110.0(1-m))**c are exactly the same as their excel calculated values but when they're multiplied by the large number they completely change.

How do I do this? do I have to use a different language? Is this problem only in python or is it in all languages?

Edit: It is the exact same equation in excel as in python. Exactly the same. The numbers are also the same until I multiply by a and -a. then they are both off by 5 point something. The answer in excel is around 0.47 for x=0.5 and m = 0.265 while the answer for python is around -0.67

解决方案

It is quite hard to understand what precision you are looking for but i think that you can also take a look at the decimal module. -

It offers several advantages over the float datatype:

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 an 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.

The exactness carries over into arithmetic. In decimal floating point, 0.1 + 0.1 + 0.1 - 0.3 is exactly equal to zero. In binary floating point, the result is 5.5511151231257827e-017. While near to zero, the differences prevent reliable equality testing and differences can accumulate. For this reason, decimal is preferred in accounting applications which have strict equality invariants.

The decimal module incorporates a notion of significant places so that 1.30 + 1.20 is 2.50. The trailing zero is kept to indicate significance. This is the customary presentation for monetary applications. For multiplication, the "schoolbook" approach uses all the figures in the multiplicands. For instance, 1.3 * 1.2 gives 1.56 while 1.30 * 1.20 gives 1.5600.

这篇关于如何在Python中使用精确的浮点运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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