11.11.2015 Взять и поделить или деление по модулю
Материал из SRNS
|
Есть некоторая неуверенность в результатах работы функций взятия по модулю, для борьбы с которой составлена эта памятка.
Ниже представлены результаты, полученные на Oryx 161, компилятор из Xilinx SDK 2014.4 ( gcc version 4.8.3 20140320 (prerelease) (Sourcery CodeBench Lite 2014.05-23)).
Классический %
Оператор %
(int(13)) % (int(-17)) = 13
(int(13)) % (int(-7)) = 6
(int(13)) % (int(-5)) = 3
(int(13)) % (int(-1)) = 0
(int(13)) % (int(1)) = 0
(int(13)) % (int(5)) = 3
(int(13)) % (int(7)) = 6
(int(13)) % (int(17)) = 13
(int(-13)) % (int(-17)) = -13
(int(-13)) % (int(-7)) = -6
(int(-13)) % (int(-5)) = -3
(int(-13)) % (int(-1)) = 0
(int(-13)) % (int(1)) = 0
(int(-13)) % (int(5)) = -3
(int(-13)) % (int(7)) = -6
(int(-13)) % (int(17)) = -13
(unsigned int(13)) % (int(-17)) = 13
(unsigned int(13)) % (int(-7)) = 13
(unsigned int(13)) % (int(-5)) = 13
(unsigned int(13)) % (int(-1)) = 13
(unsigned int(13)) % (int(1)) = 0
(unsigned int(13)) % (int(5)) = 3
(unsigned int(13)) % (int(7)) = 6
(unsigned int(13)) % (int(17)) = 13
(int(13)) % (unsigned int(1)) = 0
(int(13)) % (unsigned int(5)) = 3
(int(13)) % (unsigned int(7)) = 6
(int(13)) % (unsigned int(17)) = 13
(int(-13)) % (unsigned int(1)) = 0
(int(-13)) % (unsigned int(5)) = 3
(int(-13)) % (unsigned int(7)) = 5
(int(-13)) % (unsigned int(17)) = 5
(unsigned int(13)) % (unsigned int(1)) = 0
(unsigned int(13)) % (unsigned int(5)) = 3
(unsigned int(13)) % (unsigned int(7)) = 6
(unsigned int(13)) % (unsigned int(17)) = 13
(int(13)) % (int(-7)) = 6
(int(13)) % (int(-5)) = 3
(int(13)) % (int(-1)) = 0
(int(13)) % (int(1)) = 0
(int(13)) % (int(5)) = 3
(int(13)) % (int(7)) = 6
(int(13)) % (int(17)) = 13
(int(-13)) % (int(-17)) = -13
(int(-13)) % (int(-7)) = -6
(int(-13)) % (int(-5)) = -3
(int(-13)) % (int(-1)) = 0
(int(-13)) % (int(1)) = 0
(int(-13)) % (int(5)) = -3
(int(-13)) % (int(7)) = -6
(int(-13)) % (int(17)) = -13
(unsigned int(13)) % (int(-17)) = 13
(unsigned int(13)) % (int(-7)) = 13
(unsigned int(13)) % (int(-5)) = 13
(unsigned int(13)) % (int(-1)) = 13
(unsigned int(13)) % (int(1)) = 0
(unsigned int(13)) % (int(5)) = 3
(unsigned int(13)) % (int(7)) = 6
(unsigned int(13)) % (int(17)) = 13
(int(13)) % (unsigned int(1)) = 0
(int(13)) % (unsigned int(5)) = 3
(int(13)) % (unsigned int(7)) = 6
(int(13)) % (unsigned int(17)) = 13
(int(-13)) % (unsigned int(1)) = 0
(int(-13)) % (unsigned int(5)) = 3
(int(-13)) % (unsigned int(7)) = 5
(int(-13)) % (unsigned int(17)) = 5
(unsigned int(13)) % (unsigned int(1)) = 0
(unsigned int(13)) % (unsigned int(5)) = 3
(unsigned int(13)) % (unsigned int(7)) = 6
(unsigned int(13)) % (unsigned int(17)) = 13
Следует обратить внимание:
- результаты для -13%(int 7) и -13%(uint 7) различаются!
- взятие uint % отрицательного числа - холостая операция, результат - исходный uint
Для наглядности построены графики (доступен fig):
[ Хронологический вид ]Комментарии
Войдите, чтобы комментировать.