舍入模式(rounding mode)
2010-01-21 15:13
302 查看
一看到舍入,不知道你是否想到了四舍五入,进而想到取整,注意,这是两个概念。
取整是指取一个有理数的整数部分,而这里介绍的舍入模式是和cpu内部浮点处理相
关的,是指对结果的截取。
首先,我们简要介绍一下单精度浮点数的表示:
bit 31 30-23 22-0
含义 符号 指数 尾数
具体介绍请参看http://en.wikipedia.org/wiki/Floating_point
我们知道,浮点数能够表示的精度有限,其实只有23个有效位,但是注意,这个是
表示上,或者说存储上,但是实际在cpu里不是这样的,记得80bit的long double
吗?与其相似,float在cpu内部计算的时候也会有“额外精度”。
如果得到一个浮点计算结果(2进制),例如:
a=1.0001 0000 1000 0011 1001 0111
注意,它有24个小数位,那么我们取两个数b和c,满足b<a<c
b=1.0001 0000 1000 0011 1001 011
c=1.0001 0000 1000 0011 1001 100
最后的结果就从这两个里面取,具体取哪个,取决于舍入模式的设置。
Round to nearest (even)
Rounded result is the closest to the infinitely precise result. If two
values are equally close, the result is the even value (that is, the
one with the least-significant bit of zero).
Round down (toward −∞)
Rounded result is closest to but no greater than the infinitely
precise result.
Round up (toward +∞)
Rounded result is closest to but no less than the infinitely precise
result.
Round toward zero (Truncate)
Rounded result is closest to but no greater in absolute value than
the infinitely precise result.
取整是指取一个有理数的整数部分,而这里介绍的舍入模式是和cpu内部浮点处理相
关的,是指对结果的截取。
首先,我们简要介绍一下单精度浮点数的表示:
bit 31 30-23 22-0
含义 符号 指数 尾数
具体介绍请参看http://en.wikipedia.org/wiki/Floating_point
我们知道,浮点数能够表示的精度有限,其实只有23个有效位,但是注意,这个是
表示上,或者说存储上,但是实际在cpu里不是这样的,记得80bit的long double
吗?与其相似,float在cpu内部计算的时候也会有“额外精度”。
如果得到一个浮点计算结果(2进制),例如:
a=1.0001 0000 1000 0011 1001 0111
注意,它有24个小数位,那么我们取两个数b和c,满足b<a<c
b=1.0001 0000 1000 0011 1001 011
c=1.0001 0000 1000 0011 1001 100
最后的结果就从这两个里面取,具体取哪个,取决于舍入模式的设置。
Round to nearest (even)
Rounded result is the closest to the infinitely precise result. If two
values are equally close, the result is the even value (that is, the
one with the least-significant bit of zero).
Round down (toward −∞)
Rounded result is closest to but no greater than the infinitely
precise result.
Round up (toward +∞)
Rounded result is closest to but no less than the infinitely precise
result.
Round toward zero (Truncate)
Rounded result is closest to but no greater in absolute value than
the infinitely precise result.
相关文章推荐
- BigDecimal 舍入模式(Rounding mode)介绍
- BigDecimal 舍入模式(Rounding mode)介绍
- BigDecimal 舍入模式(Rounding mode)介绍
- BigDecimal 舍入模式(Rounding mode)介绍
- 【Java实战】DecimalFormat对数值格式化的舍入问题——RoundingMode
- DecimalFormat对数值格式化的舍入问题——RoundingMode
- DecimalFormat对数值格式化的舍入问题——RoundingMode
- DecimalFormat对数值格式化的舍入问题——RoundingMode
- Rounding Methods(舍入模式)
- DecimalFormat对数值格式化的舍入问题——RoundingMode
- DecimalFormat对数值格式化的舍入问题——RoundingMode
- DecimalFormat对数值格式化的舍入问题——RoundingMode
- Unity代码设置"Standard (Specular setup)"的渲染模式(RenderingMode)
- MySQL的sql_mode 模式修改 my.cnf
- MySQL 严格模式 sql_mode
- 谈谈一些有趣的CSS题目(十七)-- 不可思议的颜色混合模式 mix-blend-mode
- UIViewContentMode各模式的含义和效果
- 怎样激活Mac OS X操作系统的开发模式( Enable Developer Mode on this Mac?)
- Android开发之Activity launchMode启动模式
- Linux kernel boot process——从实模式(real mode)到保护模式(protected mode),再到分页(paging)(转)