2016-05-19 5 views
2

intが範囲外の場合にmaxvalueを使用する方法はありますか? 私の目的のために私は長く使うことができません。intの結果が大きすぎる場合はmaxvalueを使用

int a = 842673832; 
int b = 2131231321; 

int t = a * b; 
//if t out of range, t = Int32.MinValue; 
+0

を有効にすることで、これを試すことができますか?のように、あなたの "目的"は何ですか? –

+0

私はC#プログラマーではありませんが、このケースではおそらくロジックで条件付きで使用できると思います。コンパクトにしたい場合は、三項演算子を使うことができます。 – jeteon

+0

私はレガシーシステムで作業しており、システムを大きく変更することはできません。 – Dawson

答えて

3

あなたが手の前にそれをテストしたりvery special kind of integer(リンクのようCheckedInt)を作成する必要があります。手の前にそれをテストする

オプション

、あなたは大きな整数値(たとえば、long)を扱うことができるデータ型を必要とする:

long t = (long)a * b; 
int r = t > int.MaxValue ? int.MaxValue : t < int.MinValue ? : int.MinValue : (int)t; 

オプションB

特別な種類の整数を使用するには、基本演算子にもオーバーロードする必要があります。

は例では、リンクでは、例外がスローされますが、このような何かint.MaxValueint.MinValueを実装するには例外を変更することができます:あなたはtintないようにしたい場合は

public struct CheckedInt { 
    private int Value { get; set; } 
    public CheckedInt(int value) 
     : this() { 
     Value = value; 
    } 

    public static implicit operator CheckedInt(int me) { 
     return new CheckedInt(me); 
    } 

    public static CheckedInt operator +(CheckedInt lhs, CheckedInt rhs) { 
     double testResult = (double)lhs.Value + (double)rhs.Value; 
     if (testResult > int.MaxValue) 
      return int.MaxValue; 
     if (testResult < int.MinValue) 
      return int.MinValue; 
     return new CheckedInt(lhs.Value + rhs.Value); //note that direct lhs+rhs will cause StackOverflow 
    } 

    public static CheckedInt operator -(CheckedInt lhs, CheckedInt rhs) { 
     double testResult = (double)lhs.Value - (double)rhs.Value; 
     if (testResult > int.MaxValue) 
      return int.MaxValue; 
     if (testResult < int.MinValue) 
      return int.MinValue; 
     return new CheckedInt(lhs.Value - rhs.Value); //note that direct lhs-rhs will cause StackOverflow 
    } 

    public static CheckedInt operator *(CheckedInt lhs, CheckedInt rhs) { 
     double testResult = (double)lhs.Value * (double)rhs.Value; 
     if (testResult > int.MaxValue) 
      return int.MaxValue; 
     if (testResult < int.MinValue) 
      return int.MinValue; 
     return new CheckedInt(lhs.Value * rhs.Value); //note that direct lhs*rhs will cause StackOverflow 
    } 

    public static CheckedInt operator /(CheckedInt lhs, CheckedInt rhs) { 
     double testResult = (double)lhs.Value/(double)rhs.Value; 
     if (testResult > int.MaxValue) 
      return int.MaxValue; 
     if (testResult < int.MinValue) 
      return int.MinValue; 
     return new CheckedInt(lhs.Value/rhs.Value); //note that direct lhs-rhs will cause StackOverflow 
    } 

    //Add any other overload that you want 

    public override string ToString() { //example 
     return Value.ToString(); 
    } 

    public bool Equals(CheckedInt otherInt) { //example 
     return Value == otherInt.Value; 
    } 
} 
+0

回答ありがとうございました – Dawson

+1

@Dawsonあなたは歓迎です。 :) – Ian

-1

をイアンの答えのような大きなタイプの場合、OverflowExceptionにキャッチし、tをMaxValueに設定することができます。オーバーフローチェックを可能にするには、セクションをcheckedキーワードで囲むようにしてください。

int t = 0; 
checked { 
    try 
    { 
     int a = 842673832; 
     int b = 2131231321; 

     t = a * b; 
    } 
    catch (OverflowException) 
    { 
     t = int.MaxValue; 
    } 
} 
+0

あなたはこれを試してみましたか?それは包み込む –

+0

@DavidPilkingtonああ、私は 'checked'キーワードを見逃しているに違いありません。私は私の答えを更新しました。 – Steve

2

あなたは制限が実施されているものを説明してもらえoverflow checking

int t; 

try 
{ 
    int a = 842673832; 
    int b = 2131231321; 

    t = checked(a * b); 
} 
catch (System.OverflowException e) 
{ 
    t = Int32.MaxValue; 
} 
関連する問題