设为首页 加入收藏

TOP

5.1 Arithmetic Operators (1)
2013-10-07 15:25:28 来源: 作者: 【 】 浏览:51
Tags:5.1 Arithmetic Operators

 

Unless noted otherwise, these operatorsmay be applied to any of the arithmetic types (Section 2.1, p. 34), or any type that can be converted to an arithmetic type.

The table groups the operators by their precedence—the unary operators have the highest precedence, then the multiplication and division operators, and then the binary addition and subtraction operators. Operators of higher precedence group more tightly than do operators with lower precedence. These operators are all left associative,meaning that they group left to rightwhen the precedence levels are the same.

算术表达式的优先级和结合性符合直觉,先乘除后加减,从左往右计算。

Applying precedence and associativity to the previous expression:

  1. 5 + 10 * 20/2; 

we can see that the operands to the multiplication operator (*) are 10 and 20. The result of that expression and 2 are the operands to the division operator (/). The result of that division and 5 are the operands to the addition operator (+).

The unary minus operator has the obvious meaning. It negates its operand:

  1. int i = 1024;  
  2. int k = -i; // negates the value of its operand 

Unary plus returns the operand itself. It makes no change to its operand.

CAUTION: OVERFLOW AND OTHER ARITHMETIC EXCEPTIONS

The result of eva luating some arithmetic expressions is undefined. Some expressions are undefined due to the nature of mathematics—for example, division by zero. Others are undefined due to the nature of computers—such as overflow, in which a value is computed that is too large for its type.

Consider amachine onwhich shorts are 16 bits. In that case, themaximumshort is 32767. Given only 16 bits, the following compound assignment overflows:

  1. // max value if shorts are 8 bits  
  2. short short_value = 32767;  
  3. short ival = 1;  
  4. // this calculation overflows  
  5. short_value += ival;  
  6. cout << "short_value: " << short_value << endl; 

这里有误,应该是 :

  1. // max value if shorts are 16 bits。 

Representing a signed value of 32768 requires 17 bits, but only 16 are available. On many systems, there is no compile-time or run-time warning when an overflow might occur. The actual value put into short_value varies across different machines. On
our system the program completes and writes

  1. short_value: -32768 

The value “wrapped around:” The sign bit, which had been 0, was set to 1, resulting in a negative value. Because the arithmetic types have limited size, it is always possible for some calculations to overflow. Adhering to the recommendations from the “Advice” box on page 38 can help avoid such problems.

The binary + and - operators may also be applied to pointer values. The use of these operators with pointers was described in Section 4.2.4 (p. 123).

The arithmetic operators, +, -, *, and / have their obvious meanings: addition, subtraction, multiplication, and division. Division between integers results in an integer. If the quotient contains a fractional part, it is truncated:

  1. int ival1 = 21/6; // integral result obtained by truncating the remainder  
  2. int ival2 = 21/7; // no remainder, result is an integral value 

Both ival1 and ival2 are initialized with a value of 3.

注意:整数乘除法的运算顺序可能改变运算结果。比如有两个int 型变量a 和b,已知它们的最大公约数是c, 那么它们的最小公倍数是a*b/c。但是这个表达式的中间结果a*b 有溢出(即超过int 的表示范围)的可能,所以我们一般把最小公倍数公式写成a*(b/c) 或者a/c*b,以避免中间结果溢出。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇5.1 Arithmetic Operators (2) 下一篇2.8 Class Types (4)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Java后端面试实习自 (2025-12-25 09:24:21)
·Java LTS版本有哪些 (2025-12-25 09:24:18)
·2025年,JAVA还值得 (2025-12-25 09:24:16)
·用 C 语言或者限制使 (2025-12-25 08:50:05)
·C++构造shared_ptr为 (2025-12-25 08:50:01)