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

浮點(diǎn)任意精度是否可用?

Is floating point arbitrary precision available?(浮點(diǎn)任意精度是否可用?)
本文介紹了浮點(diǎn)任意精度是否可用?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

只是為了好玩,因?yàn)樗娴暮芎唵危覍懥艘粋€小程序來生成 嫁接號碼,但由于浮點(diǎn)精度問題,它沒有找到一些更大的示例.

Just for fun and because it was really easy, I've written a short program to generate Grafting numbers, but because of floating point precision issues it's not finding some of the larger examples.

def isGrafting(a):
  for i in xrange(1, int(ceil(log10(a))) + 2):
    if a == floor((sqrt(a) * 10**(i-1)) % 10**int(ceil(log10(a)))):
      return 1

a = 0
while(1):
  if (isGrafting(a)):
    print "%d %.15f" % (a, sqrt(a))
  a += 1

此代碼至少缺少一個已知的嫁接編號.<代碼>9999999998 =>99999.99998999999999949999999994999999999374999999912... 乘以 10**5 后似乎會降低額外的精度.

This code misses at least one known Grafting number. 9999999998 => 99999.99998999999999949999999994999999999374999999912... It seems to drop extra precision after multiplying by 10**5.

>>> a = 9999999998
>>> sqrt(a)
99999.99999
>>> a == floor((sqrt(a) * 10**(5)) % 10**int(ceil(log10(a))))
False
>>> floor((sqrt(a) * 10**(5)) % 10**int(ceil(log10(a))))
9999999999.0
>>> print "%.15f" % sqrt(a)
99999.999989999996615
>>> print "%.15f" % (sqrt(a) * 10**5)
9999999999.000000000000000

所以我寫了一個簡短的 C++ 程序,看看是我的 CPU 以某種方式截?cái)喔↑c(diǎn)數(shù)還是 python.

So I wrote a short C++ program to see if it was my CPU truncating the floating point number or python somehow.

#include <cstdio>
#include <cmath>
#include <stdint.h>

int main()
{
  uint64_t a = 9999999998;
  printf("%ld %.15f %.15f %.15f %.15f
", a, sqrt((double)a), sqrt((double)a)*1e4, sqrt((double)a)*1e5, sqrt((double)a)*1e6);
  a = 999999999998;
  printf("%ld %.15f %.15f %.15f %.15f
", a, sqrt((double)a), sqrt((double)a)*1e5, sqrt((double)a)*1e6, sqrt((double)a)*1e7);
  a = 99999999999998;
  printf("%ld %.15f %.15f %.15f %.15f
", a, sqrt((double)a), sqrt((double)a)*1e6, sqrt((double)a)*1e7, sqrt((double)a)*1e8);
  return 0;
}

哪些輸出:

9999999998 99999.999989999996615 999999999.899999976158142 9999999999.000000000000000 99999999990.000000000000000
999999999998 999999.999998999992386 99999999999.899993896484375 999999999999.000000000000000 9999999999990.000000000000000
99999999999998 9999999.999999899417162 9999999999999.900390625000000 99999999999999.000000000000000 999999999999990.000000000000000

所以看起來我正在努力克服浮點(diǎn)精度的限制,并且 CPU 正在切斷剩余的位,因?yàn)樗J(rèn)為剩余的差異是浮點(diǎn)錯誤.有沒有辦法在 Python 下解決這個問題?還是我需要遷移到 C 并使用 GMP 之類的?

So it looks like I'm running up hard against the limits of floating point precision and the CPU is chopping off the remaining bits because it thinks that the remaining difference is floating point error. Is there a way to work around this under Python? Or do I need to move to C and use GMP or something?

推薦答案

在標(biāo)準(zhǔn)庫中,decimal 模塊可能是您正在尋找的.另外,我發(fā)現(xiàn) mpmath 很有幫助.documentation 也有很多很好的例子(不幸的是我的辦公室電腦沒有 mpmath 已安裝;否則我會驗(yàn)證幾個示例并發(fā)布它們).

In the standard library, the decimal module may be what you're looking for. Also, I have found mpmath to be quite helpful. The documentation has many great examples as well (unfortunately my office computer does not have mpmath installed; otherwise I would verify a few examples and post them).

關(guān)于 decimal 的一個警告模塊,雖然.該模塊包含幾個用于簡單數(shù)學(xué)運(yùn)算的內(nèi)置函數(shù)(例如 sqrt),但這些函數(shù)的結(jié)果可能并不總是與 math 或其他模塊中的相應(yīng)函數(shù)匹配更高的精度(盡管它們可能更準(zhǔn)確).例如,

One caveat about the decimal module, though. The module contains several in-built functions for simple mathematical operations (e.g. sqrt), but the results from these functions may not always match the corresponding function in math or other modules at higher precisions (although they may be more accurate). For example,

from decimal import *
import math

getcontext().prec = 30
num = Decimal(1) / Decimal(7)

print("   math.sqrt: {0}".format(Decimal(math.sqrt(num))))
print("decimal.sqrt: {0}".format(num.sqrt()))

在 Python 3.2.3 中,這會輸出前兩行

In Python 3.2.3, this outputs the first two lines

   math.sqrt: 0.37796447300922719758631274089566431939601898193359375
decimal.sqrt: 0.377964473009227227214516536234
actual value: 0.3779644730092272272145165362341800608157513118689214

如前所述,這并不是您所期望的,您可以看到精度越高,結(jié)果匹配越少.請注意,decimal 模塊在此示例中確實(shí)具有更高的準(zhǔn)確性,因?yàn)樗咏仄ヅ?實(shí)際值.

which as stated, isn't exactly what you would expect, and you can see that the higher the precision, the less the results match. Note that the decimal module does have more accuracy in this example, since it more closely matches the actual value.

這篇關(guān)于浮點(diǎ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 有哪些可能的計(jì)算可以返回 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 的意思不同)
主站蜘蛛池模板: 亚洲 欧美 另类 综合 偷拍 | 久久精品国产久精国产 | 99re视频精品 | 久久亚洲国产精品 | 99精品热视频 | av免费网站在线观看 | 2019精品手机国产品在线 | www.国产| 黄色片视频 | 国产精品国产三级国产aⅴ中文 | 免费精品久久久久久中文字幕 | 国产精品一码二码三码在线 | 免费高清av | 天堂中文av| 97精品超碰一区二区三区 | 56pao在线| 欧美炮房 | 天堂av资源| 夜夜夜久久久 | 欧美精品一区二区在线观看 | 久久久蜜桃 | 亚洲欧美中文日韩在线v日本 | 欧美一页| 涩涩视频在线观看免费 | 精品国产乱码久久久久久久久 | 91久久久久久久久久久 | 91一区二区 | 一区二区视频免费观看 | 精品蜜桃一区二区三区 | 亚洲一区二区在线视频 | 亚洲欧美视频一区二区 | 久久久综合精品 | av免费网站在线观看 | 日日摸夜夜爽人人添av | 99精品欧美一区二区三区 | 国产精品欧美一区二区 | aaa精品 | av大片在线观看 | 国产成人精品一区二区 | 国产一区不卡在线观看 | 亚州精品成人 |