i*h
2 楼
需要用一个float存整数:
int i = 0x24240102;
float f = static_cast(i);
std::cout << std::hex << static_cast(f) << std::endl;
结果出来是:
24240100
尾巴上那个02变成00了.
为什么?
int i = 0x24240102;
float f = static_cast
std::cout << std::hex << static_cast
结果出来是:
24240100
尾巴上那个02变成00了.
为什么?
i*h
5 楼
被鄙视鸟.
这种东西, 平时用着不出错就用了, 没深入学习过.
理解中就是把这个当memcpy使的, 看来是不对的
整数是0x24240102, 就是
(24) 0010 0100 (24) 0010 0100 (01) 0000 0001 (01) 0000 0010
浮点表示是 1 bit sign, 8 bit exponent, 23 bit significant
上面就变成:
(0) (010 0100 0) (010 0100 0000 0001 0000 0010)
呣... 还是看不出为什么最后的 0010 会变成 0000
求解释
【在 t****t 的大作中提到】
: 你这么老的鸟问这么幼齿的问题不应该啊.
这种东西, 平时用着不出错就用了, 没深入学习过.
理解中就是把这个当memcpy使的, 看来是不对的
整数是0x24240102, 就是
(24) 0010 0100 (24) 0010 0100 (01) 0000 0001 (01) 0000 0010
浮点表示是 1 bit sign, 8 bit exponent, 23 bit significant
上面就变成:
(0) (010 0100 0) (010 0100 0000 0001 0000 0010)
呣... 还是看不出为什么最后的 0010 会变成 0000
求解释
【在 t****t 的大作中提到】
: 你这么老的鸟问这么幼齿的问题不应该啊.
t*t
6 楼
no, try to google static_cast.
【在 i***h 的大作中提到】
: 被鄙视鸟.
: 这种东西, 平时用着不出错就用了, 没深入学习过.
: 理解中就是把这个当memcpy使的, 看来是不对的
: 整数是0x24240102, 就是
: (24) 0010 0100 (24) 0010 0100 (01) 0000 0001 (01) 0000 0010
: 浮点表示是 1 bit sign, 8 bit exponent, 23 bit significant
: 上面就变成:
: (0) (010 0100 0) (010 0100 0000 0001 0000 0010)
: 呣... 还是看不出为什么最后的 0010 会变成 0000
: 求解释
【在 i***h 的大作中提到】
: 被鄙视鸟.
: 这种东西, 平时用着不出错就用了, 没深入学习过.
: 理解中就是把这个当memcpy使的, 看来是不对的
: 整数是0x24240102, 就是
: (24) 0010 0100 (24) 0010 0100 (01) 0000 0001 (01) 0000 0010
: 浮点表示是 1 bit sign, 8 bit exponent, 23 bit significant
: 上面就变成:
: (0) (010 0100 0) (010 0100 0000 0001 0000 0010)
: 呣... 还是看不出为什么最后的 0010 会变成 0000
: 求解释
F5
7 楼
10
this is from head to toe?
【在 i***h 的大作中提到】
: 被鄙视鸟.
: 这种东西, 平时用着不出错就用了, 没深入学习过.
: 理解中就是把这个当memcpy使的, 看来是不对的
: 整数是0x24240102, 就是
: (24) 0010 0100 (24) 0010 0100 (01) 0000 0001 (01) 0000 0010
: 浮点表示是 1 bit sign, 8 bit exponent, 23 bit significant
: 上面就变成:
: (0) (010 0100 0) (010 0100 0000 0001 0000 0010)
: 呣... 还是看不出为什么最后的 0010 会变成 0000
: 求解释
S*I
8 楼
If you use double type intead of float, you can get what you want (still not
safe though).
【在 i***h 的大作中提到】
: 被鄙视鸟.
: 这种东西, 平时用着不出错就用了, 没深入学习过.
: 理解中就是把这个当memcpy使的, 看来是不对的
: 整数是0x24240102, 就是
: (24) 0010 0100 (24) 0010 0100 (01) 0000 0001 (01) 0000 0010
: 浮点表示是 1 bit sign, 8 bit exponent, 23 bit significant
: 上面就变成:
: (0) (010 0100 0) (010 0100 0000 0001 0000 0010)
: 呣... 还是看不出为什么最后的 0010 会变成 0000
: 求解释
safe though).
【在 i***h 的大作中提到】
: 被鄙视鸟.
: 这种东西, 平时用着不出错就用了, 没深入学习过.
: 理解中就是把这个当memcpy使的, 看来是不对的
: 整数是0x24240102, 就是
: (24) 0010 0100 (24) 0010 0100 (01) 0000 0001 (01) 0000 0010
: 浮点表示是 1 bit sign, 8 bit exponent, 23 bit significant
: 上面就变成:
: (0) (010 0100 0) (010 0100 0000 0001 0000 0010)
: 呣... 还是看不出为什么最后的 0010 会变成 0000
: 求解释
t*t
12 楼
union {
int i;
float f;
};
reinterpret_cast<> is to convert between pointers and integral types. no
float allowed.
using float* to access int* (no matter cast with reinterpret_cast or (type)
cast) is undefined, don't try that. it is subtle and not easy to discover,
but will blow up eventually.
【在 i***h 的大作中提到】
: 也就是说static_cast 读了整数
: 然后试图把数字用浮点表达, 最后几位因为精度不够被舍弃了
: 除了用指针或者memcpy(), 还有别的办法把int 的内容直接理解成浮点么?
: float f = reinterpret_cast(i);
: 编译不通过
int i;
float f;
};
reinterpret_cast<> is to convert between pointers and integral types. no
float allowed.
using float* to access int* (no matter cast with reinterpret_cast or (type)
cast) is undefined, don't try that. it is subtle and not easy to discover,
but will blow up eventually.
【在 i***h 的大作中提到】
: 也就是说static_cast 读了整数
: 然后试图把数字用浮点表达, 最后几位因为精度不够被舍弃了
: 除了用指针或者memcpy(), 还有别的办法把int 的内容直接理解成浮点么?
: float f = reinterpret_cast
: 编译不通过
i*h
13 楼
没办法, legacy code, 改API很麻烦
要用已有的data field存新的东西
我也不想玩这种把戏
)
【在 t****t 的大作中提到】
: union {
: int i;
: float f;
: };
: reinterpret_cast<> is to convert between pointers and integral types. no
: float allowed.
: using float* to access int* (no matter cast with reinterpret_cast or (type)
: cast) is undefined, don't try that. it is subtle and not easy to discover,
: but will blow up eventually.
要用已有的data field存新的东西
我也不想玩这种把戏
)
【在 t****t 的大作中提到】
: union {
: int i;
: float f;
: };
: reinterpret_cast<> is to convert between pointers and integral types. no
: float allowed.
: using float* to access int* (no matter cast with reinterpret_cast or (type)
: cast) is undefined, don't try that. it is subtle and not easy to discover,
: but will blow up eventually.
a*n
17 楼
int i = 0x24240102;
float f = *reinterpret_cast(&i);
std::cout << std::hex << *reinterpret_cast(&f) << std::endl;
float f = *reinterpret_cast
std::cout << std::hex << *reinterpret_cast
h*f
19 楼
casting to void* to trick the compiler not to do anything:
int i = 0x24240102;
float f = *(float*)(void*)(&i);
std::cout << std::hex << *(int*)(void*)&f << std::endl;
int i = 0x24240102;
float f = *(float*)(void*)(&i);
std::cout << std::hex << *(int*)(void*)&f << std::endl;
h*f
21 楼
Really?
I thought casting to void* always made it point to the first byte -- defined
behavior, and then casting back also made it point to the first byte --
defined behavior as well.
No?
I thought casting to void* always made it point to the first byte -- defined
behavior, and then casting back also made it point to the first byte --
defined behavior as well.
No?
t*t
22 楼
casting itself is defined. but using casted pointer to access a different
type of value is undefined, unless certain conditions are met. specifically,
using floating pointer to access integer value, or vice versa, are both
undefined.
defined
【在 h*****f 的大作中提到】
: Really?
: I thought casting to void* always made it point to the first byte -- defined
: behavior, and then casting back also made it point to the first byte --
: defined behavior as well.
: No?
type of value is undefined, unless certain conditions are met. specifically,
using floating pointer to access integer value, or vice versa, are both
undefined.
defined
【在 h*****f 的大作中提到】
: Really?
: I thought casting to void* always made it point to the first byte -- defined
: behavior, and then casting back also made it point to the first byte --
: defined behavior as well.
: No?
相关阅读
BERT 到底行不行请教高手:python 的machine learning 算法 production code 在有些需要?请问有简单例子吗?[bssd]我说个笑话Full story of GO's three level schedulerhow to find old articles about 12306 and TeachWei's code?github把repository转让后,Eclipse里面如何改动请教大牛一个很老的VISUAL STUDIO6.0 的问题[bssd]王寅当年的老板是steve Yegee吧?有没有好的python进阶培训班或者网络课国内的参考资料请教一个离散数学问题The American AI Initiative贾扬清被国人“同胞”捅刀子,被迫离开fb (转载)调度问题,说个笑话给大家听【bssd】以后在编程版讨论金瓶梅啥的,是不是应该被办掉?机器学习准入门槛降低,机器学习工程师职位或将消失zz现在的主流语言本质上还是Algol那一套google cloud卖什么公司能翻盘?iphone 4 , siri想听听大家意见:e commerce UI 框架 Vue react ?