我有以下几行代码:
import math as mt
...
...
...
if mt.isnan (coord0):
print (111111, coord0, type (coord0), coord0 in (None, mt.nan))
print (222222, mt.nan, type (mt.nan), mt.nan in (None, mt.nan))
它打印:
111111 nan <class 'float'> False
222222 nan <class 'float'> True
我很迷茫... 有什么解释吗?
Python 3.6.0、Windows 10
我对 Python 解释器的质量有坚定的信心...... 而且我知道,每当计算机看起来出错时,实际上是我弄错了...... 那我错过了什么?
[编辑]
(对@COLDSPEED 的 react )
确实 ID 不同:
print (111111, coord0, type (coord0), id (coord0), coord0 in (None, mt.nan))
print (222222, mt.nan, type (mt.nan), id (mt.nan), mt.nan in (None, mt.nan))
打印:
111111 nan <class 'float'> 2149940586968 False
222222 nan <class 'float'> 2151724423496 True
也许 whey nan 不是真正的单例是有充分理由的。但我还不明白。在我看来,这种行为很容易出错。
[编辑2]
(回应@Sven Marnach)
仔细阅读@Sven Marnach 的回答让我明白了。这确实是设计事物时遇到的那种妥协。
冰还是很薄:
Having a in (b,) return True if id (a) == id (b) 似乎与nan 不等于 nan 的 IEEE-754 标准。
结论必须是,虽然 a 在聚合中,但同时它又不是,因为聚合中的东西,即 b 有被 IEEE 标准认为不等于 a。
我想从现在开始我会使用 isnan...
最佳答案
您看到的行为是对 Python 中的 in 运算符的优化以及 nan 与自身比较不相等的事实的人工制品,如 IEEE-754 所要求的标准。
Python 中的in 运算符返回迭代器中的任何元素是否等于您要查找的元素。表达式 x in it 本质上计算为 any(x == y for y in it),除了 CPython 应用了额外的优化:以避免必须调用 __eq__ 在每个元素上,解释器首先检查 x 和 y 是否是相同的对象,在这种情况下它立即返回 True.
这种优化几乎适用于所有对象。毕竟,每个对象都将自己与自己进行比较,这是相等性的基本属性之一。但是, float 的 IEEE-754 标准要求 nan != nan,因此 NaN 打破了这个假设。这会导致您看到奇怪的行为:如果一个 nan 恰好是迭代器中的 同一对象 作为 nan,则上述优化导致 in 运算符返回 True。但是,如果迭代器中的 nan 不是同一个对象,Python 会回退到 __eq__(),你会得到 False。
关于python - math.nan 与 'in' 运算符结合时的矛盾行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48808149/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我看到这个错误:translationmissing:da.datetime.distance_in_words.about_x_hours我的语言环境文件:http://pastie.org/2944890我的看法:我已将其添加到我的application.rb中:config.i18n.load_path+=Dir[Rails.root.join('my','locales','*.{rb,yml}').to_s]config.i18n.default_locale=:da如果我删除I18配置,帮助程序会处理英语。更新:我在config/enviorments/devolpment
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串