函数后浮点值变化 Return
Floating Point Value Changes After Function Return
我发现我的浮点值在返回后发生了变化。
我在返回之前输出浮点数的十六进制值,然后在返回之后输出。我希望十六进制输出是相同的,但它们已经改变了。您能否解释为什么会发生这种情况以及如何防止这种情况发生?
谢谢!
#include <stdio.h>
typedef int uint32;
typedef unsigned char uint8;
static const float CONST_FLOAT = 150.36e6;
uint32 foo( uint8 param ) {
return 1;
}
float get_float( uint8 param )
{
float fVal = 0.0;
fVal = (foo( param )*1.0) / ( 60.0 * CONST_FLOAT );
printf("fVal = 0x%X\n", fVal);
printf("fVal = %f\n", fVal);
return fVal;
}
int main(void) {
float fTest;
fTest = get_float(1);
printf("fTest = 0x%X\n",fTest);
printf("fTest = %f\n",fTest);
return;
}
输出:
fVal = 0x6A96C728
fVal = 0.000000
fTest = 0x9AAF9000
fTest = 0.000000
我希望以十六进制格式显示的 fVal 与十六进制格式的 fTest 相同,但结果却不同,这是为什么?
谢谢!
您不能打印带有 "%x"
说明符的 float
值。这是未定义的行为,您的程序显然会相应地表现。
这是来自 C11 草稿 1570,
7.21.6.1 The fprintf
function
The conversion specifiers and their meanings are:
o,u,x,X
The unsigned int argument is converted to unsigned octal (o), unsigned
decimal (u), or unsigned hexadecimal notation (x or X) in the style dddd; the
letters abcdef are used for x conversion and the letters ABCDEF for X
conversion. The precision specifies the minimum number of digits to appear;
if the value being converted can be represented in fewer digits, it is expanded|
with leading zeros. The default precision is 1. The result of converting a
zero value with a precision of zero is no characters.
?
- If a conversion specification is invalid, the behavior is undefined. 282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
我发现我的浮点值在返回后发生了变化。 我在返回之前输出浮点数的十六进制值,然后在返回之后输出。我希望十六进制输出是相同的,但它们已经改变了。您能否解释为什么会发生这种情况以及如何防止这种情况发生?
谢谢!
#include <stdio.h>
typedef int uint32;
typedef unsigned char uint8;
static const float CONST_FLOAT = 150.36e6;
uint32 foo( uint8 param ) {
return 1;
}
float get_float( uint8 param )
{
float fVal = 0.0;
fVal = (foo( param )*1.0) / ( 60.0 * CONST_FLOAT );
printf("fVal = 0x%X\n", fVal);
printf("fVal = %f\n", fVal);
return fVal;
}
int main(void) {
float fTest;
fTest = get_float(1);
printf("fTest = 0x%X\n",fTest);
printf("fTest = %f\n",fTest);
return;
}
输出: fVal = 0x6A96C728 fVal = 0.000000
fTest = 0x9AAF9000 fTest = 0.000000
我希望以十六进制格式显示的 fVal 与十六进制格式的 fTest 相同,但结果却不同,这是为什么?
谢谢!
您不能打印带有 "%x"
说明符的 float
值。这是未定义的行为,您的程序显然会相应地表现。
这是来自 C11 草稿 1570,
7.21.6.1 The
fprintf
function
The conversion specifiers and their meanings are:
o,u,x,X
The unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal notation (x or X) in the style dddd; the letters abcdef are used for x conversion and the letters ABCDEF for X conversion. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it is expanded| with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is no characters. ?
- If a conversion specification is invalid, the behavior is undefined. 282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.