测试纳米 [英] test for nan

查看:81
本文介绍了测试纳米的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个C扩展模块正在返回一些双打。当

双倍超出范围时,数字打印为''nan''。


是否有更好的方法来测试NaN而不是


str(p1)==''nan''


其中p1是浮点数?


python2.3

谢谢,

John Hunter


I have a C extension module that is returning some doubles. When the
doubles get out of range, the numbers print as ''nan''.

Is there a better way to test for NaN than

str(p1)==''nan''

where p1 is a float?

python2.3

Thanks,
John Hunter

推荐答案

在某些时候,John Hunter< jd ****** @ ace.bsd.uchicago.edu>写道:
At some point, John Hunter <jd******@ace.bsd.uchicago.edu> wrote:
我有一个C扩展模块,返回一些双打。当双倍超出范围时,数字打印为''nan''。

是否有更好的方法来测试NaN而不是

str( p1)==''nan''

其中p1是浮点数?
I have a C extension module that is returning some doubles. When the
doubles get out of range, the numbers print as ''nan''.

Is there a better way to test for NaN than

str(p1)==''nan''

where p1 is a float?




NaN的字符串表示形式依赖于系统。在Windows上它是#b $ b,就像#NaN一样。你最好用Jeff Epler的建议

包装isnan()(因为你已经有一个C扩展模块,你可以通过它获得
)那个)。


如果你在使用IEEE浮点表示的东西,

纯python中的这样的东西应该工作:


导入结构

def isnan(x):

s = struct.pack(''d'',x)

如果struct.pack(''h'',1)==''\ x01 \ x00'':

返回s ==''\ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ xf0 \ x7f'':

其他:

返回s ==''\ x7f \ xf8 \x00 \x00 \ x00 \ x00 \ x00 \ x00'':


自结构化以来,对结尾的测试就是存在的(''< d '',x)

抱怨frexp()超出范围。


-

|> | \\ \\ / |<

/ ------------------------------------ ---------- ---------------------------- \

| David M. Cooke

| cookedm(at)physics(dot)mcmaster(dot)ca



The string representation of NaN is system-dependent. On Windows it''s
something like #NaN. You''d be better off with Jeff Epler''s suggestion
of wrapping isnan() (since you already have a C extension module, you
could through it in there).

If you''re on something that uses IEEE floating-point representations,
something like this in pure python should work:

import struct
def isnan(x):
s = struct.pack(''d'', x)
if struct.pack(''h'', 1) == ''\x01\x00'':
return s == ''\x00\x00\x00\x00\x00\x00\xf0\x7f'':
else:
return s == ''\x7f\xf8\x00\x00\x00\x00\x00\x00'':

The test for endianness is there since struct.unpack(''<d'', x)
complains that frexp() is out of range.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca


John Hunter< jd ****** @ ace.bsd.uchicago.edu>写道:
John Hunter <jd******@ace.bsd.uchicago.edu> writes:
我有一个C扩展模块返回一些双打。当双倍超出范围时,数字打印为''nan''。

是否有更好的方法来测试NaN而不是

str( p1)==''nan''

p1是浮点数?
I have a C extension module that is returning some doubles. When the
doubles get out of range, the numbers print as ''nan''.

Is there a better way to test for NaN than

str(p1)==''nan''

where p1 is a float?




我希望如此:那是'超出不可移植的。当然,所有要做的事情
带有nans的
都是不可移植的,所以你将不得不告诉我们更多

你的要求......


干杯,

mwh

-

我有*双手拍手,但是我仍然不确定它是不是一个声音。

当我只用一只手拿着一只手来判断它是不是声音时,我从椅子上掉了下来。

- Peter Hansen,Zen master,comp.lang.python



I''d hope so: that''s exceeding unportable. Of course, all things to do
with nans are unportable, so you''re going to have to tell us more of
your requirements...

Cheers,
mwh

--
I have *both* hands clapping, but I''m still not sure it''s a sound.
When I tried deciding if it were a sound while clapping only one
hand, I fell off my chair.
-- Peter Hansen, Zen master, comp.lang.python


John Hunter写道:
John Hunter wrote:
我有一个C扩展模块,返回一些双打。当双倍超出范围时,数字打印为''nan''。

是否有更好的方法来测试NaN而不是

str( p1)==''nan''

其中p1是浮点数?

python2.3

谢谢,
John Hunter
I have a C extension module that is returning some doubles. When the
doubles get out of range, the numbers print as ''nan''.

Is there a better way to test for NaN than

str(p1)==''nan''

where p1 is a float?

python2.3

Thanks,
John Hunter




据我所知,NaN是唯一一个在将
与自身进行比较时产生错误的值。这导致


def isNaN(x):

return(x == x)== False


Mit freundlichen Gruessen,

Peter Maas


-

--------- -------------------------------------------------- --------

Peter Maas,M + R Infosysteme,D-52070 Aachen,Hubert-Wienen-Str。 24

电话+ 49-241-93878-0传真+ 49-241-93878-20电子邮件 pe********@mplusr.de

------------------------- ------------------------------------------



As far as I know NaN is th only value that yields false when compared
to itself. This leads to

def isNaN(x):
return (x == x) == False

Mit freundlichen Gruessen,

Peter Maas

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplusr.de
-------------------------------------------------------------------


这篇关于测试纳米的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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