十进制的地方问题与浮动和decimal.Decimal [英] Decimal place issues with floats and decimal.Decimal

查看:196
本文介绍了十进制的地方问题与浮动和decimal.Decimal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,我需要解决一个矩阵:

  4.0x -2.0y 1.0z = 11.0 
1.0x + 5.0y -3.0z = -6.0
2.0x + 2.0y + 5.0z = 7.0

这是我用来从文本文件导入矩阵的代码:

  f = open('gauss.dat')
lines = f.readlines()
f.close()

j = 0
对于行内行:
位= string.split(行,',')
s = [] $ b $ len(bits)):
if(i!= len(bits)-1):
s.append(float(bits [i]))
#print s [i]
b.append(s)
y.append(float(bits [len(bits)-1]))

我需要使用gauss-seidel求解,所以我需要重新排列x,y和z的方程:

 ($ 6 $ x $)=(7-2x-2y)/ 7 

下面是我用来重新排列方程的代码。 b 是一个系数矩阵, y 是答案矢量:

  def equation(b,y):
i = 0
eqn = []
row = []
while(i< ; len(b)):
j = 0
row = []
while(j if(i == j):
row.append(y [i] / b [i] [i])
else:
row.append(-b [i] [j] / b [i] [i])
j = j + 1
eqn.append(row)
i = i + 1
return eqn

然而,我得到的答案并不精确到小数点。

例如,重新排列上面的第二个等式,我应该得到:

  y = -1.2 -.2x + .6z 

我得到的是:
$ b $ pre $ y = -1.2 -0.20000000000000001x + 0.59999999999999998z

这似乎不是一个大问题,但是当您将号码提高到非常高的功率他的错误是相当大的。有没有解决的办法?我尝试了 Decimal 类,但它不能很好的与幂(即, Decimal(x)** 2 ) 。



有什么想法? 我不够熟悉用十进制类来帮助你,但是你的问题是由于小数部分通常不能用二进制精确表示的事实,所以你看到的是最接近的可能的近似值;没有办法避免这个问题,而不使用一个特殊的类(如十进制,可能)。



编辑:什么关于小数班对你来说工作不正常?只要我以一个字符串开头,而不是一个浮动,权力似乎工作正常。

 >>>进口十进制
>>> print(decimal.Decimal(1.2)** 2)
1.44

一个href =http://docs.python.org/library/decimal.html =noreferrer>模块文档解释了对 decimal.Decimal code>很清楚,如果你还没有,应该检查一下。


I seem to be losing a lot of precision with floats.

For example I need to solve a matrix:

4.0x -2.0y 1.0z =11.0
1.0x +5.0y -3.0z =-6.0
2.0x +2.0y +5.0z =7.0

This is the code I use to import the matrix from a text file:

f = open('gauss.dat')
lines =  f.readlines()
f.close()

j=0
for line in lines:
    bits = string.split(line, ',')
    s=[]
    for i in range(len(bits)):
        if (i!= len(bits)-1):
            s.append(float(bits[i]))
            #print s[i]
    b.append(s)
    y.append(float(bits[len(bits)-1]))

I need to solve using gauss-seidel so I need to rearrange the equations for x, y, and z:

x=(11+2y-1z)/4
y=(-6-x+3z)/5
z=(7-2x-2y)/7

Here is the code I use to rearrange the equations. b is a matrix of coefficients and y is the answer vector:

def equations(b,y):
    i=0
    eqn=[]
    row=[]
    while(i<len(b)):
        j=0
        row=[]
        while(j<len(b)):
            if(i==j):
                row.append(y[i]/b[i][i])
            else:
                row.append(-b[i][j]/b[i][i])
            j=j+1
        eqn.append(row)
        i=i+1
    return eqn

However the answers I get back aren't precise to the decimal place.

For example, upon rearranging the second equation from above, I should get:

y=-1.2-.2x+.6z

What I get is:

y=-1.2-0.20000000000000001x+0.59999999999999998z

This might not seem like a big issue but when you raise the number to a very high power the error is quite large. Is there a way around this? I tried the Decimal class but it does not work well with powers (i.e, Decimal(x)**2).

Any ideas?

解决方案

I'm not familiar enough with the Decimal class to help you out, but your problem is due to the fact that decimal fractions can often not be accurate represented in binary, so what you're seeing is the closest possible approximation; there's no way to avoid this problem without using a special class (like Decimal, probably).

EDIT: What about the decimal class isn't working properly for you? As long as I start with a string, rather than a float, powers seem to work fine.

>>> import decimal
>>> print(decimal.Decimal("1.2") ** 2)
1.44

The module documentation explains the need for and usage of decimal.Decimal pretty clearly, you should check it out if you haven't yet.

这篇关于十进制的地方问题与浮动和decimal.Decimal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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