比较纳米“数字” [英] comparing nan "number"
问题描述
您好!
我需要知道数学公式的结果是否为nan(非数字)。
我该怎么办?
谢谢,
B.
< blockquote>
" Bo ?? tjan Jerko" <博*********** @ mf.uni-lj.si>在消息新闻中写道:ma ************************************* @ pytho n.org .. 。
你好!
我需要知道数学公式的结果是否为nan(非数字)。
我该怎么做?
谢谢,
B。
我使用repr()作为解决方法:
INF = 1e9999
NAN = INF - INF
def isnan(x):
.... rx = repr(float(x))
....返回rx == repr(NAN)或rx == repr(-NAN)
.... t = 2 * NAN
t == NAN
假t是NAN
False isnan(t)
真
您还可以使用数字的二进制表示:
import struct
NAN_POS = struct.pack(''d'',NAN)
NAN_NEG = struct.pack(''d'', -NAN)
def isnan2(x):
.... xs = struct.pack(''d'',float (x))
.... ret urn xs == NAN_POS或xs == NAN_NEG
....
HTH
-
Georgy Pruss
E ^ mail:''ZDAwMTEyMHQwMzMwQGhvdG1haWwuY29t\ n''。decode(''base6 4'')
Bo ?? tjan Jerko< bo *********** @ mf.uni-lj.si>写道:
你好!
我需要知道数学公式的结果是否为nan(非数字)。
我该怎么办那个?
我所知道的唯一非hacky方式是安装scipy并使用scipy.isnan。
'' as
Bo ?? tjan Jerko< bo *********** @ mf.uni-lj.si>写道:
我需要知道数学公式的结果是否为nan(非数字)。
我该怎么做?
我想:
NAN = any_calculation_that_returns_nan()
如果some_other_calculation()是NAN:
...
John
Hello !
I need to know if the result of math formula is nan (Not a number).
How can I do that?
Thanks,
B.
"Bo??tjan Jerko" <bo***********@mf.uni-lj.si> wrote in message news:ma*************************************@pytho n.org...Hello !
I need to know if the result of math formula is nan (Not a number).
How can I do that?
Thanks,
B.
I used repr() as a workaround:
INF = 1e9999
NAN = INF - INF
def isnan( x ): .... rx = repr(float(x))
.... return rx==repr(NAN) or rx==repr(-NAN)
.... t = 2*NAN
t == NAN False t is NAN False isnan(t) True
You can also use binary representation of numbers:
import struct
NAN_POS = struct.pack(''d'',NAN)
NAN_NEG = struct.pack(''d'',-NAN)
def isnan2( x ):
.... xs = struct.pack(''d'',float(x))
.... return xs==NAN_POS or xs==NAN_NEG
....
HTH
--
Georgy Pruss
E^mail: ''ZDAwMTEyMHQwMzMwQGhvdG1haWwuY29t\n''.decode(''base6 4'')
Bo??tjan Jerko <bo***********@mf.uni-lj.si> writes:
Hello !
I need to know if the result of math formula is nan (Not a number).
How can I do that?
The only non-hacky way I know of is installing scipy and using scipy.isnan.
''as
Bo??tjan Jerko <bo***********@mf.uni-lj.si> writes:
I need to know if the result of math formula is nan (Not a number).
How can I do that?
I suppose:
NAN = any_calculation_that_returns_nan()
if some_other_calculation() is NAN:
...
John
这篇关于比较纳米“数字”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!