java Number小结

2017-01-09  本文已影响0人  好好学习天天引体向上

Byte

Short

Integer

Long

关于单精浮点数和双精浮点数结构

根据IEEE754标准,Float和Double的计算公式

在单精度时: 
  V=(-1)^s*2^(E-127)*M    
在双精度时: 
  V=(-1)^s*2^(E-1023)*M 

以float为例,float长度32位,位数从高到低

(对于Double,E为11位,M为52位)

当指数位取值1,小数位取最小值时,即0x00800000,获得float最小正标准值2^(-126)。

关于最小正标准值与最小正值的区别,摘抄一段stackoverflow中的回答:

For the single format, the difference between a normal number and a subnormal number is that the leading bit of the significand (the bit to left of the binary point) of a normal number is 1, whereas the leading bit of the significand of a subnormal number is 0. Single-format subnormal numbers were called single-format denormalized numbers in IEEE Standard 754.

也就是说,最小正标准值是保证小数位前导位为1的最小值。

非规格化表示:

当指数位全0时,小数位前导整数位由1变为0,此时取值范围:

2^(-126)*2^(-23) ~ 2^(-126)*(1-2^(-23))
此时的最小值也是float的正最小值,2^(-149)

特殊表示:

关于+0和-0,《java语言规范》中说的很明确:

Positive zero and negative zero compare equal; thus the result of the expression 0.0==-0.0 is true and the result of 0.0>-0.0 is false. But other operations can distinguish positive and negative zero; for example, 1.0/0.0 has the value positive infinity, while the value of 1.0/-0.0 is negative infinity.

对于float类型的+0.0f与-0.0f

关于NaN,同样来自《java语言规范》:

NaN is unordered, so:

•The numerical comparison operators < , <= , > , and >= return false if either or both operands are NaN (§15.20.1).

• The equality operator == returns false if either operand is NaN.
In particular, (x<y) == !(x>=y) will be false if x or y is NaN.

• The inequality operator != returns true if either operand is NaN (§15.21.1).
In particular, x!=x is true if and only if x is NaN.

综合以上,可以得到float的正最小值为2^(-149),即0x00000001,正最大值为0x7f7fffff,也即0x7fffffff-0x00000001(正无穷大-正最小值)

Float

对于Float的equals方法,比较的是两个float值的二进制表示,因此+0.0f.equals(-0.0f)返回false,NaN.equals(NaN)返回true,这是FLoat与float不同的地方。

Double

上一篇下一篇

猜你喜欢

热点阅读