当前位置:网站首页>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.
边栏推荐
- C陷阱与缺陷 第7章 可移植性缺陷 7.11 可移植性问题的一个例子
- idea实用快捷键合集——持续更新
- 【力扣】字符串相乘
- 【中文树库标记---CTB】
- Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
- Realize the superposition display analysis of DWG drawing with CAD in Cesium
- 在Cesium中实现与CAD的DWG图叠加显示分析
- 【Unity实战100例】文件压缩Zip和ZIP文件的解压
- (七)《数电》——CMOS与TTL门电路
- 软考 ----- UML设计与分析(上)
猜你喜欢
[Chinese tree tags - CTB]
An online JVM FullGC made it impossible to sleep all night and completely crashed~
Nacos 配置中心
Realize the superposition display analysis of DWG drawing with CAD in Cesium
技能大赛训练:A部分加固题目
Internet使用的网络协议是什么
测试的意义并不是能找到全部的缺陷
R语言 数据的关系探索
Excel advanced drawing techniques, 100 (22) - how to respectively the irregular data
有点奇怪!访问目的网址,主机能容器却不行
随机推荐
Questions I don't know in database kernel interview(1)
Imitation cattle forum project
15 分钟带你入门 Grafana
LeetCode·每日一题·1374.生成每种字符都是奇数个的字符串·模拟
附录A printf、varargs与stdarg A.3 stdarg.h ANSI版的varargs.h
Common pits in the Go language
Pytorch框架学习记录8——最大池化的使用
Classification interface, Taobao classification details API
C Pitfalls and Defects Chapter 7 Portability Defects 7.11 An Example of a Portability Problem
ISC2022 HackingClub白帽峰会倒计时1天!最全议程正式公布!元宇宙集结,精彩绝伦!
织梦发布文章提示body has not allow words错误
sizeof的详细解说和与strlen的区别
那些关于DOM的常见Hook封装(一)
C Pitfalls and Defects Chapter 5 Library Functions 5.5 Library Function Signal
微服务负载均衡器Ribbon
C陷阱与缺陷 第7章 可移植性缺陷 7.11 可移植性问题的一个例子
Protocol Buffer usage
织梦模板加入php代码
Internet使用的网络协议是什么
关于Request复用的那点破事儿。研究明白了,给你汇报一下。