首页 > 编程知识 正文

float原理,float数据类型举例

时间:2023-05-06 04:23:08 阅读:61291 作者:178

float类型的值以4字节表示,共32bit,根据3358 www.Sina.com /标准,float类型以1位为符号,8.8

整个浮点数可以表示为f=st2if=stimesttimes2^ { I } f=st2i。 其中ss是符号位-1或1,t t t是尾数,i i i是指数

以下三个部分分别介绍

符号: 0表示正,1表示负

指数部分:指数位用8位移码表示。 8bit可以表示256个,可以用转码表示的十进制范围为-127~128,如果将指数位的转码设为0,则意味着浮点数的指数为负的无限,浮点整体为0; 指数移动代码为255时,指数为正无限,浮点数为最大值。 因此,实际表现的代码移位范围为1 ^ {-126 } 254,变换为十进制的原代码为- 126 ^ 127,即可以用整个指数部分(注意:只计算指数部分)表现的最小正值为2126^{-126}2126 如图所示:

尾数部)尾数部共23位,显示范围为0(2) 23 )-1 ) 2231。 范式浮点数的尾数部分在小数点前隐藏固定的整数值整数1,尾数部分的23位仅用于表示小数点后的值。 这确保了浮点数的唯一表示。 规约式浮点尾数部分如图所示为:

最小绝对值分析规约表达式浮点数可以表示的最小正数为2 126 2^{-126} 2126,可以通过代码验证:

publicstaticvoidmain (final string [ ] args ) Throwsinterruptedexception ) bigdecimal B1=new bigdecimal (' 0.5 ' ); system.out.println(B1.pow(126 ).floatValue ) ); //2的-126次方的浮点数system.out.println (float.min _ normal ); //浮点数最小正规化正数}输出结果显示,float的最小规约化正数为2 126 2^{-126} 2126。

正值小于2时

− 126 2^{-126} 2−126时,指数部分将变成-127,根据前面的指数部分的介绍,此时的浮点数应为0,我们使用代码来验证发现并不是0:

public static void main(final String[] args) throws InterruptedException { BigDecimal b1 = new BigDecimal("0.5"); System.out.println(b1.pow(126).floatValue()); System.out.println(b1.pow(127).floatValue()); }

运行截图:

这是因为float类型采用了渐进式下溢出,当float类型的数小于 2 − 126 2^{-126} 2−126次方时将不继续采用规约式表示,而改为非规约式表示,所谓非规约式表示也就是摆脱了小数点前必须为1的限制,对于小于 2 − 126 2^{-126} 2−126的数小数点前可以为0。采用渐进式下溢出的原因是,如果不采用0与绝对值最小的浮点数之间距离将大于两个相邻浮点数之间的距离,且前者是后者的 2 23 2^{23} 223倍!!!(绝对值最小的规约式浮点数之前计算出为 2 − 126 2^{-126} 2−126,与0的距离为 2 − 126 2^{-126} 2−126,倒数第二小的值为 ( 1 + 2 − 23 ) × 2 − 126 = 2 − 126 + 2 − 149 (1+2^{-23}) times 2^{-126}=2^{-126}+2^{-149} (1+2−23)×2−126=2−126+2−149,与 2 − 126 2^{-126} 2−126的距离为 2 − 149 2^{-149} 2−149)

当采用了渐进式下溢出,float类型所能表示的绝对值最小的数便为: 2 − 149 2^{-149} 2−149,可用代码验证:

public static void main(final String[] args) throws InterruptedException { BigDecimal b1 = new BigDecimal("0.5"); System.out.println(b1.pow(149).floatValue()); System.out.println(Float.MIN_VALUE); }

运行结果为:

当浮点数的绝对值小于 2 − 149 2^{-149} 2−149时,则彻底超出了float所表示的最小绝对值,将会被表示成0。我们来验证一下 2 − 150 2^{-150} 2−150

public static void main(final String[] args) throws InterruptedException { BigDecimal b1 = new BigDecimal("0.5"); System.out.println(b1.pow(150).floatValue()); }

运行结果为:

综上所述,float类型的最小绝对值数为: 2 − 149 2^{-149} 2−149

最大绝对值分析

最大绝对值就比较简单了,当尾数全为1,指数为127时为最大,也就是 ( 2 − 2 − 23 ) × 2 127 (2-2^{-23}) times 2^{127} (2−2−23)×2127
使用代码来验证下:

public static void main(final String[] args) throws InterruptedException { BigDecimal b1 = new BigDecimal("2"); System.out.println(b1.pow(128).subtract(b1.pow(104)).floatValue()); System.out.println(Float.MAX_VALUE); }

运行结果如下:

当大于 ( 2 − 2 − 23 ) × 2 127 (2-2^{-23}) times 2^{127} (2−2−23)×2127时,将显示无穷大:

public static void main(final String[] args) throws InterruptedException { BigDecimal b1 = new BigDecimal("2"); System.out.println(b1.pow(128).add(new BigDecimal(1)).floatValue()); }

运行结果为:

参考文献:
https://baike.baidu.com/item/IEEE%20754/3869922?fr=aladdin

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。