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

Python中不準(zhǔn)確的對(duì)數(shù)

Inaccurate Logarithm in Python(Python中不準(zhǔn)確的對(duì)數(shù))
本文介紹了Python中不準(zhǔn)確的對(duì)數(shù)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我每天都在公司使用 Python 2.4.我使用了標(biāo)準(zhǔn)數(shù)學(xué)庫(kù)中的通用對(duì)數(shù)函數(shù)log",當(dāng)我輸入 log(2**31, 2) 時(shí),它返回 31.000000000000004,這讓我覺得有點(diǎn)奇怪.

I work daily with Python 2.4 at my company. I used the versatile logarithm function 'log' from the standard math library, and when I entered log(2**31, 2) it returned 31.000000000000004, which struck me as a bit odd.

我對(duì) 2 的其他冪也做了同樣的事情,而且效果很好.我跑了 'log10(2**31)/log10(2)' 得到了 31.0 輪

I did the same thing with other powers of 2, and it worked perfectly. I ran 'log10(2**31) / log10(2)' and I got a round 31.0

我嘗試在 Python 3.0.1 中運(yùn)行相同的原始函數(shù),假設(shè)它已在更高級(jí)的版本中得到修復(fù).

I tried running the same original function in Python 3.0.1, assuming that it was fixed in a more advanced version.

為什么會(huì)這樣?Python中的數(shù)學(xué)函數(shù)是否可能存在一些不準(zhǔn)確之處?

Why does this happen? Is it possible that there are some inaccuracies in mathematical functions in Python?

推薦答案

這對(duì)于計(jì)算機(jī)算術(shù)來說是意料之中的.它遵循特定規(guī)則,例如 IEEE 754,可能與您所學(xué)的數(shù)學(xué)不符在學(xué)校.

This is to be expected with computer arithmetic. It is following particular rules, such as IEEE 754, that probably don't match the math you learned in school.

如果這確實(shí)很重要,請(qǐng)使用 Python 的 十進(jìn)制類型.

If this actually matters, use Python's decimal type.

例子:

from decimal import Decimal, Context
ctx = Context(prec=20)
two = Decimal(2)
ctx.divide(ctx.power(two, Decimal(31)).ln(ctx), two.ln(ctx))

這篇關(guān)于Python中不準(zhǔn)確的對(duì)數(shù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(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 有哪些可能的計(jì)算可以返回 NaN?)
Python float to ratio(Python浮動(dòng)比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 伊人2222| 亚洲精品乱码久久久久久 | 色香蕉网 | 国产乱淫片视频 | 亚洲最大黄色网址 | 亚洲在线视频 | 美女黄色在线观看 | 天海翼在线视频 | 国产免费a | 欧美成人小视频 | 免费福利在线观看 | 国产成人午夜高潮毛片 | 97在线免费观看视频 | 天天爽夜夜操 | 一区二区在线看 | 性欧美69 | 欧美777| 欧美一区二区在线视频 | 欧美日韩精品一区 | 国产精品久久久一区二区 | 中文字幕理伦片免费看 | 中文字幕一区在线 | 久久xxx | 中文字幕+乱码+中文乱码91 | 国产综合一区二区 | 婷婷中文字幕 | 国产黄色免费视频 | 91精品久久久久久粉嫩 | 免费在线观看黄 | 岛国av在线免费观看 | 国产精品一区av | 一级黄色在线观看 | 欧美视频在线一区 | 亚洲一区二区在线视频 | 欧美精品在线免费观看 | 亚洲特级片 | 一区二区三区不卡视频 | 欧美一级片网站 | 精品国产91| 日韩一区二区免费视频 | av黄色在线观看 |