473,704 Members | 2,347 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 106326

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
13409
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. So, am I doing the right thing to remove all embedded double quotes from my data and write...
4
2414
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
15094
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
1234
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 know an easy way to do this? Any help is really appreciated! Thanks
9
2490
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
4102
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
2507
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
2
1927
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
2916
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
2959
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
8764
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9020
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8974
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7879
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4435
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3130
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
2
2468
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2074
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.