Memory comparison of float-point values
Object representation of floating-point values can be different (same) for equal (not equal) floating-point values
Description
This defect occurs when
you compare the object representation of floating-point values or the object
representation of structures containing floating-point members. When you use the
functions memcmp
, bcmp
, or
wmemcmp
to perform the bit pattern comparison, the defect is
raised.
Risk
The object representation of floating-point values uses specific bit patterns to
encode those values. Floating-point values that are equal, for instance
-0.0
and 0.0
in the IEC 60559 standard,
can have different bit patterns in their object representation. Similarly,
floating-point values that are not equal can have the same bit pattern in their
object representation.
Fix
When you compare structures containing floating-point members, compare the structure members individually.
To compare two floating-point values, use the ==
or
!=
operators. If you follow a standard that discourages the
use of these operators, such as MISRA™, ensure that the difference between the floating-point values is
within an acceptable range.
Examples
Result Information
Group: Programming |
Language: C | C++ |
Default: Off |
Command-Line Syntax:
MEMCMP_FLOAT |
Impact: Low |
Version History
Introduced in R2018a
See Also
Find defects (-checkers)
| Floating point comparison with equality
operators
| Memory comparison of padding data
| Memory comparison of strings
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)