简体   繁体   English

如何从整数引用浮点型指针时知道其值? 例如:float * f =(float *)someInteger

[英]how to tell the value of a float pointer when it has been referenced from an integer? ex: float *f= (float *)someInteger

I cannot figure out how to convert the value of a referenced float pointer when it is referenced from an integer casted into a float pointer. 我无法弄清楚如何将引用的值转换float当从铸造到一个整数引用指针float指针。 I'm sorry if I'm wording this incorrectly. 很抱歉,我的措词不正确。 Here is an example of what I mean: 这是我的意思的示例:

#include <stdio.h>

main() {
    int i;
    float *f;

    i = 1092616192;

    f = (float *)&i;

    printf("i is %d and f is %f\n", i, *f);
}

the output for f is 10 . f的输出为10 How did I get that result? 我怎么得到那个结果的?

Normally, the value of 1092616192 in hexadecimal is 0x41200000. 通常,以十六进制表示的1092616192的值为0x41200000。

In floating-point, that will give you: 在浮点运算中,这将为您提供:

sign = positive (0b)
exponent = 130, 2^3 (10000010b)
significand = 2097152, 1.25 (01000000000000000000000b)

2^3*1.25 = 8 *1.25 = 10 2 ^ 3 * 1.25 = 8 * 1.25 = 10

To explain the exponent part uses an offset encoding, so you have to subtract 127 from it to get the real value. 为了解释指数部分使用偏移编码,因此必须从中减去127才能获得实数值。 130 - 127 = 3. And since this is a binary encoding, we use 2 as the base. 130-127 =3。由于这是二进制编码,因此我们以2为基数。 2 ^ 3 = 8. 2 ^ 3 = 8。

To explain the significand part, you start with an invisible 'whole' value of 1. the uppermost (leftmost) bit is half of that, 0.5. 为了解释有效部分,您从一个不可见的“整个”值1开始。最高(最左边)位是该值的一半,即0.5。 The next bit is half of 0.5, 0.25. 下一位是0.5、0.25的一半。 Because only the 0.25 bit and the default '1' bit is set, the significand represents 1 + 0.25 = 1.25. 因为仅设置了0.25位和默认的“ 1”位,所以有效位数表示1 + 0.25 = 1.25。

What you are trying to do is called type-punning. 您尝试做的事情称为类型调整。 It should be done via a union , or using memcpy() and is only meaningful on an architecture where sizeof(int) == sizeof(float) without padding bits. 应该通过union或使用memcpy()来完成,并且仅在sizeof(int) == sizeof(float)没有填充位的体系结构上有意义。 The result is highly dependent on the architecture: byte ordering and floating point representation will affect the reinterpreted value. 结果高度依赖于体系结构:字节顺序和浮点表示形式将影响重新解释的值。 The presence of padding bits would invoke undefined behavior as the representation of float 15.0 could be a trap value for type int . 填充位的存在将引发未定义的行为,因为float 15.0的表示形式可能是int类型的陷阱值。

Here is how you get the number corresponding to 15.0 : 这是如何获得与15.0对应的数字的:

#include <stdio.h>

int main(void) {
    union {
        float f;
        int i;
        unsigned int u;
    } u;

    u.f = 15;

    printf("re-interpreting the bits of float %.1f as int gives %d (%#x in hex)\n",
           u.f, u.i, u.u);

    return 0;
}

output on an Intel PC: 在Intel PC上的输出:

re-interpreting the bits of float 15.0 as int gives 1097859072 (0x41700000 in hex)

You are trying to predict the consequence of an undefined activity - it depends on a lot of random things, and on the hardware and OS you are using. 您正在尝试预测未定义活动的后果-它取决于许多随机因素以及所使用的硬件和操作系统。

Basically, what you are doing is throwing a glass against the wall and getting a certain shard. 基本上,您正在做的是将玻璃杯扔到墙上,并得到一定的碎片。 Now you are asking how to get a differently formed shard. 现在,您正在询问如何获取其他形式的分片。 well, you need to throw the glass differently against the wall... 好吧,您需要将玻璃杯以不同的方式靠在墙上...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM