主要内容

MISRA C++:2008 Rule 3-9-3

The underlying bit representations of floating-point values shall not be used

描述

规则定义

The underlying bit representations of floating-point values shall not be used. 1

理由

浮点值的底层位表示在不同编译器之间有所不同。如果您直接使用浮点值的底层表示,则您的程序不可跨实现移植。

Polyspace 实现

规则检查项会标记从指向浮点类型的指针到指向整数类型的指针的转换,以及反向转换。

故障排除

如果您预期会出现违规,而 Polyspace® 未报告该违规,请参阅诊断为何编码规范违规未按预期显示

示例

全部展开

float fabs2(float f) {
    unsigned int* ptr = reinterpret_cast <unsigned int*> (&f); //Noncompliant
    *(ptr + 3) &= 0x7f;
    return f;
}

在此示例中,reinterpret_cast 试图将一个浮点值转换为整数,并访问该浮点值的底层位表示。

检查信息

组:基本概念
类别:必需

版本历史记录

在 R2013b 中推出


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.