Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of dividing negative numbers by positive numbers in C language

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the analysis of negative numbers divided by positive numbers in C language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "C language negative number divided by positive number example analysis" it!

A modest spur to induce others to come forward with valuable contributions

C language negative number divided by positive number, positive number divided by negative number or negative number divided by negative remainder and quotient, who is responsible?

-3 / 2 =?

-3% 2 =?

3 / (- 2) =?

3% (- 2) =?

(- 3) / (- 2) =?

(- 3)% (- 2) =?; premise hypothesis

Suppose we divide a by b, the quotient is Q, and the remainder is rpart Q = a / b _ t _ r = a% b; here, we might as well assume that b is greater than 0. What kind of relationship do we want to maintain between a, b, Q and r?

The heaviest point, we want Q * b + r = = a, because this is the relationship that defines the remainder.

If we change the sign of a, we hope this will change the symbol of Q, but it will not change the absolute value of Q.

When b > 0, we want to ensure that r > = 0 and r

< b。 例如,如果余数用于哈希表的索引,确保它是一个有效的索引值很重 。这三条性质是我们认为整数除法和余数操作所应该具备的。很不幸的是,它们不可能同时成立。 举例说明 考虑一个简单的例子:3/2,商为1,余数也为1。此时,第1条性质得到了满足。(-3)/2 的值应该是多少呢?如果满足第2条性质,答案应该是-1,但如果是这样,余数就必定是-1,这样第3条性质就无法满足了。如果我们首先满足第3条性质,即余数是 1,这种情况下根据第1条性质则商是-2,那么第2条性质又无法满足了。 因此,C语言或者其他语言在实现整数除法截断运算时,必须放弃上述三条原则中的至少一条。大多数程序设计语言选择了放弃第 3 条,而改为求余数与被除数的正负号相同。这样,性质1和性质2就可以得到满足。大多数C编译器在实践中也都是这样做的。 然而,C语言的定义只保证了性质1,以及当 a>

When = 0 and b > 0, guarantee | r |

< |b|以及 r>

= 0. The latter part of the guarantee is much less restrictive than property 2 or property 3.

Example demonstration

Although the definition of C language sometimes brings unnecessary flexibility, most of the time, as long as the programmer knows exactly what to do and what to do, this definition is sufficient for integer division operations to meet their needs. For example,

Suppose we have a number n, which represents the result of some function operation on the characters in the identifier. We want to get the entry h of the hash table by division, which satisfies 0.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report