如何检查双数是inf还是NaN? [英] how to check a double is inf or NaN?
问题描述
有人能让我知道命令,检查c ++中的值是否为inf或NaN
?我在linux中使用gcc。感谢您的帮助。
zl2k
Can someone let me know the command to check if a value is inf or NaN
in c++? I am using gcc in linux. Thanks for help.
zl2k
推荐答案
zl2k写道:
zl2k wrote:
有人能让我知道命令,检查c ++中的值是inf还是NaN
?我在linux中使用gcc。感谢帮助。
Can someone let me know the command to check if a value is inf or NaN
in c++? I am using gcc in linux. Thanks for help.
man isnan
-----
Ivan
http://www.0x4849.net
man isnan
-----
Ivan
http://www.0x4849.net
zl2k napsal:
zl2k napsal:
hi,
有人能告诉我这个命令在c ++中检查值是否为inf或NaN
?我在linux中使用gcc。感谢您的帮助。
zl2k
Can someone let me know the command to check if a value is inf or NaN
in c++? I am using gcc in linux. Thanks for help.
zl2k
NaN是唯一的值,表达式值==值总是
假。所以:
模板< typename T>
inline bool isnan(T值)
{
返回值!=值;
}
//需要#include< limits>
模板< typename T>
inline bool isinf(T值)
{
return std :: numeric_limits< T> :: has_infinity()&&& br />
value == std :: numeric_limits< T> :: infinity();
}
NaN is the only value, for which is expression value == value always
false. So:
template<typename T>
inline bool isnan(T value)
{
return value != value;
}
// requires #include <limits>
template<typename T>
inline bool isinf(T value)
{
return std::numeric_limits<T>::has_infinity() &&
value == std::numeric_limits<T>::infinity();
}
Ondra Holub napsal:
Ondra Holub napsal:
zl2k napsal:
zl2k napsal:
有人能让我知道命令,检查c ++中的值是否为inf或NaN
?我在linux中使用gcc。感谢您的帮助。
zl2k
Can someone let me know the command to check if a value is inf or NaN
in c++? I am using gcc in linux. Thanks for help.
zl2k
NaN是唯一的值,表达式值==值总是
假。所以:
模板< typename T>
inline bool isnan(T值)
{
返回值!=值;
}
//需要#include< limits>
模板< typename T>
inline bool isinf(T值)
{
return std :: numeric_limits< T> :: has_infinity()&&& br />
value == std :: numeric_limits< T> :: infinity();
}
NaN is the only value, for which is expression value == value always
false. So:
template<typename T>
inline bool isnan(T value)
{
return value != value;
}
// requires #include <limits>
template<typename T>
inline bool isinf(T value)
{
return std::numeric_limits<T>::has_infinity() &&
value == std::numeric_limits<T>::infinity();
}
小修正发布的代码(has_infinity不是函数,所以
括号被删除):
模板< typename T>
inline bool isnan(T值)
{
返回值!=值;
}
//需要#include< limits>
模板< typename T>
内联bool isinf(T值)
{
返回std :: numeric_limits< T> :: has_infinity&&
value == std :: numeric_limits< T>: :infinity();
}
Small correction of posted code (has_infinity is not function, so
parentheses are removed):
template<typename T>
inline bool isnan(T value)
{
return value != value;
}
// requires #include <limits>
template<typename T>
inline bool isinf(T value)
{
return std::numeric_limits<T>::has_infinity &&
value == std::numeric_limits<T>::infinity();
}
这篇关于如何检查双数是inf还是NaN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!