Mathematical Expressions · 数学表达式
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| arithmetic/əˈrɪθmətɪk/ | 算术 | suàn shù |
| arithmetic expression/əˈrɪθmətɪk ekˈspreʃn/ | 算术表达式 | suàn shù biǎo dá shì |
| order of operations/ˈɔːdə ɒv ˌɒpəˈreɪʃnz/ | 运算顺序 | yùn suàn shùn xù |
| modulus/ˈmɒdjʊləs/ | 取模 | qǔ mó |
| remainder/rɪˈmeɪndə/ | 余数 | yú shù |
| integer/ˈɪntɪdʒə/ | 整数 | zhěng shù |
Doing arithmetic in a program
- Programs often do arithmetic 算术.
- An arithmetic expression 算术表达式 combines numbers with operators: $+$, $-$, $\times$, $\div$.
- The result is usually stored in a variable for later, e.g. $\text{area} \leftarrow \text{length} \times \text{width}$.
- To get the right answer, you must apply the operators in the right order.
在程序中做算术
- 程序常常做算术(arithmetic)。
- 一个算术表达式(arithmetic expression)用运算符组合数字:$+$、$-$、$\times$、$\div$。
- 结果通常存进一个变量供以后用,例如 $\text{area} \leftarrow \text{length} \times \text{width}$。
- 要得到正确答案,你必须以正确的顺序应用运算符。
Apply the order of operations · 应用运算顺序
Multiplication and division bind tighter than addition and subtraction, and equal-rank operators go left to right. Evaluate one step at a time. · 乘法和除法比加法和减法结合得更紧,同等级运算符从左到右。一次求值一步。
What is 2 + 3 × 4? · 2 + 3 × 4 是多少?
Multiplication first: 3 × 4 = 12, then 2 + 12 = 14. · 乘法先:3 × 4 = 12,然后 2 + 12 = 14。
What is (2 + 3) × 4? · (2 + 3) × 4 是多少?
Brackets first: 2 + 3 = 5, then 5 × 4 = 20. · 括号先:2 + 3 = 5,然后 5 × 4 = 20。
Order of operations
- A computer follows the order of operations 运算顺序 — the same rules as maths.
- Do multiplication and division before addition and subtraction.
- Work left to right for operators of equal rank; do anything in brackets first.
- So $2 + 3 \times 4 = 14$ (not 20); writing $(2 + 3) \times 4$ forces the addition first, giving 20.
运算顺序
- 计算机遵循运算顺序(order of operations)——和数学一样的规则。
- 乘法和除法先于加法和减法。
- 同等级的运算符从左到右;括号里的先做。
- 所以 $2 + 3 \times 4 = 14$(不是 20);写 $(2 + 3) \times 4$ 强制加法先做,得 20。
What is 17 MOD 5 (the remainder)? · 17 MOD 5(余数)是多少?
17 ÷ 5 is 3 remainder 2, so 17 MOD 5 = 2. · 17 ÷ 5 是 3 余 2,所以 17 MOD 5 = 2。
How can a program test whether a number n is even? · 程序如何测试一个数 n 是否为偶数?
An even number leaves no remainder when divided by 2. · 偶数除以 2 没有余数。
The modulus operator
- The modulus 取模 operator (
MOD) returns the remainder 余数 of integer division. - For example $17 \bmod 5 = 2$, because $17 \div 5$ is 3 with 2 left over.
- Modulus is handy for tests: a number is even when $n \bmod 2 = 0$.
- It comes up constantly — cycling through positions, checking divisibility, and more.
取模运算符
- 取模(modulus)运算符(
MOD)返回整数除法的余数(remainder)。 - 例如 $17 \bmod 5 = 2$,因为 $17 \div 5$ 是 3 余 2。
- 取模对测试很方便:一个数在 $n \bmod 2 = 0$ 时是偶数。
- 它经常出现——循环遍历位置、检查整除性等等。
Evaluate 10 + 6 ÷ 2 × 3. · 求 10 + 6 ÷ 2 × 3。
6 ÷ 2 = 3, then 3 × 3 = 9, then 10 + 9 = 19. · 6 ÷ 2 = 3,然后 3 × 3 = 9,然后 10 + 9 = 19。
Dividing two integers can give a decimal, such as 7 ÷ 2 = 3.5. · 两个整数相除能得小数,如 7 ÷ 2 = 3.5。
Some languages give 3.5; others do integer division and give 3. · 有些语言给 3.5;另一些做整数除法给 3。
Integer vs decimal
- Think about integer 整数 versus decimal results.
- Adding two integers gives an integer, but division can give a decimal: $7 \div 2 = 3.5$.
- Some languages treat $7 \div 2$ as integer division and give $3$ — know which you get.
Evaluate $10 + 6 \div 2 \times 3$. Division and multiplication first, left to right: $6 \div 2 = 3$, then $3 \times 3 = 9$. Now add: $10 + 9 = 19$.
整数与小数
- 想想整数(integer)与小数结果的区别。
- 两个整数相加得整数,但除法能得小数:$7 \div 2 = 3.5$。
- 有些语言把 $7 \div 2$ 当作整数除法,得 $3$——要知道你得到哪种。
求 $10 + 6 \div 2 \times 3$。 除法和乘法先,从左到右:$6 \div 2 = 3$,然后 $3 \times 3 = 9$。现在加:$10 + 9 = 19$。
An arithmetic expression must follow the order of operations: brackets, then $\times \div$, then $+ -$, left to right — so $2 + 3 \times 4 = 14$. The modulus operator gives the remainder ($17 \bmod 5 = 2$), great for even/odd tests. Watch integer vs decimal division ($7 \div 2 = 3.5$ or $3$).
一个算术表达式必须遵循运算顺序:括号,然后 $\times \div$,然后 $+ -$,从左到右——所以 $2 + 3 \times 4 = 14$。取模运算符给出余数($17 \bmod 5 = 2$),很适合奇偶测试。注意整数与小数除法($7 \div 2 = 3.5$ 或 $3$)。