强制转换 float f=1.0;(int &)f为多少? 如果f=0.0,(int &)f为0 为什么?

chenhuixiaogou 2006-11-14 03:44:20
强制转换 float f=1.0;(int &)f为多少?
如果f=0.0,(int &)f为0 为什么?
...全文
647 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
haohuilai 2006-11-14
  • 打赏
  • 举报
回复
mark
laodujxx 2006-11-14
  • 打赏
  • 举报
回复
C语言将浮点转换成整型采用直接截尾操作,就是舍去小数点后面的一切
mLee79 2006-11-14
  • 打赏
  • 举报
回复
1.0f : 符号位 0 , 阶码 0 + 127 = 0x7f , 尾数: 0 == 0011 1111 1 000 .... = 0x3f800000
0.0f : 有正0负0之分, 0x80000000 , 0x0 都是表示 0.0f ....
blue_zyb 2006-11-14
  • 打赏
  • 举报
回复
1.0的浮点表示为0x3F800000,换成十进制就是了
abblly 2006-11-14
  • 打赏
  • 举报
回复
5.2.10 Reinterpret cast
10 An lvalue expression of type T1 can be cast to the type
"reference to T2" if an expression of type "pointer to
T1" can be explicitly converted to the type "pointer to
T2" using a reinterpret_cast. That is, a reference cast
reinterpret_cast<T&>(x) has the same effect as the conversion
*reinterpret_cast<T*>(&x) with the built-in & and * operators.
The result is an lvalue that refers to the same object as the
source lvalue, but with a different type. No temporary is created,
no copy is made, and constructors (12.1) or conversion functions
(12.3) are not called.
转自水木社区CPlusPlus版
----------------------------
#include <iostream>
using namespace std;

int main()
{
float f = 1.0;
int i = *reinterpret_cast<int*>(&f);
cout<<"i="<<i<<endl;
cout<<"(int&)f = "<<(int&)f<<endl;
return 0;
}
jixingzhong 2006-11-14
  • 打赏
  • 举报
回复
0.0, 内存中就是全0,
那么用 int 解析,结果自然也是0

如果浮点数不是0.0,
那么需要使用浮点数规格表示数据:
float
共计32位,折合4字节
由最高到最低位分别是第31、30、29、……、0位
31位是符号位,1表示该数为负,0反之。
30-23位,一共8位是指数位。
22-0位,一共23位是尾数位。
每8位分为一组,分成4组,分别是A组、B组、C组、D组。
每一组是一个字节,在内存中逆序存储,即:DCBA

这样的结果,
解析为 int, 就是一个很大 整数 ....
jixingzhong 2006-11-14
  • 打赏
  • 举报
回复
解析 浮点数标准 ~~
taodm 2006-11-14
  • 打赏
  • 举报
回复
汗!
去看置顶文章:IEEE浮点数表示法。

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧