bate's blog

調べたこと実装したことなどを取りとめもなく書きます。

演算子で気をつけること

result = friction1 / friction2;

この時に使われる除算演算子( / )は左のfriction1の物。

result = friction1.operator/( friction2 );

このような形になる。

TFriction<T> operator/( TFriction<T> t )
{
	this->Exec();
	t.Exec();

	TFriction<T> temp( this->m_numerator*t.GetDenominator(),	// 分子
			this->m_denominator*t.GetNumerator() );		// 分母

	temp = temp.reduction();

	return temp;
}

上記にある分子、分母の計算部分で、

TFriction<T> temp( this->m_numerator*t.GetDenominator(),	// 分子
		this->m_denominator*t.GetNumerator() );		// 分母

ここで、
( friction1.分子/friction1.分母 ) / ( friction2.分子 / friction2.分母 ) = ( friction1.分子/friction1.分母 ) * ( friction2.分母 / friction2.分子 )
にしているので、friction1とfriction2の順番で結果が変わる。
a=1/2, b=1/3 としてみれば、a / b = 1/2 * 3/1 = 3/2, b / a = 1/3 * 2/1 = 2/3となる。
左右オペランドに注意すること。