freeCodeCamp/guide/chinese/c/math/index.md

172 lines
5.8 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Basic Math
localeTitle: 基础数学
---
# 数学在C.
C为数学提供了很多选择。我们首先从一些比较常见的开始。
## 基本的东西
以下示例不是完整的代码它们只是一段代码的示例。请记住在C中我们需要在使用之前声明所有内容 - `result` `a`和`b`将需要已经初始化并设置为值。无论是`int` `double`还是其他什么对于防止错误和信息丢失都很重要 - 我们稍后会介绍。
#### 增加: `+`
使用`+`执行添加,如下所示:
```C
result = a + b;
```
在上面的例子中,变量`result`将等于a + b。
#### 减法: `-`
添加是使用`-` ,如下所示:
```C
result = a - b;
```
在上面的例子中,变量`result`将等于a-b。
#### 乘法: `*`
使用`*`执行乘法,如下所示:
```C
result = a * b;
```
在上面的例子中,变量`result`将等于a乘以b。
#### 部门: `/`
使用`/`执行除法,如下所示:
```C
result = a / b;
```
在上面的例子中,变量`result`将等于a除以b。然而这并不总是超过b的一小部分。在处理整数时事情会有所不同 - 稍后会有所不同。
# 不那么基本的东西
## 模数:''
好的,现在事情变得有点奇怪了。
Modulo允许您在除法中找到余数。请记住对于整数我们不能有小数。分部是关于找到一个数字适合另一个数字的次数模数是关于找到遗留的东西。取27除以44适合27 6次。 6次4是24这意味着剩下3次。结果274是3. 10除以5是2,2倍5是10所以剩下0。结果105为0。
模运算符比您想象的更重要尤其是在C中强制执行浮点数和整数数之间的差异。值得拥抱。这是一个例子
```C
result = a % b;
```
在上面的例子中, `result`将等于模b。
## 整数和数学
整数不能有小数。因此当您使用整数执行除法时将截断任何类型的小数。例如17除以10是1.7。但是如果我们只处理整数那么结果将是1而不是1.7。 10次适合17次1所以答案是1。
处理浮点数时,这不是问题。浮点数可以取小数位,因此我们不必担心会被截断的事情。
### 为什么C这样做
如前所述C是一种低级语言。计算机硬件中的浮点数和整数数之间存在很大差异它们要求某些数据类型具有某些属性例如不接受小数。 C不会假设你想要什么并强迫你自己思考它。
### 为什么我们不一直只使用浮点数?
这也归结为C是一种低级语言。 C比许多其他语言更快更快更轻也是这样的原因之一因为程序员能够对数据类型做出明智的决策。请记住浮点数比整数占用更多内存。因此使用适当的数据类型很重要并在需要时处理整数到浮点的转换。
### 我们如何解决这个问题?
铸造(后面描述)是一种解决方案。另一种是使用浮点数。如果正在操作的一个或两个数字是浮点数,则结果将是浮点数。当我们开始处理操作顺序时,这变得更加复杂,但是现在,请注意这是有效的:
```C
double result = 23.0 / 2;
```
# 一个完整的例子
```C
#include <stdio.h>
// This is a comment. It gets ignored by the compiler, so we can write notes after the double slashes.
int main(void) {
int a = 3;
int b = 5;
int result = 0;
// Doing things with variables:
result = a + b;
printf("a plus b = %i \n", result);
// You can also do the operation inside the printf.
// Here's an example of that with subtraction:
printf("a minus b = %i \n", ab);
// You don't need to do this with variables at all.
// Here's an example of that with multiplication:
printf("10 times 5 = %i \n", 10*5);
// Here's a look at division.
// Notice that the decimals are truncated because we're dealing with integers.
printf("12 divided by 5 = %i \n", 12/5);
// Now let's force floating point numbers by including a decimal.
// Notice that even though these are integers, the decimal forces them to be
// treated as floating point numbers, so they aren't truncated.
// Note that I'm printing a double with %d, not an int with %i.
printf("12.0 divided by 5 = %d \n", 12.0/5);
return 0;
}
```
给出一个运行来看看会发生什么,并确保使用运算符和值来查看事物的变化和方式。
# 数学库
C提供了一个数学库 `math.h` ),它提供了许多有用的数学函数。例如,数字的幂可以计算为:
```#include<math.h>
int result = pow(2,3) // will result in 2*2*2 or 8
```
其他一些可能证明有用的( `math.h` )库函数是:
`#include <math.h> double angle = cos(90.00); // Givs us 0.00 int result = sqrt(16); // Gives us 4 double result = log(10.00) // Gives us 2.30 (note this is ln(10), not log base 10)`
// C代码来说明 //使用ceil函数。
# 包括
# 包括
int main { float val1val2val3val4;
val1 = 1.6; val2 = 1.2; val3 = -2.8; val4 = -2.3;
printf“value1 =。1lf \\ n”ceilval1; printf“value2 =。1lf \\ n”ceilval2; printf“value3 =。1lf \\ n”ceilval3; printf“value4 =。1lf \\ n”ceilval4;
返回0; }
# 在你继续之前......
## 回顾
* 有几个基本的运算符:
* `+`加法
* `-`减法
* `*`用于乘法
* `/`分裂
* 模数`%`
* 还有更多的运营商,但我们稍后会详细介绍它们。
* 整数数学是C非常严格的事情。
* C对数据类型非常严格
* 如果只涉及整数,则返回一个整数
* 如果操作中涉及浮点数,则该部分操作变为浮点
* C为`math.h`库提供了多个函数,如`pow`用于计算数字的幂。