[英]Why can a single be defined out-of-range?
The range of single data type is -3.4028235E+38
through -1.401298E-45
for negative values. 对于负值,单个数据类型的范围是-3.4028235E+38
到-1.401298E-45
。 Why does this code not result in a compile time error? 为什么此代码不会导致编译时错误?
'(Option Strict On)
Dim s As Single
s = -5.4028235E+38
Because it is converted from a Double to a Single, which results in Single.NegativeInfinity
. 因为它是从Double转换为Single,所以导致Single.NegativeInfinity
。 If you would explicitly define the datatype like this (or with a !
, details here ): 如果您要像这样显式定义数据类型(或使用!
, 请参见此处的详细信息 ):
s = -5.4028235E+38F
It would result in an overflow and wouldn't compile. 这将导致溢出并且无法编译。
From the MSDN page about conversions of the single data type: 在有关单个数据类型转换的MSDN页面上 :
Double Narrowing: out-of-range values are converted to Double.NegativeInfinity or Double.PositiveInfinity. Double Narrowing:超出范围的值将转换为Double.NegativeInfinity或Double.PositiveInfinity。
In addition, the Double values Double.NaN, Double.PositiveInfinity, and Double.NegativeInfinity covert to Single.NaN, Single.PositiveInfinity, and Single.NegativeInfinity, respectively. 此外,将Double.NaN,Double.PositiveInfinity和Double.NegativeInfinity的Double值分别隐藏为Single.NaN,Single.PositiveInfinity和Single.NegativeInfinity。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.