比较浮点数和小数 [英] Comparing float and decimal

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

问题描述

我不确定我是否遵循这个逻辑。有人可以解释为什么浮点数和

整数可以相互比较,十进制可以比较为

整数但十进制数不能与浮点数进行比较吗?


>>来自十进制导入十进制
i = 10
f = 10.0
d =十进制(" 10.00")
i == f



True


>> i == d



True


>> f == d



False


这似乎违反了规则,如果A是等于B和B等于

C然后A等于C.


-

D''Arcy J.M. Cain< da *** @ druid.net |民主是三只狼
http://www.druid.net/darcy/ |和一只羊投票

+1 416 425 1212(mod#0082)(eNTP)|什么是晚餐。

解决方案

2008年9月23日星期二07:20:12 -0400,D''Arcy JM Cain写道:


我不确定我是否遵循这个逻辑。有人可以解释为什么浮点数和

整数可以相互比较,十进制可以比较为

整数但是十进制数不能与浮点数相比较吗?



在比较中,`Decimal`尝试将另一种类型转换为`Decimal`。

如果失败 - 它确实对于花车 - 平等比较

呈现为假。对于订购比较,例如。 ``D(" 10")< 10.0``,它

更加冗长地失败::

TypeError:unorderable类型:Decimal()< float()

十进制教程_表示:


"要从浮点数创建一个Decimal,首先将其转换为串。这个

用于明确提醒转换的详细信息

(包括表示错误)。


请参阅`decimal FAQ`_用于将浮点数转换为小数的方法。


>>>来自十进制导入十进制
i = 10
f = 10.0
d =十进制(" 10.00")
i == f



True


>>> i == d



True


>> > f == d



False


这似乎打破了如果A等于的规则到B和B等于C

然后A等于C.



我不明白为什么传递性应该适用o一般的Python对象。


HTH,


... _decimal tutorial: http://docs.python.org/lib/decimal-tutorial.html

... _decimal常见问题解答: http://docs.python.org /lib/decimal-faq.html


-

RobertStargaming Lehmann



这似乎违反了规则,即如果A等于B且B等于C

然后A等于C.



我不知道为什么传递性应该适用于Python对象一般。



嗯,对于数字来说肯定是个不错的选择,不是吗。

可能是Decimal接受的原因float参数是

无理数或非常长的有理数不能转换为
到十进制没有舍入误差,而Decimal不想要任何部分

吧。但是对我来说似乎毫无意义。


9月23日上午10点08分,Michael Palmer< m_palme ... @ yahoo.cawrote:


可能是Decimal接受float参数的原因是



不接受浮动参数。


I''m not sure I follow this logic. Can someone explain why float and
integer can be compared with each other and decimal can be compared to
integer but decimal can''t be compared to float?

>>from decimal import Decimal
i = 10
f = 10.0
d = Decimal("10.00")
i == f

True

>>i == d

True

>>f == d

False

This seems to break the rule that if A is equal to B and B is equal to
C then A is equal to C.

--
D''Arcy J.M. Cain <da***@druid.net | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what''s for dinner.

解决方案

On Tue, 23 Sep 2008 07:20:12 -0400, D''Arcy J.M. Cain wrote:

I''m not sure I follow this logic. Can someone explain why float and
integer can be compared with each other and decimal can be compared to
integer but decimal can''t be compared to float?

In comparisons, `Decimal` tries to convert the other type to a `Decimal`.
If this fails -- and it does for floats -- the equality comparison
renders to False. For ordering comparisons, eg. ``D("10") < 10.0``, it
fails more verbosely::

TypeError: unorderable types: Decimal() < float()

The `decimal tutorial`_ states:

"To create a Decimal from a float, first convert it to a string. This
serves as an explicit reminder of the details of the conversion
(including representation error)."

See the `decimal FAQ`_ for a way to convert floats to Decimals.

>>>from decimal import Decimal
i = 10
f = 10.0
d = Decimal("10.00")
i == f

True

>>>i == d

True

>>>f == d

False

This seems to break the rule that if A is equal to B and B is equal to C
then A is equal to C.

I don''t see why transitivity should apply to Python objects in general.

HTH,

... _decimal tutorial: http://docs.python.org/lib/decimal-tutorial.html
... _decimal FAQ: http://docs.python.org/lib/decimal-faq.html

--
Robert "Stargaming" Lehmann


This seems to break the rule that if A is equal to B and B is equal to C
then A is equal to C.


I don''t see why transitivity should apply to Python objects in general.

Well, for numbers it surely would be a nice touch, wouldn''t it.
May be the reason for Decimal to accept float arguments is that
irrational numbers or very long rational numbers cannot be converted
to a Decimal without rounding error, and Decimal doesn''t want any part
of it. Seems pointless to me, though.


On Sep 23, 10:08 am, Michael Palmer <m_palme...@yahoo.cawrote:

May be the reason for Decimal to accept float arguments is that

NOT to accept float arguments.


这篇关于比较浮点数和小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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