久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

浮點(diǎn)數(shù)和小數(shù)點(diǎn)的小數(shù)位問題.十進(jìn)制

Decimal place issues with floats and decimal.Decimal(浮點(diǎn)數(shù)和小數(shù)點(diǎn)的小數(shù)位問題.十進(jìn)制)
本文介紹了浮點(diǎn)數(shù)和小數(shù)點(diǎn)的小數(shù)位問題.十進(jìn)制的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我似乎在使用浮點(diǎn)數(shù)時失去了很多精度.

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

這是我用來從文本文件中導(dǎo)入矩陣的代碼:

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]))

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

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

這是我用來重新排列方程式的代碼.b 是系數(shù)矩陣,y 是答案向量:

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

但是我得到的答案并不精確到小數(shù)位.

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

例如,從上面重新排列第二個等式后,我應(yīng)該得到:

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

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

我得到的是:

y=-1.2-0.20000000000000001x+0.59999999999999998z

這似乎不是一個大問題,但是當(dāng)您將數(shù)字提高到非常高的冪時,錯誤會非常大.有沒有解決的辦法?我嘗試了 Decimal 類,但它不適用于冪(即 Decimal(x)**2).

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

有什么想法嗎?

推薦答案

我對 Decimal 類不夠熟悉,無法幫助您解決問題,但您的問題是由于小數(shù)通常無法準(zhǔn)確表示二進(jìn)制,所以你看到的是最接近的近似值;如果不使用特殊類(可能是 Decimal),就無法避免這個問題.

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

那么小數(shù)類不能正常工作怎么辦?只要我從字符串而不是浮點(diǎn)數(shù)開始,power 似乎就可以正常工作.

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

模塊文檔解釋了 decimal.Decimal 的必要性和用法很清楚,如果你還沒有,你應(yīng)該檢查一下.

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

這篇關(guān)于浮點(diǎn)數(shù)和小數(shù)點(diǎn)的小數(shù)位問題.十進(jìn)制的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 黄色香蕉视频在线观看 | 午夜a级理论片915影院 | 久久视频一区 | 久久久国产一区二区三区四区小说 | 国产精品国产成人国产三级 | 午夜电影福利 | 国产美女永久免费无遮挡 | 美女黄网 | 久久r精品 | 91天堂网| 羞羞在线视频 | 亚州av | 久久国产精品首页 | 亚洲日韩中文字幕一区 | 涩涩视频在线观看 | 日韩在线欧美 | 亚洲免费一区二区 | 成人精品一区二区三区 | 一区二区三区免费 | 欧美炮房 | 国产在线一区二区 | 欧美一区二区三区在线看 | 大香网伊人 | 国产精品亚洲视频 | 精品久久久久久久久久久下田 | 亚洲二区在线 | 国产成人在线视频免费观看 | 国产精品91网站 | 成人免费黄色 | 色婷婷亚洲一区二区三区 | 在线日韩 | 91久久精品一区 | 国产免费一区二区 | 色综合久久天天综合网 | 国产日韩一区二区 | 欧美亚州| 国产乱码精品一区二区三区av | 久草免费视 | 日本a v在线播放 | 久久亚洲一区 | 久久免费福利 |