当前位置:网站首页>C Pitfalls and Defects Chapter 7 Portability Defects 7.7 Truncation During Division
C Pitfalls and Defects Chapter 7 Portability Defects 7.7 Truncation During Division
2022-08-01 21:10:00 【weixin_Guest time】
Truncation during division
q = a / b;
r = a % b;
Here, let's assume b is greater than 0.
We want a,b,q,rWhat kind of relationship do you maintain?
1. The most important point, we want q * b + r == a, because this is the relationship that defines the remainder.
2. If we change the sign of a, we hope this will change the sign of q, but this will not change the absolute value of q.
3. When b>0, we want to ensure that r>=0 and r
However, the definition of C language only guarantees the first form, and when a>=0 and b>0, guarantees |r|<|b| and r>=0.
0 <= h< HASHSIZE, n is always non-negative, then we simply write as follows:
h = n % HASHSIZE;
However, if n may be negative, and h may be negative at this time, thenIt may not always be appropriate to do so.However, we know that h > -HASHSIZE,
so we can write:
h = n % HASHSIZE;
if (h < 0) {
h += HASHSIZE;
}
Better yet, programs should be designed to avoid situations where the value of n is negative and declare n to be unsignednumber.
边栏推荐
- 使用员工管理软件,解锁人力生产力新水平,提高人力资源团队灵活性
- 网红驼背矫正产品真的管用吗?如何预防驼背?医生说要这样做
- 织梦模板加入php代码
- How to encapsulate the cookie/localStorage sessionStorage hook?
- C Pitfalls and pitfalls Appendix B Interview with Koenig and Moo
- 移植MQTT源码到STM32F407开发板上
- ISC2022 HackingClub white hat summit countdown 1 day!Most comprehensive agenda formally announced!Yuan universe, wonderful!
- C Pitfalls and Defects Chapter 7 Portability Defects 7.9 Case Conversion
- 【接口测试】JMeter调用JS文件实现RSA加密
- property语法
猜你喜欢
随机推荐
【中文树库标记---CTB】
虚拟内存与物理内存之间的关系
Telnet弱口令渗透测试
使用员工管理软件,解锁人力生产力新水平,提高人力资源团队灵活性
这些 hook 更优雅的管理你的状态
360借条安全专家:陌生微信好友不要轻易加贷款推广多是诈骗
和我一起写一个音乐播放器,听一首最伟大的作品
Questions I don't know in database kernel interview(1)
关于Request复用的那点破事儿。研究明白了,给你汇报一下。
微服务负载均衡器Ribbon
R语言 线性回归的有关方法
响应式织梦模板美容整形类网站
使用百度EasyDL实现厂区工人抽烟行为识别
Hiking, cured my mental internal friction
ISC2022 HackingClub白帽峰会倒计时1天!最全议程正式公布!元宇宙集结,精彩绝伦!
TP5-NPs负载噻吩类化合物TP5白蛋白纳米粒/阿魏酸钠新糖牛血清蛋白纳米粒
测试的意义并不是能找到全部的缺陷
2022牛客多校联赛第五场 题解
移植MQTT源码到STM32F407开发板上
R语言进行相关的操作