473,395 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 106210

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<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();
}

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<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();
}
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();
}

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<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();
}
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<typename T>
inline bool isinf(T value)
{
return std::numeric_limits<T>::has_infinity() &&
value == std::numeric_limits<T>::infinity();
}
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<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();
}

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<typename T>
inline bool isinf(T value)
{
return std::numeric_limits<T>::has_infinity() &&
value == std::numeric_limits<T>::infinity();
}

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<typename T>
inline bool isanyinf(T value)
{
return value >= std::numeric_limits<T>::min() && value <=
std::numeric_limits<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<typename T>
inline bool isinf(T value)
{
return std::numeric_limits<T>::has_infinity() &&
value == std::numeric_limits<T>::infinity();
}
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

Greg wrote:
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<typename T>
inline bool isinf(T value)
{
return std::numeric_limits<T>::has_infinity() &&
value == std::numeric_limits<T>::infinity();
}
>
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
Thanks for all the comments, very helpful.

zl2k

Jan 17 '07 #11

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

Similar topics

4
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...
4
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);...
12
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...
1
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...
9
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
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 );...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
2
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...
6
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,...
29
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.