什么时候是min(a,b)!= min(b,a)? [英] When is min(a, b) != min(b, a)?

查看:102
本文介绍了什么时候是min(a,b)!= min(b,a)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题可能已在

新闻中提及:< ma ************************** ************* @ py thon.org但我没有完全理解这个解释。基本上我有这个:


>> a = float(6)
b = float(''nan'')
min(a,b)



6.0


>> min(b,a)



nan


>> max (a,b)



6.0


>> max(b,a)



nan


之前我不知道会发生什么,但我当然不希望这是
。所以我的问题是一个数字的最小值/最大值和NaN是什么?或者是没有定义的
(我预计会有一个例外是

筹集或者NaN在每种情况下都返回了。)


作为一个指数,我可以依赖上述行为,或者它是否可以改变(b)b可以修改最小/最大错误:-)?

解决方案

''NaN''表示不是数字。根据Python语义,如果你尝试将
与任何其他浮点数进行比较,它应该返回FALSE。

就像


>>>
1.0''abc''



False


>>> ;



因为它总是返回FALSE,所以你的问题并不令人意外。


如果你想获得不定数,你会使用''inf''或'' - ''',来自

IEEE 754语义:


>>> a = float(6)
b = float(''inf'')
c = float('' - inf'')



Albert Hopkins写道:


此问题可能已在

新闻中提及:< ma ********************** ***************** @ py thon.org但我没有完全理解这个解释。基本上我有这个:


>> a = float(6)

>> ; b = float(''nan'')

>> min(a,b)



6.0


>> min(b,a)



nan


>> max(a,b)



6.0


>> max(b,a)



nan


之前我不知道会发生什么,但我当然没想到

这个。所以我的问题是一个数字的最小值/最大值和NaN是什么?或者是没有定义的
(我预计会有一个例外是

筹集或者NaN在每种情况下都返回了。)


作为一个指数,我可以依赖上述行为,或者它是否可以改变(b)b可以修改也许最小/最大的错误:-)?


''NaN''表示非数字。根据Python语义,如果你尝试将
与任何其他浮点数进行比较,它应该返回FALSE。

就像:


>>> 1.0''abc''



错误


因为它总是返回FALSE,所以你的问题并不令人意外。


如果你希望获得不定数字,你会使用''inf''表示正面

不定式或'-inf''表示否定不定式,来自

IEEE 754语义,就像:


>>> a = float(6)
b = float(''inf'')
c = float('' - inf'')



欲了解更多信息,PEP-0754可能会有所帮助。


On Ja 21日凌晨3点15分,Albert Hopkins< mar ... @ python.invalidwrote:


此问题可能已在
$中提及b $ b< news:ma *************************************** @ py thon .org但我没有完全理解这个解释。基本上我有这个:


>> a = float(6)

>> ; b = float(''nan'')

>> min(a,b)



6.0


>> min(b,a)



nan


>> max(a,b)



6.0


>> max(b,a)



nan


之前我不知道会发生什么,但我当然没想到

这个。所以我的问题是一个数字的最小值/最大值和NaN是什么?或者是没有定义的
(我预计会有一个例外是

筹集或者NaN在每种情况下都返回了。)


作为一个指数,我可以依赖上述行为,或者它是否可以改变(b)b可以修改也许最小/最大的错误:-)?



我绝对不是浮点专家,但我确实发现了这个:
http://en.wikipedia.org/wiki/IEEE_754r#min_and_max


PS您使用什么平台/编译器用于Python?


- Paddy。


This issue may have been referred to in
news:<ma***************************************@py thon.orgbut I didn''t
entirely understand the explanation. Basically I have this:

>>a = float(6)
b = float(''nan'')
min(a, b)

6.0

>>min(b, a)

nan

>>max(a, b)

6.0

>>max(b, a)

nan

Before I did not know what to expect, but I certainly didn''t expect
this. So my question is what is the min/max of a number and NaN or is it
not defined (for which I would have expected either an exception to be
raised or NaN returned in each case).

As a corrollary would I be able to rely on the above behavior or is it
subject to change (to fix a bug in min/max perhaps :-)?

解决方案

''NaN'' means "Not a number". according to Python semantics, if you try
to compare it with any other float numbers, it should return FALSE.
just like

>>>
1.0 ''abc''

False

>>>

Since it always return FALSE, it is not a surprise for your question.

If you wish to get infinitive number, you''d use ''inf'' or ''-inf'', from
IEEE 754 semantics:

>>>a=float(6)
b=float(''inf'')
c=float(''-inf'')


Albert Hopkins wrote:

This issue may have been referred to in
news:<ma***************************************@py thon.orgbut I didn''t
entirely understand the explanation. Basically I have this:

>>a = float(6)
>>b = float(''nan'')
>>min(a, b)

6.0

>>min(b, a)

nan

>>max(a, b)

6.0

>>max(b, a)

nan

Before I did not know what to expect, but I certainly didn''t expect
this. So my question is what is the min/max of a number and NaN or is it
not defined (for which I would have expected either an exception to be
raised or NaN returned in each case).

As a corrollary would I be able to rely on the above behavior or is it
subject to change (to fix a bug in min/max perhaps :-)?


''NaN'' means "Not a number". according to Python semantics, if you try
to compare it with any other float numbers, it should return FALSE.
just like:

>>>1.0 ''abc''

False

Since it always return FALSE, it is not a surprise for your question.

If you wish to get infinitive number, you''d use ''inf'' for positive
infinitive or ''-inf'' for negative infinitive, from
IEEE 754 semantics, just like:

>>>a=float(6)
b=float(''inf'')
c=float(''-inf'')

For more information, PEP-0754 may be helpful.


On Jan 21, 3:15 am, Albert Hopkins <mar...@python.invalidwrote:

This issue may have been referred to in
<news:ma***************************************@py thon.orgbut I didn''t
entirely understand the explanation. Basically I have this:

>>a = float(6)
>>b = float(''nan'')
>>min(a, b)

6.0

>>min(b, a)

nan

>>max(a, b)

6.0

>>max(b, a)

nan

Before I did not know what to expect, but I certainly didn''t expect
this. So my question is what is the min/max of a number and NaN or is it
not defined (for which I would have expected either an exception to be
raised or NaN returned in each case).

As a corrollary would I be able to rely on the above behavior or is it
subject to change (to fix a bug in min/max perhaps :-)?

I am definitely NOT a floating point expert, but I did find this:
http://en.wikipedia.org/wiki/IEEE_754r#min_and_max

P.S. What platform /Compiler are you using for Python?

- Paddy.


这篇关于什么时候是min(a,b)!= min(b,a)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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