英文:
gdb set value at address of a float
问题
I am learning about floating point number representation and I have the following C++ code:
#include <float.h>
int main()
{
unsigned int a = 8;
float f = 1;
float fmax = FLT_MAX;
float n = 3.402823669e+38;
return 0;
}
When I print the value of f
as binary in gdb like x/tw &f
I get:
0 01111111 00000000000000000000000 (1 sign bit, 8 exp bits, 23 mantissa bits).
In hexadecimal it is 0x3f800000.
Now I want to see which number I will get if I make the exponent as large as possible.
So I do set *(&f) = 0x7f800000
.
However, instead of getting the expected
0 11111111 00000000000000000000000,
I get something completely arbitrary like
0 10011101 11111110000000000000000,
which is 0x4eff0000, which is not the value I set.
This happens also if the set result doesn't exceed FLT_MAX.
Changing unsigned int a
in this way works fine.
What could be a possible explanation to this behavior?
英文:
I am learning about floating point number representation and I have the following C++ code:
#include <float.h>
int main()
{
unsigned int a = 8;
float f = 1;
float fmax = FLT_MAX;
float n = 3.402823669e+38;
return 0;
}
When I print the value of f
as binary in gdb like x/tw &f
I get:
0 01111111 00000000000000000000000 (1 sign bit, 8 exp bits, 23 mantissa bits).
In hexadecimal it is 0x3f800000.
Now I want to see which number I will get if I make the exponent as large as possible.
So I do set *(&f) = 0x7f800000
.
However, instead of getting the expected
0 11111111 00000000000000000000000,
I get something completely arbitrary like
0 10011101 11111110000000000000000,
which is 0x4eff0000, which is not the value I set.
This happens also if the set result doesn't exceed FLT_MAX.
Changing unsigned int a
in this way works fine.
What could be a possible explanation to this behavior?
答案1
得分: 1
这种行为的可能解释是什么?
OP将2139095040.0f
的值赋给了f
。
*(&f) = 0x7f800000
就像*(&f) = 2139095040.0f
。
2139095040.0f
以16进制模式0x4eff0000编码为float
。
毫不奇怪。
英文:
> What could be a possible explanation to this behavior?
OP assigned the value of 2139095040.0f
to f
.
*(&f) = 0x7f800000
is like *(&f) = 2139095040.0f
.
2139095040.0f
is encoded in a float
with hex pattern 0x4eff0000.
No surprise.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论