铸造float.MaxValue龙抛出的异常? [英] Casting float.MaxValue to Long throws exception?
问题描述
试图让float.MaxValue的整数部分,但它抛出错误?
Trying to get integer part of float.MaxValue but it is throwing error?
long l = Convert.ToInt64(float.MaxValue);
Console.WriteLine(l);
错误: 算术运算导致溢出。
Error: Arithmetic operation resulted in an overflow.
推荐答案
在 浮动
是3.4E + 38,而一的 长
是9.2 E + 18。因此, float.MaxValue
将不适合进入长
。
借助 Convert.ToInt64
方法抛出发生OverflowException
当您指定的值比 Int64.MaxValue
或小于 Int64.MinValue
The Convert.ToInt64
method throws an OverflowException
when you specify a value that is greater than Int64.MaxValue
or less than Int64.MinValue
如果你只是想获得 float.MaxValue
的整数值,请参阅相关的答案的这个问题。但要注意,即使剥夺了浮点,一个浮动的最大值
仍然不适合在长
数据类型。
If you just want to get the integer value of float.MaxValue
, see the relevant answers to this question. But note that even stripped of the floating point, the maximum value of a float
will still not fit in the long
data type.
编辑:的原因,一个浮动
(32位值)比一个更高的最大值长
(64位值)是由于的方式浮点数被内部重新由计算机psented $ P $。浮点值类型可容纳值所能比的定点和整数类型的更广泛的范围,虽然这更大的范围在precision为代价来实现。
The reason that a float
(a 32-bit value) has a higher maximum value than a long
(a 64-bit value) is because of the way floating point numbers are internally represented by the computer. Floating point value types can hold wider ranges of values than can fixed-point and integer types, although this greater range is achieved at the expense of precision.
这篇关于铸造float.MaxValue龙抛出的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!