在内存中float数是如何转化为int数的zhang_he_xiang的博客CSDN
来源:互联网 发布:用java打印菱形思路 编辑:程序博客网 时间:2024/06/07 20:08
转化过程如下(敬请谅解,英文版本)
Separate the 32 bits into one sign bit (s), eight exponent bits (e), and 23 significand bits (f). We will treat these as twos-complement integers.
If e is 255, the floating-point object is either infinity (if f is zero) or a NaN (otherwise). In this case, the conversion cannot be performed, and an error should be reported.
Otherwise, if e is not zero, add 224 to f. (If e is not zero, the significand implicitly has a 1 bit at its front. Adding 224 makes that bit explicit in f.)
Subtract 127 from e. (This converts the exponent from its biased/encoded form to the actual exponent. If we were doing a general conversion to any value, we would have to handle the special case when e is zero: Subtract 126 instead of 127. But, since we are only converting to an integer result, we can neglect this case, as long as the integer result is zero for these tiny input numbers.)
If s is 0 (the sign is positive) and e is 31 or more, then the value overflows a signed 32-bit integer (it is 231 or larger). The conversion cannot be performed, and an error should be reported.
If s is 1 (the sign is negative) and e is more than 31, then the value overflows a signed 32-bit integer (it is less than or equal to -232). If s is one, e is 32, and f is greater than 224 (any of the original significand bits were set), then the value overflows a signed 32-bit integer (it is less than -231; if the original f were zero, it would be exactly -231, which does not overflow). In any of these cases, the conversion cannot be performed, and an error should be reported.
Now we have an s, an e, and an f for a value which does not overflow, so we can prepare the final value.
If s is 1, set f to -f.
The exponent value is for a significand between 1 (inclusive) and 2 (exclusive), but our significand starts with a bit at 224. So we have to adjust for that. If e is 24, our significand is correct, and we are done, so return f as the result. If e is greater than 24 or less than 24, we have to shift the significand appropriately. Also, if we are going to shift f right, we may have to round it, to get a result rounded to the nearest integer.
If e is greater than 24, shift f left e-24 bits. Return f as the result.
If e is less than -1, the floating-point number is between -½ and ½, exclusive. Return 0 as the result.
Otherwise, we will shift f right 24-e bits. However, we will first save the bits we need for rounding. Set r to the result of casting f to an unsigned 32-bit integer and shifting it left by 32-(24-e) bits (equivalently, left by 8+e bits). This takes the bits that will be shifted out of f (below) and “left adjusts” them in the 32 bits, so we have a fixed position where they start.
Shift f right 24-e bits.
If r is less than 231, do nothing (this is rounding down; the shift truncated bits). If r is greater than 231, add one to f (this is rounding up). If r equals 231, add the low bit of f to f. (If f is odd, add one to f. Of the two equally near values, this rounds to the even value.) Return f.
https://stackoverflow.com/questions/12342926/casting-float-to-int-bitwise-in-c(此处为文章来源,供可以翻墙的小伙伴参阅)
- 在内存中float数是如何转化为int数的zhang_he_xiang的博客CSDN
- 整形数int、浮点型数据float,在内存中存储的表示
- 借助类型转化考察浮点数在内存中表示的一个心得
- float类型数据在内存中如何存储的?
- OJ------求int型数据在内存中存储时1的个数(二进制数中1的个数)
- float浮点数0.0和1.0在计算机内存中是怎么存的?
- float型的数在内存中的表示 附:python3解析函数
- 浮点数在内存上的表示
- 浮点数在内存中的格式(理解浮点到定点的转化)
- 由(int&)a引发的思考及浮点数在内存中的表示
- 由(int&)a引发的思考及浮点数在内存中的表示
- 类型转换:浮点数在内存中的存储方式以及(int&)a的意义
- 关于浮点型float数值是怎样在内存中存储的
- 浮点数f和浮点数的相反数-f在内存中怎么表示
- 有符号数在内存中是以其补码的形式存在的【如何求一个负数的补码】
- 将十进制数(int类型或者float类型)转化为二进制数
- 十进制数转化为二进制数中1的个数
- float在内存中二进制的存储原理
- 实用VPC虚拟私有云设计原则
- Java读取Excel文件
- Java写入Excel文件
- 成为程序员ing日记
- vim使用教程
- 在内存中float数是如何转化为int数的zhang_he_xiang的博客CSDN
- CSSbackground的详细使用
- 使用@Order调整配置类加载顺序
- Java文件上传
- Spring MVC文件上传
- 数据结构-二路归并-练习题1
- (一)安卓框架搭建之项目分层、主题、gradle基本配置
- 计蒜客 字符串长度
- Xcode IOS开发错误汇总