比较浮点数和小数

编程入门 行业动态 更新时间:2024-10-28 10:32:33
本文介绍了比较浮点数和小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不确定我是否遵循这个逻辑。有人可以解释为什么浮点数和 整数可以相互比较,十进制可以比较为 整数但十进制数不能与浮点数进行比较吗?

>>来自十进制导入十进制 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 |民主是三只狼 www.druid/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: docs.python/lib/decimal-tutorial.html ... _decimal常见问题解答: docs.python /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 Decimali = 10f = 10.0d = 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 | Democracy is three wolves www.druid/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 Decimali = 10f = 10.0d = 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: docs.python/lib/decimal-tutorial.html ... _decimal FAQ: docs.python/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.

更多推荐

比较浮点数和小数

本文发布于:2023-11-10 06:52:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574654.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数   浮点数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!