0

二重型変数に関する多くの問題、等式のテストやゼロ除算などの問題が発生した後、二重値を処理するクラスを作成し、新しいクラスで定期的に使用する固有の二重をシームレスに切り替えようと考えました。しかし、それは完全には適合しません。これが私のクラスです:

class HDouble
{
  //private:
public:
  double dValue;
  static const double dEpsilon;


  HDouble()
  {
    dValue = 0.0;
  }

  HDouble(double OtherValue)
  {
    if (IsNaN(OtherValue))
    {
      assert(0);
    }
    dValue = OtherValue;
  }

  const HDouble& operator=(const HDouble& OtherValue)
  {
    if (this == &OtherValue)      // Same object?
      return *this;

    if (IsNaN(OtherValue.dValue))
    {
      assert(0);
    }
    dValue = OtherValue.dValue;
    return *this;
  }

  const HDouble& operator=(const double& OtherValue)
  {
    dValue = OtherValue;
    return *this;
  }
  bool operator==(const HDouble& OtherValue)
  {
    return (abs(dValue - OtherValue.dValue) < dEpsilon);
  }

  //////////////////////////////////////////////////////////////////////////
  const HDouble& operator++()
  {
    dValue++;
    return *this;
  }
  const HDouble& operator++(int dummy)
  {
    dValue++;
    return *this;
  }
  const HDouble& operator--()
  {
    dValue--;
    return *this;
  }
  const HDouble& operator--(int dummy)
  {
    dValue--;
    return *this;
  }


//////////////////////////////////////////////////////////////////////////
  HDouble operator*(const HDouble& OtherValue)
  {
    HDouble Result = *this;
    Result *= OtherValue;
    return Result;
  }
  HDouble operator*(const double& OtherValue)
  {
    HDouble Result = *this;
    Result *= OtherValue;
    return Result;
  }

  HDouble operator/(const HDouble& OtherValue)
  {
    HDouble Result = *this;
    Result /= OtherValue;
    return Result;
  }
  HDouble operator/(const double& OtherValue)
  {
    HDouble Result = *this;
    Result /= OtherValue;
    return Result;
  }

  HDouble operator+(const HDouble& OtherValue)
  {
    HDouble Result = *this;
    Result += OtherValue;
    return Result;
  }
  HDouble operator+(const double& OtherValue)
  {
    HDouble Result = *this;
    Result += OtherValue;
    return Result;
  }

  HDouble operator-(const HDouble& OtherValue)
  {
    HDouble Result = *this;
    Result -= OtherValue;
    return Result;
  }
  HDouble operator-(const double& OtherValue)
  {
    HDouble Result = *this;
    Result -= OtherValue;
    return Result;
  }

  //////////////////////////////////////////////////////////////////////////
  HDouble& operator*=(const double& OtherValue)
  {
    dValue *= OtherValue;
    return *this;
  }
  HDouble& operator*=(const HDouble& OtherValue)
  {
    dValue *= OtherValue.dValue;
    return *this;
  }

  HDouble& operator+=(const HDouble& OtherValue)
  {
    dValue += OtherValue.dValue;
    return *this;
  }
  HDouble& operator+=(const double& OtherValue)
  {
    dValue += OtherValue;
    return *this;
  }


  HDouble& operator-=(const double& OtherValue)
  {
    dValue -= OtherValue;
    return *this;
  }
  HDouble& operator-=(const HDouble& OtherValue)
  {
    dValue -= OtherValue.dValue;
    return *this;
  }

  HDouble& operator/=(const double& OtherValue)
  {
    dValue /= OtherValue;
    return *this;
  }
  HDouble& operator/=(const HDouble& OtherValue)
  {
    dValue /= OtherValue.dValue;
    return *this;
  }
  //////////////////////////////////////////////////////////////////////////


  inline bool IsNaN(double d) 
  {
    if (!(d >= DBL_MIN && d <= DBL_MAX))
    {
      return true;
    }
    else
      return false;
  }
};

1つの問題は、たとえばcos()関数を呼び出す既存の関数のようなもので、doubleを予期します。必要なときにクラスオブジェクトを組み込みのdoubleに減衰させる方法はありますか?ありがとう。

ps私のクラスは既存のコードとシームレスに適合しなければなりません。それを変えることはできません。私にできることは、doubleを検索してHDoubleに置き換えることだけです。

4

3 に答える 3

0

はい、変換演算子をクラスに追加します:operator double() { return dValue; }クラスに。そうHDoubleすれば、に渡すときに double に変換されますcos

また、すべての演算子をメンバー関数として実装しました。operator overloadingについて読むことをお勧めします。

于 2012-09-21T21:17:04.193 に答える
0

以下を返すメソッドを追加できますdouble

double value()
{
    return dValue;
}
于 2012-09-21T21:17:48.467 に答える
0

クラスにコピー コンストラクターと変換演算子を追加する必要があります。また、クラスのデータ メンバーをprivate次の場所に移動する必要があります。

class HDouble 
{ 
private: 
  double dValue; 

public: 
  static const double dEpsilon; 

  HDouble() 
  { 
    dValue = 0.0; 
  } 

  HDouble(double OtherValue) 
  { 
    if (IsNaN(OtherValue)) 
    { 
      assert(0); 
    } 
    dValue = OtherValue; 
  } 

  HDouble(const HDouble &src) 
  { 
    dValue = src.dValue; 
  } 

  ...

  operator double() const
  {
    return dValue;
  }

  ...
}; 
于 2012-09-21T21:18:25.603 に答える