确定是否整数溢出高于或低于界限高于、整数、界限

2023-09-06 15:55:02 作者:?原谅我曾经想过,你给的是我的,伤心就点到为止。

使用 C#的,我有我需要将一些自定义类能够检测整数溢出并返回缺省最小值或最大值取决于如果溢出是由于其结果是超过最大值或下的最小值。我似乎无法找到如何能够检测到出现在任何地方的类型溢出的建议。

Using C#, I have a few custom classes where I need to be able to detect integer overflows and return a default minimum or maximum value depending on if the overflow was due to the result being over the maximum value or under the minimum value. I can't seem to find a suggestion on how to detect the "type" of overflow that occurs anywhere.

中的类两种类型划分:那些使用符号值,和那些使用无符号值

The classes are divided between two general types: ones that use signed values, and ones that use unsigned values.

作为一个例子,这里是与Int32值处理的类之一:

As an example, here is one of the classes that deals with Int32 values:

public class Stat32Tf : IStat32T<float>
{
    #region fields

    private int baseValue, baseAdjustment;
    private float baseMultiplier;

    #endregion

    #region ctors

    public Stat32Tf()
    {
        baseValue = 0;
        baseAdjustment = 0;
        baseMultiplier = 1f;
    }

    public Stat32Tf(int baseValue, int baseAdjustment = 0, float baseMultiplier = 1f)
    {
        this.baseValue = baseValue;
        this.baseAdjustment = baseAdjustment;
        this.baseMultiplier = baseMultiplier;
    }

    #endregion

    #region properties

    public int BaseValue
    {
        get 
        { 
            return baseValue; 
        }
        set 
        {
            baseValue = value; 
        }
    }

    public int BaseAdjustment
    {
        get 
        { 
            return baseAdjustment; 
        }
        set 
        { 
            baseAdjustment = value; 
        }
    }

    public float BaseMultiplier
    {
        get 
        { 
            return BaseMultiplier; 
        }
        set 
        { 
            baseMultiplier = value; 
        }
    }

    public int TruncValue
    {
        get 
        { 
            return (int)Value; 
        }
    }

    public float Value
    {
        get 
        { 
            return (baseValue + baseAdjustment) * baseMultiplier; 
        }
    }

    #endregion

}

正如你所看到的,这个类的想法是保持一个基本值,调整值,并乘数值,并在Value属性返回的总价值。 (该TruncValue财产公正,因为它表明,返回截断的整体价值,丢弃任何分数值)。

As you can see, the idea of the class is to hold a base value, an adjustment value, and a multiplier value, and return the aggregate value in the Value property. (The TruncValue property just, as it suggests, returns the truncated whole value, dropping any fractional values).

的目标是处理溢出,在数值属性的获取访问,并且如果结果为超过最大int值,返回int.MaxValue并且如果它是在最小值,返回int.MinValue,所有不扔实际的溢出错误。该计划使棘手的适合我的部分是,调整值和乘数可能是负值,以及(根据设计要求)。

The goal is to handle overflows in the "get" accessor of the Value property and, if the result is over the max int value, return int.MaxValue and if it is under the min value, return int.MinValue, all without throwing the actual overflow error. The part that's making it tricky for me is that the adjustment values and multipliers could be negative values as well (as per the design requirement).

什么是安全的方式来实现这一目标?我一直没能找到解决这种情况的任何资源。我猜某种算术算法将需要用于确定结果将超过或下

What is a safe way to achieve this? I have not been able to find any resources that address this kind of situation. I'm guessing some sort of arithmetic algorithm will need to be used to determine of results will be over or under.

推荐答案

花车pretty的大。你期待的get值溢出或者你希望强制转换为int溢出?如果它只是类似于以下code投的东西可能会奏效。

Floats are pretty big. Are you expecting the get value to overflow or do you expect the cast to int to overflow? If it's just the cast something similar to the following code might work.

//This answer is wrong, see below.
public int TruncValue
{
    get
    {
        if (Value > (float)int.MaxValue)
        {
            return int.MaxValue
        }
        else if (Value < (float)int.MinValue)
        {
            return int.MinValue
        }
        else
        {
            return (int)Value;
        }
    }
}

虽然你可能需要一些额外的处理的边缘情况。

Although you might need some additional handling for the edge cases.

编辑 - 我研究了一下这个在一些code,发现我没想到的一些行为,但显然它是在规范

Edit - I played around with this in some code and found some behavior that I didn't expect, but apparently it is in the specification.

例如,

var Value = int.MaxValue + int.MaxValue //Ends up returning -2 with no exception in debug mode.
var MaxCalculatedValue = (int.MaxValue + int.MaxValue) * float.MaxValue //Ends up returning something like -3.4... ^38.

您真的可能需要多达投一切都变成双,然后检查,如果结果大于或小于一个int。

You really might need to up cast everything into a double and then check to see if the result is greater than or less than an int.

因此​​,它可能是这个样子:

So it might look something like this:

public float Value
{
    get
    {
        var result = ((double)baseValue + (double)baseAdjustment) * (double)baseMultiplier;
        if (result > (double)int.MaxValue)
        {
           return (float)int.MaxValue)
        }
        if (result < (double)int.MinValue)
        {
           return (float)int.MinValue)
        }
        return (float)result;
    }
}