The mod(%) operation 负数取模运算详解
2015-08-13 22:07
435 查看
Please indicate the source if you want to reprint: http://blog.csdn.net/gaoxiangnumber1
If a and d are integers, d is not zero, then the remainder r fits the formula:
a = q*d + r
q is an integer, and 0 ≤ |r| < |d|.
For C++/Java, the mod operation satisfy the following rules:
When a and d have the same symbol, the result of a%d is to make q as small as possible.
When a and d don’t have the same symbol, the result of a%d is to make q as big as possible.
Test program:
Result:
2
-2
-3
-2
2
-3
3
Please indicate the source if you want to reprint: http://blog.csdn.net/gaoxiangnumber1
If a and d are integers, d is not zero, then the remainder r fits the formula:
a = q*d + r
q is an integer, and 0 ≤ |r| < |d|.
For C++/Java, the mod operation satisfy the following rules:
When a and d have the same symbol, the result of a%d is to make q as small as possible.
When a and d don’t have the same symbol, the result of a%d is to make q as big as possible.
Test program:
[code]#include<iostream> using namespace std; int main() { cout<<20%3<<endl; cout<<(-20)%(-3)<<endl; cout<<(-3)%(-20)<<endl; cout<<(-20)%(3)<<endl; cout<<(20)%(-3)<<endl; cout<<(-3)%(20)<<endl; cout<<(3)%(-20)<<endl; return 0; }
Result:
2
-2
-3
-2
2
-3
3
Please indicate the source if you want to reprint: http://blog.csdn.net/gaoxiangnumber1
相关文章推荐
- linux下安装mysql数据库
- lnmp全面优化集合nginx+mysql+php
- 我的学习笔记1:加密、解密,以及OpenSSL创建私有CA
- Linux权限数字解析
- linux yum命令详解
- Linux-2.6.32 NUMA架构之内存和调度
- 如何在Linux操作系统之上的MySQL数据库操作环境中使用清屏命令?
- Linux C编程的DEBUG宏
- oop_day05_package、public、static、final、内部类
- 终端、控制台、shell的关系
- linux中fork()函数详解
- -Dmaven.multiModuleProjectDirectory system propery is not set.
- OpenGL ES 光照
- UVA11261 Bishops
- Linux socket编程的心跳机制总结
- Centos java 安装
- 关于记号粘贴操作符(token paste operator): `##`
- linux系统优化参数
- summary of mirantis openstack integrating with vsphere
- 菜鸟第四天:Linux文件管理命令详解