Solving NAN Handling in C Programming

  • Thread starter Thread starter Alamino
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around handling "nan" (not a number) results in C programming, particularly in the context of mathematical functions like logarithms. Participants explore methods to detect "nan" values and adjust program flow accordingly, with a focus on maintaining the integrity of calculations in scenarios involving constraints such as probabilities.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks assistance in implementing a check for "nan" results from a function, indicating the need for a procedural change when such results occur.
  • Another participant explains that "nan" can arise from operations like taking the square root of negative numbers and suggests using the isnan() function from math.h to check for "nan" values.
  • A different approach is proposed, where a participant mentions that "nan" can be detected using the condition (x != x), although this may not always be reliable due to compiler optimizations.
  • One participant expresses the need to identify "nan" specifically in the context of a logarithm function and mentions that they cannot rewrite the code to avoid the issue.
  • A suggestion is made to rewrite the logarithm function to handle nonpositive arguments by signaling an error, providing a code example for this approach.
  • Another participant clarifies that their function involves a summation of logarithms in a minimization problem, where the variables represent probabilities, and they need to adjust the step size to avoid "nan" results while maintaining the constraints of the problem.

Areas of Agreement / Disagreement

Participants present various methods for detecting "nan" values and propose different strategies for handling them, but there is no consensus on a single solution. The discussion remains unresolved regarding the best approach to take in the specific context of the logarithm function and the associated constraints.

Contextual Notes

Participants express limitations in their current implementations, particularly regarding the inability to rewrite existing code and the challenges posed by the mathematical properties of the functions involved.

Alamino
Messages
69
Reaction score
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
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().
 
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.
 
I've just tested the suggestions and they worked well.

Thanks for saving me a lot of time!
 
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);
}
 
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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
Replies
6
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 14 ·
Replies
14
Views
35K
Replies
53
Views
5K
  • · Replies 30 ·
2
Replies
30
Views
8K
Replies
20
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 17 ·
Replies
17
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K