确定操作是否会导致溢出? [英] Determine if operation will result in overflow?

查看:210
本文介绍了确定操作是否会导致溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一些代表物理单位的类,例如温度。这些类有能力有一些与他们相关的单位,如Celcius和Kelvin。他们也有用户定义的最大值和最小值。

当我想改变单位时,我得到两个给定单位之间的转换系数,然后改变 Min Max Data 转换因子。

  Double ConversionFactor = GetConversionFactor(curUnits,newUnits); 
Units = newUnits;
Min * = ConversionFactor;
Max * = ConversionFactor;
Data * = ConversionFactor;

我遇到的问题是,有时用户可能会允许max和min值为 MinValue MaxValue ,例如 Double 。如果用户要改变换算系数大于1的单位,将导致溢出。



是否有一种方法在C#中检测溢出和溢出是否会发生?



从一些测试中我可以看出,我认为我只需要检查手术的结果是不是等于正的或负的无穷大?如果是这样的话,我可以把值设回 MinValue MaxValue



另外,奖金问题。在我的测试中,我发现如果我创建一个 Double max = Double.MaxValue 并在其中添加一个像100这样的小东西。值不会改变。

为什么会这样? MaxValue PositiveInfinity 因为精度在这么大的价值是如此之低吗?

解决方案

是一个双精度型,你应该检查 Infinity

最好的方法是使用 double.IsInfinity ,它将检查下溢和溢出(正和负无穷大)。

  bool overflowed = double.IsInfinity(Min); 

你可以用自己的扩展方法来包装它,如果你愿意的话。 b
$ b




如果变量是一个整数,则可以将语句包装在选中的块中,当操作溢出时会造成异常。

 已勾选
{
...

$ / code>

您可以 try ... catch 那。


I have some classes within my application that represent physical units, for instance temperature. These classes have the ability to have some units associated with them and changed, such as Celcius and Kelvin. They also have user defined Max and Min values.

When I want to change units I get a conversion factor between the two given units and then alter the Min, Max, and Data values within the class by performing some arithmetic operation with the conversion factor.

Double ConversionFactor = GetConversionFactor(curUnits, newUnits);
Units = newUnits;
Min *= ConversionFactor;
Max *= ConversionFactor;
Data *= ConversionFactor;

The issue I am having is that sometimes a user might allow the max and min values to be the MinValue and MaxValue of some type such as Double. If a user were to change units that have a conversion factor larger than 1 this will result in overflow.

Is there a way in C# to detect if this overflow will happen for both overflow and underflow?

From what I can tell with some tests I think I just need to check that the result of the operation does not equal positive or negative infinity? If so I can then just set the value back to either MinValue or MaxValue.

Also, bonus question. In my testing I found that if I create a Double max = Double.MaxValue and add something small like 100 to it. The value does not change.

Why is this? Is there some threshold between MaxValue and PositiveInfinity because the precision is so low at that large of value?

解决方案

If the variable is a double, you should check for Infinity.

The best way to do that is by using double.IsInfinity, which will check for both underflow and overflow (positive and negative infinity).

bool overflowed = double.IsInfinity(Min);

You can wrap that in an own extension method or so if you want to.


If the variable is an integer you can wrap the statement in a checked block, which will cause an exception when the operation overflows. This isn't preventive, but it does the job.

checked
{
   ...
}

You can try...catch that.

这篇关于确定操作是否会导致溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆