Solved

Not A Number - quick fix

Posted on 2001-09-03
9
730 Views
Last Modified: 2007-12-19
I am using GNU C++ and Borland Builder C++

I have written a program, and discovered a fault - However I do not have the time to rewrite the whole program from the ground up as it will take too long. I wish to patch the code instead.

My program sometimes returns NaN becasue it has exceeded a boundary, my idea is to use a if statement to check to see if the return value is real number.


I believe that I need to use <math.h>

I have tried using :

if ( number == NAN)
{
Do this . .. .
 }
else
{
Do that
}

However there is a compiler error NAN not declared.

Any suggestions

Cheers

Will Ryder
0
Comment
Question by:wjryder
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6451051
>>if ( number == NAN)

You'd better use

if ( isnan ( number)) {
}

ans comparing doubles for equality is not a good idea. 'isnan()' is declared in 'float.h'
0
 
LVL 86

Expert Comment

by:jkr
ID: 6451063
BTW - what you are looking for is '_Nan', and it can be found in 'ymath.h'...
0
 

Author Comment

by:wjryder
ID: 6451112
perhaps using finite is better as it will also catch infinity problems

0
 

Author Comment

by:wjryder
ID: 6451115
Is this a Borland compatiable function ?

can't find it  in the help file
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:jkr
ID: 6451127
Try to look up '_isnan()' or '_isinf()' - the functions itself are IEEE, so they should be present in any compiler with floating point support...
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 6451129
BTW, as you mentioned gcc also:

ISINF(3)            Linux Programmer's Manual            ISINF(3)


NAME
       isinf,  isnan,  finite - test for infinity or not-a-number
       (NaN)

SYNOPSIS
       #include <math.h>

       int isinf(double value);

       int isnan(double value);

       int finite(double value);

DESCRIPTION
       The isinf() function returns -1 if value represents  nega-
       tive  infinity,  1  if value represents positive infinity,
       and 0 otherwise.

       The isnan() function returns a non-zero value if value  is
       "not-a-number" (NaN), and 0 otherwise.

       The finite() function returns a non-zero value if value is
       neither infinite nor a "not-a-number" (NaN) value,  and  0
       otherwise.

CONFORMING TO
       BSD 4.3
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6451428
For BCB a bit other:
_isNAN(double d)
_isNANl(long double ld)
to check if given floating-point value for not a
number (NAN).

0
 
LVL 86

Expert Comment

by:jkr
ID: 6451444
Why do they have to diversify these names?

gcc: isnan()
VC:  _isnan()
BCB: _isNAN()

Hmmpf....
0
 
LVL 30

Expert Comment

by:Axter
ID: 6454636
>>Why do they have to diversify these names?

VC is using the correct nameing convension.
According to the standards, any function that is part of the implementation, should start with an underscore, and proceed with a lowercase letter.

The GCC compiler should not be using isnan() with out the underscore prefix.
And it makes no sence to me why BCB would want to have a function that starts with a lower case letter, and then go upercase in the last part of the name.
BCB is not breaking the standards, but it certainly using a stupid naming convention.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
The dreaded error 1935 with VC++ 2008 Redistributable 12 88
Issue accessing member variable of atomic class 16 110
Header of docx file 17 98
FMX TCameraComponent Problem 2 52
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now