在Python中的浮点减法 [英] floating point subtraction in python

查看:176
本文介绍了在Python中的浮点减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在python中减去浮点数。
我有价值

$ p $ a $ 1460356156116843.000000 b = 2301.93138123


当我尝试打印 ab 时,结果是 1460356156114541.000000 与实际值相反值 1460356156114541.06861877



在执行浮点运算时,python有什么限制。有没有什么办法在Python中,通过我可以得到这种减法的实际结果?

解决方案

作为所有其他语言的点算术。您可以使用 Decimal 以获得准确的结果:

pre $ 从十进制进位十进制

a =十进制(' 1460356156116843.000000')
b =十进制('2301.93138123')

打印a - b#1460356156114541.06861877


I'm trying to subtract to floating point numbers in python. I've values

a = 1460356156116843.000000, b = 2301.93138123

When I try to print a-b, it's resulting the value 1460356156114541.000000 contrary to the actual value 1460356156114541.06861877.

What are the limitations in python while doing floating point arithmetic. Is there any way in python through which I can get the actual result of this subtraction?

解决方案

Python has the same limitations for floating point arithmetic as all the other languages. You can use Decimal to get the accurate result:

from decimal import Decimal

a = Decimal('1460356156116843.000000')
b = Decimal('2301.93138123')

print a - b # 1460356156114541.06861877

这篇关于在Python中的浮点减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆