Current Location: Home> Latest Articles> How to use is_finite and is_nan in combination

How to use is_finite and is_nan in combination

gitbox 2025-05-29

When performing numerical calculations and data verification, PHP provides some functions for detecting floating-point numbers, such as is_finite() and is_nan() . These two functions are used to determine whether a value is a finite value (that is, it is not infinite or infinite) and whether it is a "non-numeric value" (NaN, Not a Number). In actual development, we often need to use them at the same time to ensure the validity of the values ​​to avoid logical errors or operational exceptions.

Basic Function Introduction

  • is_finite(float $num): bool
    Returns true if $num is a finite value, otherwise returns false . This can be used to exclude positive and negative infinity (INF and -INF).

  • is_nan(float $num): bool
    Return true If $num is NaN (Not a Number), it is usually the result of illegal mathematical operations.

Why use it in combination?

When processing external data (such as user input, file reading, or API return), we cannot preset that the data must be a legal floating point number. A typical scenario is to perform mathematical operations such as division, square root, logarithm, etc., and the result may be NaN or infinity. For example:

 $result = log(0); // return -INF

or:

 $result = acos(2); // Out of the domain,return NAN

Using is_finite() alone does not exclude NaN values ​​and vice versa. Therefore, two functions must be combined to more comprehensively determine whether a value is a "normal" value.

Best practices for joint use

To more efficiently determine whether a value is "normal", you can write a general function as shown below:

 function is_valid_number($value) {
    return is_finite($value) && !is_nan($value);
}

Using this function, we can easily call it in various data processing:

 $data = [
    log(0),
    3.1415,
    acos(2),
    1.0e309
];

foreach ($data as $value) {
    if (is_valid_number($value)) {
        echo "Valid value: $value\n";
    } else {
        echo "Invalid value: $value\n";
    }
}

The output will be:

 Invalid value: -INF
Valid value: 3.1415
Invalid value: NAN
Invalid value: INF

Combined with application scenarios

For example, in a data processing pipeline, you might get a numeric array from <code> https://gitbox.net/api/data </code> for statistical analysis. Before you perform any math operations, use is_valid_number() to clean the data to avoid unexpected NaN or infinite contamination subsequent results.

 $json = file_get_contents('https://gitbox.net/api/data');
$data = json_decode($json, true);

$cleanData = array_filter($data, 'is_valid_number');
$average = array_sum($cleanData) / count($cleanData);

echo "average value: $average";

This not only makes the code cleaner, but also minimizes logical vulnerabilities.

summary

Using is_finite() and is_nan() in combination is an important means to verify the validity of numerical values. By encapsulating it as a general function and using it uniformly in input verification, data preprocessing, API reception and other links, the robustness and maintainability of PHP applications can be greatly improved. In actual development, this defensive programming technique often avoids many difficult-to-discover boundary errors.