473,544 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to check a double is inf or NaN?

hi,
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

Jan 15 '07 #1
10 106261

zl2k wrote:
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

Jan 15 '07 #2

zl2k napsal:
hi,
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 is the only value, for which is expression value == value always
false. So:

template<typena me T>
inline bool isnan(T value)
{
return value != value;
}

// requires #include <limits>
template<typena me T>
inline bool isinf(T value)
{
return std::numeric_li mits<T>::has_in finity() &&
value == std::numeric_li mits<T>::infini ty();
}

Jan 15 '07 #3

Ondra Holub napsal:
zl2k napsal:
hi,
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 is the only value, for which is expression value == value always
false. So:

template<typena me T>
inline bool isnan(T value)
{
return value != value;
}

// requires #include <limits>
template<typena me T>
inline bool isinf(T value)
{
return std::numeric_li mits<T>::has_in finity() &&
value == std::numeric_li mits<T>::infini ty();
}
Small correction of posted code (has_infinity is not function, so
parentheses are removed):

template<typena me T>
inline bool isnan(T value)
{
return value != value;

}

// requires #include <limits>
template<typena me T>
inline bool isinf(T value)
{
return std::numeric_li mits<T>::has_in finity &&
value == std::numeric_li mits<T>::infini ty();
}

Jan 15 '07 #4
Ondra Holub wrote:
Ondra Holub napsal:
zl2k napsal:
hi,
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
template<typena me T>
inline bool isnan(T value)
{
return value != value;

}

// requires #include <limits>
template<typena me T>
inline bool isinf(T value)
{
return std::numeric_li mits<T>::has_in finity &&
value == std::numeric_li mits<T>::infini ty();
}
Why not:

#include <cmath>

...
if ( std::isinf( value ))
{
// value is infinity
}

if ( std::isnan( value ))
{
// value is not a number
}

Greg

Jan 15 '07 #5

Ondra Holub wrote:
Ondra Holub napsal:
zl2k napsal:
hi,
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
// requires #include <limits>
template<typena me T>
inline bool isinf(T value)
{
return std::numeric_li mits<T>::has_in finity() &&
value == std::numeric_li mits<T>::infini ty();
}
This implementation of isinf() incorrectly returns false when value is
equal to negative infinity.

Greg

Jan 15 '07 #6

Greg napsal:
Ondra Holub wrote:
Ondra Holub napsal:
zl2k napsal:
hi,
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
template<typena me T>
inline bool isnan(T value)
{
return value != value;

}

// requires #include <limits>
template<typena me T>
inline bool isinf(T value)
{
return std::numeric_li mits<T>::has_in finity &&
value == std::numeric_li mits<T>::infini ty();
}

Why not:

#include <cmath>

...
if ( std::isinf( value ))
{
// value is infinity
}

if ( std::isnan( value ))
{
// value is not a number
}

Greg
isnan is part of C99 standard. It is not required in current C++
standard (because it is from 1998). Although many compilers support it,
it is not 100% portable. See
http://www.parashift.com/c++-faq-lit...html#faq-29.15

Jan 16 '07 #7
Greg napsal:
Ondra Holub wrote:
Ondra Holub napsal:
zl2k napsal:
hi,
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
>
// requires #include <limits>
template<typena me T>
inline bool isinf(T value)
{
return std::numeric_li mits<T>::has_in finity() &&
value == std::numeric_li mits<T>::infini ty();
}

This implementation of isinf() incorrectly returns false when value is
equal to negative infinity.

Greg
Question was, how to check for infinity (which I understand positive
infinity as 1 is understand +1), not how to check for positive or
negative infinity. Of course, it does not detect -inf. If there is
necessary to detect any infinity, it may be done for example this way:

#include <limits>
template<typena me T>
inline bool isanyinf(T value)
{
return value >= std::numeric_li mits<T>::min() && value <=
std::numeric_li mits<T>::max();
}

Jan 16 '07 #8
Ondra Holub wrote:
>
isnan is part of C99 standard. It is not required in current C++
standard (because it is from 1998). Although many compilers support it,
it is not 100% portable. See
The current C++ standard is from 2003, a technical revision of the 1998
standard.

isnan is part of TR1, and has been incorporated into the working draft
for C++0x.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jan 16 '07 #9
Ondra Holub wrote:
Greg napsal:
Ondra Holub wrote:
Ondra Holub napsal:
zl2k napsal:
hi,
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

// requires #include <limits>
template<typena me T>
inline bool isinf(T value)
{
return std::numeric_li mits<T>::has_in finity() &&
value == std::numeric_li mits<T>::infini ty();
}
This implementation of isinf() incorrectly returns false when value is
equal to negative infinity.

Question was, how to check for infinity (which I understand positive
infinity as 1 is understand +1), not how to check for positive or
negative infinity.
The standard routine, isinf() in <math.htests for whether its
argument has an infinite value. Therefore isinf() returns true when
called with either positive or negative infinity, because both are
infinite values.

Implementing another isinf() function that performs a similar, but
different test, would just cause confusion - at best. A programmer
would have to make sure which isinf() is being called in a particular
fil - just to know what its return value means.

Greg

Jan 16 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
13309
by: (PeteCresswell) | last post by:
Is his just a flat-out "No-No" or is there some workaround when it comes time for SQL searches and DAO.FindFirsts against fields containing same? I can see maybe wrapping the value searched for in single quotes to deal with embedded double quotes, but if there are both embedded single AND double quotes, I can't see any way to deal with it. ...
4
2401
by: Lodewijk Smit | last post by:
Hi, In the C standard of 1999 additional mathematical functions are added. For example, beside the traditional double sin(double x); there are also the functions: float sinf(float x); long double sinl(long double x);
12
15075
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition should be kept for references to previous element of list. Here is my solution below: struct node {
1
1223
by: Matt | last post by:
I am wanting to make a login for my VB app. Basically what I want to do is have a user enter their username and click "Login". The program then binds to the user object in AD. Then it checks to see if the user is a member of a pre-defined group. I have everything going except for how to check to see if a user is a member of a group. Does anyone...
9
2480
by: RJ | last post by:
Hi All, Suppose i have a double pointer, how do I check the content of that pointer is a 8/16/24/32 bit data? Any suggestion!! Thanks, Raja
32
4051
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon ); } int main() { std::deque<double> pt ;
17
2473
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
2
1920
by: shuisheng | last post by:
Dear All, Assume I have a class for a cuboid domain. The domain is defined by the cuboid's lower corner, such as (0, 0, 0), and upper corner, such as (1, 1, 1). The upper corner should be always higher than the lower corner. I write a code as below class Domain { private:
6
2905
by: Alexander Stoyakin | last post by:
Hello, please advise on the following issue. I need to check that difference between two double values is not higher than defined limit. int main() { double limit = 0.3; double val1 = 0.5, val2 = 0.2; if ( (val1 - val2) limit )
29
2931
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any bug , i am still c++ beginner
0
7371
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7622
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7716
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5925
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5305
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3425
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1848
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
676
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.