当前位置:网站首页>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.
边栏推荐
- 虚拟内存与物理内存之间的关系
- 【Unity实战100例】文件压缩Zip和ZIP文件的解压
- C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.2 Early Experience of C Language
- Internet使用的网络协议是什么
- C专家编程 序
- 通过这两个 hook 回顾 Set/Map 基础知识
- [译] 容器和 Kubernetes 中的退出码完整指南
- 【微信小程序】【AR】threejs-miniprogram 安装(76/100)
- JVM内存结构详解
- 漏洞分析丨HEVD-0x6.UninitializedStackVariable[win7x86]
猜你喜欢

Graph adjacency matrix storage

使用百度EasyDL实现厂区工人抽烟行为识别

Nacos 配置中心

ISC2022 HackingClub white hat summit countdown 1 day!Most comprehensive agenda formally announced!Yuan universe, wonderful!

Interview Blitz 70: What are sticky packs and half packs?How to deal with it?

磷酸化甘露糖苷修饰白蛋白纳米粒/卵白蛋白-葡聚糖纳米凝胶的

移植MQTT源码到STM32F407开发板上

Transplant MQTT source code to STM32F407 development board

对C语言结构体内存对齐的理解

C语言_联合体共用体引入
随机推荐
Imitation cattle forum project
JS hoisting: how to break the chain of Promise calls
C专家编程 前言
在Cesium中实现与CAD的DWG图叠加显示分析
WeChat applet cloud development | personal blog applet
正则表达式
线上一次JVM FullGC搞得整晚都没睡,彻底崩溃~
图的邻接矩阵存储
C Pitfalls and pitfalls Appendix B Interview with Koenig and Moo
深拷贝浅拷贝
ISC2022 HackingClub white hat summit countdown 1 day!Most comprehensive agenda formally announced!Yuan universe, wonderful!
15 分钟带你入门 Grafana
MySQL 中出现的字符编码错误 Incorrect string value: ‘\x\x\x\x‘ for column ‘x‘
Taobao's API to get the list of shipping addresses
Common pits in the Go language
with语句和上下文管理器
对C语言结构体内存对齐的理解
MySQL Syntax Basics
C语言_typedef和结构体
Pytorch框架学校记录11——搭建小实战完整细节