作者s90104123 (也許當時忙著微笑和哭泣)
看板Python
標題[問題] 大數字取對數(log)問題??
時間Tue May 19 11:24:21 2020
一般來說用math.log2(數字)就能取對數
但是數字太大好像就不太準
像是2**87-1 會出現87
正確來說應該是86.多吧
請問有什麼解決方法呢?
感謝大大
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.141.88.170 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/Python/M.1589858663.A.325.html
1F:推 pmove: 要絕對準的話,就是要自己寫? 05/19 12:22
2F:→ GeoMeTric: decimal module 05/19 13:47
3F:推 kenduest: decimal 內沒看到 log2(), 但是有log10() ,轉一下 05/19 14:25
4F:→ kenduest: from decimal import Decimal, getcontext 05/19 14:25
5F:→ kenduest: getcontext().prec = 30 05/19 14:25
6F:→ kenduest: r = Decimal(2**87-1).log10() / Decimal(2).log10() 05/19 14:26
7F:→ kenduest: r 應該就是你要的? 05/19 14:26
8F:→ s90104123: 感謝樓上的各位 05/19 18:00