Solving NAN Handling in C Programming

  • Thread starter Alamino
  • Start date
In summary, the conversation discusses a problem with writing a program that sometimes results in a "nan" output. The solution proposed is to use an "if" statement to execute a special subroutine when the function returns "nan." Other suggestions include using math.h functions and rewriting the code to handle exceptional cases. However, the main issue is that the function being used is a summation of logarithms and the goal is to rescale the step size to prevent "nan" values from occurring.
  • #1
Alamino
71
0
I'm trying to write a program but I face a little problem (and probably very simple): there is a function that sometimes is calculated outside of its domain and then gives the result "nan".

I need to put an "if" that says that everytime that function returns "nan" the program will execute some procedure.

Can anyone help me with this?
 
Technology news on Phys.org
  • #2
If you don't known what nan is, it means "not a number." This occurs when you do things like take the square root of negative one.

To check for this, math.h includes this function:
int isnan( float value )

math.h also includes isinf() and finite() if your interested.

If you just want to include an if statement without using math.h simply use this:

if( x != x )

It might look strange, but it works (most of the time). It sometimes doesn't work because of your compiler optimization options. It will just think the statement is always false.

In the rare ocassions that I get a nan I just rewrite my code to handle exceptional cases better so i don't have to use isnan().
 
  • #3
Thanks for the help.

I cannot rewrite the code, because I need to identify the "nan" in the program: there is a function that calculates a logarithm in my program and everytime the log gives a nan, I need to change the flow of the program to a special subroutine that will use another function only in this case.

I will try to use the other solutions you gave.

Thanks again.
 
  • #4
I've just tested the suggestions and they worked well.

Thanks for saving me a lot of time!
 
  • #5
there is a function that calculates a logarithm in my program and everytime the log gives a nan, I need to change the flow of the program to a special subroutine that will use another function only in this case.

What dduardo was suggesting is that you rewrite the function that calculates the logarithm to signal an error in some way if the argument to the logarithm is nonpositive. e.g.:

Code:
int foo(double param, double *result)
{
  if(param <= 0) return 1;
  *result = log(param);
  return 0;
}

void bar(double param)
{
  double value;

  if(foo(param, &value)) {
    printf("Invaild argument.\n");
    return;
  }

  printf("The logarithm is %f.\n", value);
}
 
  • #6
I'm grateful for the suggestion, but the problem is that my function is a summation of logarithms. I will explain: I have a minimization problem with a barrier function. The minimization involves a d-dimensional variable with components that cannot be negative because they represent probabilities. But when I use backtracking line search with this function, sometimes the function calculated at the new point (without any scale factor multiplying the step) falls outside the barrier and gives the nan value. So, what I need is just test if the function is nan to rescale the step size such that the new point will fall inside the domain of the function plus the barrier.

But thanks anyway.
 

1. What is NAN in C Programming?

NAN stands for "Not a Number" and is a special value used to represent invalid or undefined numerical values in C programming.

2. Why is it important to handle NAN in C Programming?

Handling NAN is important because operations involving NAN can produce unexpected results or even crash the program. Therefore, it is necessary to properly handle NAN to ensure the program runs smoothly and accurately.

3. How can NAN be detected in C Programming?

NAN can be detected using the isnan() function, which returns a non-zero value if the given argument is NAN. Alternatively, the macro isnan() can also be used for the same purpose.

4. What is the most common approach to handling NAN in C Programming?

The most common approach to handling NAN in C Programming is to use conditional statements (if/else) to check for NAN before performing any mathematical operations. If NAN is detected, the program can display an error message or prompt the user for valid input.

5. Are there any built-in functions in C Programming to handle NAN?

Yes, C Programming provides the functions isnan(), isinf(), and signbit() which can be used to detect and handle NAN, infinity, and sign information respectively.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
4
Views
463
  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
Replies
14
Views
30K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top