5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonで小数点以下の計算結果

Last updated at Posted at 2017-05-30

小数点以下の計算がおかしい

Pythonの小数点数は、「IEEE754 浮動小数点数」というルールを使っているらしい、、なので小数点の計算結果が以下な感じに・・・仕様です!

>>> 0.1 + 0.1 + 0.1
0.30000000000000004

正しい計算結果を返すには、Decimalモジュールを使う方法あるようです。小数点以下を「丸め誤差なし」での計算例になります↓

>>> from decimal import *

>>> Decimal('0.1') + Decimal('0.1') + Decimal('0.1')
Decimal('0.3')
5
5
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?