[英]How can I convert 'np.inf' to an integer type?
I want to convert the return type of np.inf
to int
, by default it returns float
type. 我想将np.inf
的返回类型转换为int
,默认情况下它将返回float
类型。
I have tried the followings, but both give erros. 我已经尝试了以下方法,但是都给出了错误。
int(np.inf)
OverflowError: cannot convert float infinity to integer OverflowError:无法将浮点无穷大转换为整数
(np.inf).astype(int64)
AttributeError: 'float' object has no attribute 'astype' AttributeError:“ float”对象没有属性“ astype”
There is a standard for floating point numbers: "IEEE 754" (it is not specific to python). 浮点数有一个标准:“ IEEE 754”(不是特定于python)。 This standard reserves some special bytes sequences for +/-Infinity and NaN. 该标准为+/- Infinity和NaN保留了一些特殊的字节序列。 For integer such special values are not reserved so it is impossible to have an int infinity. 对于整数,不保留此类特殊值,因此不可能具有int无限大。
I have done some experimenting: you can do np.array([np.inf]).astype(int)[0]
this will give you -9223372036854775808
( -(2 ** 64)/2
). 我做了一些实验:您可以执行np.array([np.inf]).astype(int)[0]
这将为您提供-9223372036854775808
( -(2 ** 64)/2
)。 np.array([np.nan]).astype(int)[0]
also produces the same value. np.array([np.nan]).astype(int)[0]
也产生相同的值。
Unfortunately as the comments suggest no there isn't, but if you know the integer type you want, you can use np.iinfo and pick max or min 不幸的是,由于注释表明没有,但如果您知道所需的整数类型,则可以使用np.iinfo并选择max或min
np.iinfo(np.int32).max # ---- 2147483647
np.iinfo(np.int32).min # ---- -2147483648
np.iinfo(np.int64).max # ---- 9223372036854775807
np.iinfo(np.int64).min # ---- -9223372036854775808
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.