nan() Function

The nan() function in C generates a quiet NaN (Not-A-Number) value of type double. NaN values are used to represent undefined or non-representable results in floating-point computations, such as when an operation like the square root of a negative number or 0/0 is performed.


Syntax of nan()

</>
Copy
double nan(const char *tagp);

Parameters

ParameterDescription
tagpAn implementation-specific C-string. If this is an empty string (“”), the function returns a generic NaN value.

The nan() function returns a quiet NaN value which can be useful for signaling undefined or non-representable numerical results. Note that the tag argument can be used by different library implementations to distinguish between various NaN values in an implementation-specific manner. Additionally, similar functions like nanf() and nanl() are available for generating NaN values for the float and long double types respectively.


Examples for nan()

Example 1: Generating a Generic NaN Value Using an Empty Tag

This example demonstrates how to generate a generic NaN value by passing an empty string to nan().

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double value = nan("");
    printf("Generated NaN value: %f\n", value);
    return 0;
}

Explanation:

  1. The program includes the necessary headers <stdio.h> and <math.h>.
  2. The nan() function is called with an empty string to generate a generic NaN value.
  3. The resulting NaN value is printed to the console.

Program Output:

Generated NaN value: nan

Example 2: Generating a Tagged NaN Value

This example shows how a tag string can be used to generate a NaN value, which may help distinguish between different NaN values in an implementation-specific way.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double value = nan("NAN");
    printf("Generated tagged NaN value: %f\n", value);
    return 0;
}

Explanation:

  1. The program includes the <math.h> header for access to the nan() function.
  2. A tagged NaN value is generated by passing the string "NAN" to the function.
  3. The tagged NaN value is printed using printf().

Program Output:

Generated tagged NaN value: nan

Example 3: Using nan() in a Calculation That Yields NaN

This example demonstrates how a computation resulting in an undefined value produces NaN, and how isnan() can be used to verify this result.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double result = sqrt(-1.0);
    if (isnan(result)) {
        printf("The result is NaN\n");
    } else {
        printf("The result is a valid number: %f\n", result);
    }
    return 0;
}

Explanation:

  1. The program attempts to compute the square root of a negative number, which is undefined.
  2. This computation results in a NaN value.
  3. The isnan() function checks whether the result is NaN.
  4. An appropriate message is printed based on the outcome of the check.

Program Output:

The result is NaN

Example 4: Demonstrating NaN Generation for Different Floating-Point Types

This example explains that while nan() generates a NaN value for the double type, similar functions exist for float and long double types.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double nanValue = nan("");
    printf("NaN for double: %f\n", nanValue);

    // Note: nanf() returns a float and nanl() returns a long double.
    return 0;
}

Explanation:

  1. The program shows how to generate a NaN value for the double type using nan().
  2. It highlights that similar functions (nanf() and nanl()) exist for other floating-point types.
  3. The generated NaN value is printed to the console.

Program Output:

NaN for double: nan