当我使用类型为
据我的研究,MISRA是正确的,因为“%f”需要
以下是需要代码示例的部分:
float
的参数并使用“%f”说明符进行snprintf
时,我遇到了MISRA类型错误。据我的研究,MISRA是正确的,因为“%f”需要
double
类型。
是否有浮点说明符或修饰符可以使用float
类型参数而不是double
类型?
我正在开发嵌入式系统,并不想将32位浮点数转换为64位double
,只是为了满足snprintf
函数。该代码打印到调试/控制台端口,这是唯一进行转换的地方。以下是需要代码示例的部分:
// This section is for those C++ purists and it fulfills the C++ tag.
#if __cplusplus
#include <cstdio>
#else
#include <stdio.h>
#endif
#define BUFFER_SIZE (128U)
int main(void)
{
char buffer[BUFFER_SIZE];
float my_float = 1.234F;
// The compiler will promote the single precision "my_float"
// to double precision before passing to snprintf.
(void)snprintf(buffer, BUFFER_SIZE, "%10.4f", my_float);
puts(buffer);
return 0;
}
我在Stack Overflow和网络上的所有研究都是关于打印浮点数值的,而不是关于哪些说明符需要float
参数以避免发生到double
的提升。
我正在使用IAR嵌入式Workbench编译器用于ARM7TDMI处理器。