Main Content

Invalid operation on floats

Result of floating-point operation is NaN for non-NaN operands

Description

This check determines if the result of a floating-point operation is NaN. The check is performed only if you enable a verification mode that incorporates NaNs and specify that the verification must highlight operations that result in NaN.

If you specify that the verification must produce a warning for NaN, the check is:

  • Red, if the operation produces NaN on all execution paths that the software considers, and the operands are not NaN.

  • Orange, if the operation produces NaN on some of the execution paths when the operands are not NaN.

  • Green, if the operation does not produce NaN unless the operands are NaN.

If you specify that the verification must forbid NaN, the check color depends on the result of the operation only. The color does not depend on the operands.

The check also highlights conversions from floating-point variables to integers where the floating-point variable can be NaN. In this case, the check is always performed when you incorporate NaNs in the verification and does not allow NaNs as input to the conversion.

To enable this verification mode, use these options:

Examples

expand all

Results in forbid mode:

double func(void) {
    double x=1.0/0.0;
    double y=x-x;
    return y;
}
In this example, both the operands of the - operation are not NaN but the result is NaN. The Invalid operation on floats check on the - operation is red. In the forbid mode, the verification stops after the red check. For instance, a Non-initialized local variable check does not appear on y in the return statement.

Results in warn-first mode:

double func(void) {
    double x=1.0/0.0;
    double y=x-x;
    return y;
}
In this example, both the operands of the - operation are not NaN but the result is NaN. The Invalid operation on floats check on the - operation is red. The red checks in warn-first mode are different from red checks for other check types. The verification does not stop after the red check. For instance, a green Non-initialized local variable check appears on y in the return statement. If you place your cursor on y in the verification result, you see that it has the value NaN.

Results in forbid mode:

double func(double arg1, double arg2) {
    double ret=arg1-arg2;
    return ret;
}
In this example, the values of arg1 and arg2 are unknown to the verification. The verification assumes that arg1 and arg2 can be both infinite, for instance, and the result of arg1-arg2 can be NaN. In the forbid mode, following the check, the verification terminates the execution path that results in NaN. If you place your cursor on ret in the return statement, it does not have the value NaN.

Results in warn-first mode:

double func(double arg1, double arg2) {
    double ret=arg1-arg2;
    return ret;
}
In this example, the values of arg1 and arg2 are unknown to the verification. The verification assumes that arg1 and arg2 can be both infinite, for instance, and the result of arg1-arg2 can be NaN. The orange checks in warn-first mode are different from orange checks for other check types. Following the check, the verification does not terminate the execution path that results in NaN. If you place your cursor on ret in the return statement, it continues to have the value NaN along with other possible values.

double func(double arg1, double arg2) {
    double z=arg1-arg2;
    return z;
}

void caller() {
    double x=1.0/0.0;
    double y=x-x;
    func(x,x);
    func(y,y);
}

In this example, in func, the result of the - operation is always NaN but the Invalid operation on floats check is orange instead of red.

  • In the first call to func, both the operands arg1 and arg2 are not NaN, but the result is NaN. So, the check is red.

  • In the second call to func, both the operands arg1 and arg2 are NaN, and therefore the result is NaN. So, the check is green, indicating that the result is not NaN unless the operands are NaN.

Combining the colors for the two calls to func, the check is orange.

In the example, the option -check-nan warn-first was used.

void func() {
    double x= 1.0/0.0;
    double y= x-x;
    int z = y;
}

In this example, the Invalid operation on floats check detects the assignment of NaN to an integer variable z.

The check is enabled if you specify that non-finite floats must be considered in the verification. The check blocks further verification on the same execution path irrespective of whether you allow, forbid or ask for warnings on non-finite floats.

Result Information

Group: Numerical
Language: C | C++
Acronym: INVALID_FLOAT_OP

Version History

Introduced in R2016a