如果我尝试填充比float.MaxValue
浮点数更大的数字会发生什么?
我的代码中有这个:
int a;
float x;
float y;
...
float b = (a * x) / y;
我对此有两个问题:
float.MaxValue
吗?a
吗?如果没有,我应该如何处理以防止灾难?你会得到float.PositiveInfinity
(或float.NegativeInfinity
)...或者你只会留在float.MaxValue
(float.MaxValue + 1 == float.MaxValue
)...浮点有一个“分辨率”,浮点越大,分辨率越低......最后两个浮点之间的距离是关于...
3,40282347E+38: float.MaxValue
3,40282326E+38:之前的浮动
2,028241E + 31:差异<--------- 2 * 2 ^ 31!
通过计算
float nearMaxValue = BitConverter.ToSingle(BitConverter.GetBytes(BitConverter.ToInt32(BitConverter.GetBytes(float.MaxValue), 0) - 1), 0);
Console.WriteLine("{0:R}: float.MaxValue", float.MaxValue);
Console.WriteLine("{0:R}: previous float", nearMaxValue);
Console.WriteLine("{0:R}: difference", float.MaxValue - nearMaxValue);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句