unsigned minus unsigned

Unsigned integer arithmetic is always performed modulo 2n
 where n is the number of bits in that unsigned integer. (From here.)

That's as much as to say: {0, 1, 2 ... 7}, besides addition modulo 8, when 1 - 6:

(1 - 6) mod 8 = 8 + 1 -6 = 3 belongs to {0, 1, 2 ... 7}

8-6 is the complement of -6 (similar to 2's complement used in computer)


It takes 3 steps to move forward to 1 from 6 on the following straight line: 

... 0, 1 ... 6, 7,   0, 1 ... 6, 7,   0, 1 ... 6, 7 ...

or a circle.


A better name for unsigned integers' + and - should be "circular plus" and "circular minus".Circular !

Also take a look at how to compute the mean of circular quantities ( e.g. angles ) correctly ! Code: 

inline double CircularMean(const std::vector<double> &angles)
{
	double sum_cos = 0, sum_sin = 0;
	for (std::vector<double>::const_iterator it = angles.begin(); it != angles.end(); it++)
	{
		sum_cos += std::cos(*it);
		sum_sin += std::sin(*it);
	}
	return std::atan2(sum_sin, sum_cos);
}


Yeah. There's something of mathematics behind circular quantities.

Cyclic order

Cyclically ordered group


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章