Main Content

Invalid use of standard library floating point routine

Wrong arguments to standard library function

Description

This defect occurs when you use invalid arguments with a floating point function from the standard library and the standard name space. This defect picks up:

  • Rounding and absolute value functions (ceil(), fabs(), floor(), fmod(), and so on)

  • Fractions and division functions (fmod(), modf()

  • Exponents and log functions (frexp(), ldexp(), sqrt(), pow(), exp(), log(), log10(), and so on)

  • Trigonometry functions (cos(), sin(), tan(), acos(), asin(), atan(), atan2(), and so on)

Risk

Domain errors on standard library floating point functions result in implementation-defined values. If you use the function return value in subsequent computations, you can see unexpected results.

Fix

The fix depends on the root cause of the defect. Often the result details (or source code tooltips in Polyspace as You Code) show a sequence of events that led to the defect. You can implement the fix on any event in the sequence. If the result details do not show this event history, you can search for previous references of variables relevant to the defect using right-click options in the source code and find related events. See also Interpret Bug Finder Results in Polyspace Desktop User Interface or Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access).

It is a good practice to handle for domain errors before using a standard library floating point function. For instance, before calling the acos function, check if the argument is in [-1.0, 1.0] and handle the error.

See examples of fixes below.

If you do not want to fix the issue, add comments to your result or code to avoid another review. See:

By default, a Bug Finder analysis does not recognize infinities and NaNs. Operations that results in infinities and NaNs might be flagged as defects. To handle infinities and NaN values in your code, use the option Consider non finite floats (-allow-non-finite-floats).

Extend Checker

Extend this checker to check for defects caused by specific values and invalid use of functions from a custom library. For instance:

Examples

expand all

#include <math.h>

double arccosine(void) {
    double degree = 5.0;
    return acos(degree);
}

The input value to acos must be in the interval [-1,1]. This input argument, degree, is outside this range.

Correction — Change Input Argument

One possible correction is to change the input value to fit the specified range. In this example, change the input value from degrees to radians to fix this defect.

#include <math.h>

double arccosine(void) {
    double degree = 5.0;
    double radian = degree * 3.14159 / 180.;
    return acos(radian);
}
#include <cmath>

void InvalidUse(double& val1, double& val2){
  int in = -5;
  int ratio = 0;
  //...
  val1 = std::sqrt(in);
  val2 = std::log(ratio);
}

The input value to std::sqrt() must be nonnegative. Polyspace® flags the use of negative input argument in. The input value to std::log must be greater than zero. Polyspace flags calling std::log by using ratio.

Correction — Refactor Code

One possible correction is to check the logic and refactor your code. For instance, instead of taking the square root of -5, perhaps val1 is indented to be the negative square root of 5. The function might be defining ratio incorrectly as well.

#include <cmath>
int getRatio(void);
void ValidUse(double& val1, double& val2){
  int in = 5;
  int ratio = getRatio();
  //...
  val1 = -1* std::sqrt(in);
  val2 = std::log(ratio);
}

Result Information

Group: Numerical
Language: C | C++
Default: On
Command-Line Syntax: FLOAT_STD_LIB
Impact: High

Version History

Introduced in R2013b

expand all