简体   繁体   English

为什么单个定义超出范围?

[英]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.

相关问题 索引超出范围错误 - Index out-of-range error 将varchar转换为smalldatetime会导致超出范围的值 - conversion of a varchar to a smalldatetime results in an out-of-range value 当变量定义大小时,为什么索引超出范围? - Why is the index out of range when the size is defined by a variable? 将varchar数据类型转换为datetime数据类型会引发超出范围的异常。 - The conversion of a varchar data type to a datetime data type throws an out-of-range exception..? Linq到Entities:将char数据类型转换为datetime数据类型会导致超出范围的datetime值 - Linq to Entities: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value 服务器区域性:将varchar数据类型转换为日期时间数据类型导致值超出范围 - Server culture:The conversion of a varchar data type to a datetime data type resulted in an out-of-range value 为什么我的阵列超出范围? - Why is my array out of range? 为什么该索引超出范围? (Visual Basic) - Why is this index out of range? (Visual Basic) 为什么会出现索引超出范围异常? - Why am I getting an Index Out Of Range Exception? 为什么该索引超出范围? 我该如何解决呢? - Why is this index out of range? And how do I solve it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM