固定宽度浮动类型在哪里?

发布于 2025-02-02 19:00:47 字数 833 浏览 3 评论 0原文

C标准固定宽度浮点类型是什么?它们在哪里定义?

Misra-C:2004,规则6.3:
typedefs指示大小和签名的应代替基本数值类型。

Misra-C:2004,规则6.3 ISO(POSIX)TYPEDEFS是:

typedef          char   char_t;
typedef signed   char   int8_t;
typedef signed   short  int16_t;  
typedef signed   int    int32_t;
typedef signed   long   int64_t;
typedef unsigned char   uint8_t;
typedef unsigned short  uint16_t;
typedef unsigned int    uint32_t;
typedef unsigned long   uint64_t;
typedef          float  float32_t;
typedef          double float64_t;
typedef long     double float128_t;

在我的公司我们使用C-99。
IAR电子工作台,第8.4版

我们在Misra-C:2004,因为他们投票不升级Misra,这将需要创建验证测试协议并运行协议。

平台是运行Microcos操作系统的ARM Cortex M。

这是详细的问题:

  1. 固定宽度浮点类型的类型是什么?
  2. 什么包括文件内部(或由编译器定义为默认值)?

What are the C standard fixed width floating point types and where are they defined?

From MISRA-C:2004, Rule 6.3:
typedefs that indicate size and signedness should be used in place of the basic numerical types.

The MISRA-C:2004, Rule 6.3 quotes that ISO (POSIX) typedefs are:

typedef          char   char_t;
typedef signed   char   int8_t;
typedef signed   short  int16_t;  
typedef signed   int    int32_t;
typedef signed   long   int64_t;
typedef unsigned char   uint8_t;
typedef unsigned short  uint16_t;
typedef unsigned int    uint32_t;
typedef unsigned long   uint64_t;
typedef          float  float32_t;
typedef          double float64_t;
typedef long     double float128_t;

At my company we are using C-99.
IAR Electronic Workbench, version 8.4

We are at MISRA-C:2004 because they voted not to upgrade MISRA, which would require create a validation test protocol and running the protocol.

Platform is ARM Cortex M running MicroCOS operating system.

Here are the detailed questions:

  1. What are the typedefs for the fixed width floating point types?
  2. What include file are they inside (or are they defined as default by the compiler)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

握住你手 2025-02-09 19:00:47

c标准固定宽度浮点类型未定义

  • c浮点(FP)目标旨在包含变化和许多实现。

  • MISRA FP目标是限制多样性。

固定尺寸的FP类型不会导致均匀的位编码或其他一致的FP属性。它们在C中的实用性有限 - 因此它们不属于C标准或库的一部分。


fall-back

代码可以在下面使用,_STATIC_ASSERT(自C11)或C99 替代

typedef      float  float32_t;
typedef      double float64_t;
typedef long double float128_t;

_Static_assert(sizeof(float32_t)*CHAR_BIT == 32, "float 32");
_Static_assert(sizeof(float64_t)*CHAR_BIT == 64, "float 64");
_Static_assert(sizeof(float128_t)*CHAR_BIT == 128, "float 128");

进一步的注释

兼容C可能没有所有32、64、128位FP类型,因此无法定义所有float32_t,float64_t,float128_t

2种不同的C实现可能具有32位FP类型,但编码不同。比较 float32

2不同的C实现可能具有32位FP类型,即使他们的整数Endians同意,但相同的编码也可能具有不同的Endians。

规则6.3(咨询):应用指示尺寸和签名的类型和签名使用基本的数值类型。:该目标“有助于阐明存储的大小”而不是其他。

规则1.5(咨询):浮点数应符合定义的浮点点标准。特别难以实现。即使实现使用相同的fp编码与 ieee 754 ,C允许操作足够定义的操作定义了与IEE 754不同的行为。

理想情况下,在C中,符合IEEE 754的实现定义了__ stdc_iec_559 __ __。然而,证明维持的合格足够挑战,以至于实现可以放弃定义__ stdc_iec_559 __,因为它可能仅为99.999%。

C standard fixed width floating point types are not defined

  • C floating point (FP) goals are designed to embrace variations and many implementations.

  • MISRA FP goals are to restrict variety.

Fixed size FP types do not result in uniform bit encoding nor other consistent FP properties. They have limited usefulness in C - hence they are not part of the C standard or library.


Fall-back

Code could use below and a _Static_assert (since C11) or a C99 substitute.

typedef      float  float32_t;
typedef      double float64_t;
typedef long double float128_t;

_Static_assert(sizeof(float32_t)*CHAR_BIT == 32, "float 32");
_Static_assert(sizeof(float64_t)*CHAR_BIT == 64, "float 64");
_Static_assert(sizeof(float128_t)*CHAR_BIT == 128, "float 128");

Further notes

Compliant C may not have all 32, 64, 128 bit FP types, thus unable to define all float32_t, float64_t, float128_t.

2 different Compliant C implementations may have a 32-bit FP types, but different encoding. Compare float32 vs. CCSI resulting in different range, precision and sub-normal support.

2 different Compliant C implementations may have a 32-bit FP types with the same encoding, but different endians, even if their integer endians agree.

Rule 6.3 (advisory): typedefs that indicate size and signedness should be used in place of the basic numerical types.: that goal "helps to clarify the size of the storage" and not much else.

Rule 1.5 (advisory): Floating-point implementations should comply with a defined floating-point standard. is particularly difficult to achieve. Even if an implementation uses the same FP encoding as IEEE 754, C allows the operations enough implementation defined behavior to differ from IEE 754.

Ideally, in C, an implementation that conforms to IEEE 754 defines __STDC_IEC_559__. Yet proving and maintaining conformity is challenging enough that an implementation may forego defining __STDC_IEC_559__ as it may only be 99.999% conforming.

一个人练习一个人 2025-02-09 19:00:47

C标准固定宽度浮点类型是什么?在哪里定义?

没有。


固定宽度FP类型可能不足以实现OP的更高级别(未说)目标。

如果存在固定宽度的浮点,则可能只能构成固定宽度。如果不是肯定会导致固定范围,精度,编码等

。如果要与之相处,请考虑使用一系列#if测试常见的FP编码特征,然后简单地使用float,double double,long double

What are the C standard fixed width floating point types and where are they defined?

There are none.


A fixed width FP type is likely insufficient for OP's higher level (unstated) goal.

If a fixed width floating point existed, it may only make for a fixed width. If would not certainly make for a fixed range, precision, encoding, etc.

If a fixed width floating point also defined encoding, portability is reduced. If will to live with that, consider instead a series of #if tests for common FP encoding characteristics and simply use float, double, long double.

黑寡妇 2025-02-09 19:00:47

显然(非官方来源),Misra-C:2004年第6.3条说:

…例如,建议使用以下图所示的ISO(POSIX)Typedefs,并用于本文档中的所有基本数值和字符类型。对于32位整数机,这些如下:…

这是不好的措辞。这并不意味着POSIX或任何ISO标准提供这些typedef声明。这意味着与Misra符合的软件, IE ,您正在编写的软件本身定义这些typedef名称并使用它们。

如果这不是规则的预期含义,则该规则是错误的,暗示这些类型是由POSIX指定的,因为float64_t未出现在Posix 2008规范中。 (我没有检查较早的版本;我假设它是否出现在早期版本中,至少会在Posix 2008版本中提及它。)

Apparently (unofficial source), MISRA-C:2004 rule 6.3 says:

… For example, the ISO (POSIX) typedefs as shown below are recommended and are used for all basic numerical and character types in this document. For a 32-bit integer machine, these are as follows:…

This is bad phrasing. It does not mean that POSIX or any ISO standard provides these typedef declarations. It means the software conforming to MISRA, i.e., the software you are writing, should itself define these typedef names and use them.

If that is not the intended meaning of the rule, then the rule is wrong to imply these types are specified by POSIX, because float64_t does not appear in the POSIX 2008 specification. (I did not check earlier versions; I am assuming if it appeared in earlier versions, there would at least be mention of it in the POSIX 2008 version.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文