| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Python Decimal

提供: MyMemoWiki
2020年2月15日 (土) 08:38時点におけるPiroto (トーク | 投稿記録)による版
ナビゲーションに移動 検索に移動

Python Decimal

Python | [Python Osmosis] |

Decimal

  • from decimal で利用
  • 小数点以下の計算も正しく行う
  • getcontext().prec で有効桁数を設定
  1. Python 2.6.3 (r263rc1:75186, Oct 2 2009, 20:40:30) [MSC v.1500 32 bit (Intel)] on win32
  2. Type "copyright", "credits" or "license()" for more information.
  3.  
  4. ****************************************************************
  5. Personal firewall software may warn about the connection IDLE
  6. makes to its subprocess using this computer's internal loopback
  7. interface. This connection is not visible on any external
  8. interface and no data is sent to or received from the Internet.
  9. ****************************************************************
  10. >>> from decimal import *
  11. >>> Decimal('0.70') * Decimal('1.05')
  12. Decimal('0.7350')
  13. >>> .70 * 1.05
  14. 0.73499999999999999
  15. >>> Decimal('1.00') % Decimal('.10')
  16. Decimal('0.00')
  17. >>> 1.00 % 0.10
  18. 0.09999999999999995
  19. >>> sum([Decimal('0.1')] * 10) == Decimal('1.0')
  20. True
  21. >>> sum([0.1] * 10) == 1.0
  22. False
  23. >>> getcontext().prec = 36
  24. >>> Decimal(1) / Decimal(7)
  25. Decimal('0.142857142857142857142857142857142857')