Cmath functions: isfinite(), isinf(), isnan()

in

Let’s talk about some of the most underrated functions in Cmath library isfinite(), isinf(), and isnan(). These guys might not get as much love as their more popular counterparts like sin() or cos(), but they are just as important when it comes to checking if a number is, well… finite.

First up, we have isfinite(). This function takes in one argument (a float or double) and returns 1 if that value is neither infinite nor NaN (not-a-number), otherwise it returns 0. Pretty straightforward, right? Let’s take a look at some examples:

// This script checks if a number is finite using the functions isfinite() and isnan().

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

int main() {
    float x = 1.0/0.0; // Corrected to use floating point numbers for division
    int result_isfinite = isfinite(x); // Calls the isfinite() function to check if x is finite
    printf("Is %f finite? %d\n", x, result_isfinite); // Prints the value of x and the result of isfinite()

    double y = NAN; // Corrected to use the NAN constant for not-a-number
    int result_isnan = isnan(y); // Calls the isnan() function to check if y is a NaN
    printf("Is %lf a NaN? %d\n", y, result_isnan); // Prints the value of y and the result of isnan()

    float z = 3.14159265; // Corrected to use a finite value
    int result_finite = isfinite(z); // Calls the isfinite() function to check if z is finite
    printf("Is %f finite? %d\n", z, result_finite); // Prints the value of z and the result of isfinite()

    return 0;
}

In this example, we’re checking if x and y are finite or not. As you can see, the output for x is 0 (false) because it’s an infinite value, while the output for y is also 0 (true), but in a different way because y is NaN! The output for z is 1 (true) since it’s a finite number.

Now isinf(). This function works similarly to isfinite(), except that it specifically checks if the input value is infinite, either positive or negative. If the value is not infinite and not NaN, then isinf() returns 0:

// This script checks the functionality of the isinf() function in C, which is used to determine if a given value is infinite or not.

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

int main() {
    float x = 1.0 / 0.0; // Assigns a value of positive infinity to x
    int result_isinf = isinf(x); // Checks if x is infinite and stores the result in result_isinf
    printf("Is %f infinite? %d\n", x, result_isinf); // Prints the value of x and the result of the isinf() function
    
    double y = -INFINITY; // Assigns a value of negative infinity to y
    int result_isinf2 = isinf(y); // Checks if y is infinite and stores the result in result_isinf2
    printf("Is %lf infinite? %d\n", y, result_isinf2); // Prints the value of y and the result of the isinf() function
    
    float z = 3.14159265; // Assigns a finite value to z
    int result_finite = isinf(z); // Checks if z is infinite and stores the result in result_finite
    printf("Is %f infinite? %d\n", z, result_finite); // Prints the value of z and the result of the isinf() function
    
    return 0;
}

// The output for x is 1 (true) since it's a finite number.
// The output for y is also 1 (true) since it's a finite number.
// The output for z is 0 (false) since it's a finite number and not infinite.

In this example, we’re checking if x and y are infinite or not. As you can see, the output for x is 1 (true) because it’s an infinite value with a positive sign, while the output for y is also 1 (true), but in a different way because y is negative infinity! The output for z is 0 (false) since it’s not infinite.

Lastly, we have isnan(). This function works similarly to isfinite() and isinf(), except that it specifically checks if the input value is NaN:

// This script checks for NaN (Not-a-Number) values using the functions isnan(), isfinite(), and isinf().

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

int main() {
    float x = NAN; // Assigns the value of NaN to the variable x
    int result_isnan = isnan(x); // Checks if x is a NaN value and assigns the result to the variable result_isnan
    printf("Is %f a NaN? %d\n", x, result_isnan); // Prints the value of x and the result of the isnan() function
    
    double y = 3.14159265; // Assigns a finite value to the variable y
    int result_finite = isfinite(y); // Checks if y is a finite value and assigns the result to the variable result_finite
    printf("Is %lf a finite value? %d\n", y, result_finite); // Prints the value of y and the result of the isfinite() function
    
    double z = -INFINITY; // Assigns the value of negative infinity to the variable z
    int result_inf = isinf(z); // Checks if z is an infinite value and assigns the result to the variable result_inf
    printf("Is %lf an infinite value? %d\n", z, result_inf); // Prints the value of z and the result of the isinf() function
    
    return 0;
}

In this example, we’re checking if x is NaN or not. As you can see, the output for x is 1 (true) because it’s a NaN value, while the output for y is 0 (false) since it’s not NaN.

SICORPS