# 03.01-數字

Go 有幾種表示數字的型別，通常我們將數字分成整數與浮點數兩種。

#### 整數（Integers） <a href="#toc-integers" id="toc-integers"></a>

整數（類似整數在數學上的對應）是沒有小數點的數字（..., -3, -2, -1, 0, 1, ...）。電腦使用 2 進位的系統，與我們習慣使用的 10 進位的系統不同。

我們的數學系統由 10 個不同的數字組成，一旦耗盡了可用的數字，我們用 2 位數表達更大的數（再來是 3 位數、4 位數、5 位數、...）。比如，9 之後是 10、99 之後是 100，依此類推。電腦也是一樣，可是電腦只有 2 個數字而不是 10 個，所以算數就會像這樣，0, 1,, 10, 11, 100, 101, 110, 111 等。我們用的數字系統與電腦用的數字系統的另一個差別是全部的整數型別都有定義的大小，它們只對特定位數的數字提供空間，所以，一個 4 bit 的整數看起來會像這樣：0000, 0001, 0010, 0011, 0100。即使我們用完了空間，多數的電腦只會再從頭開始（這會導致一些很詭異的行為）。

Go 的整數型別是：`uint8`, `uint16`, `uint32`, `uint64`, `int8`, `int16`, `int32` 與 `int64`。8, 16, 32 與 64 讓我們知道這些型別使用了多少個 bits。`uint` 表示 "無號整數（unsigned integer）"，而 `int` 表示 "有號整數（signed integer）"。無號整數只包含正整數（與零）。此外，還有兩個別名型別（alias types）：`byte` 與 `uint8` 一樣，而 `rune` 與 `int32` 一樣。Bytes 在電腦上是很常見的量測單位（1 byte = 8 bits, 1024 bytes = 1 kilobyte, 1024 kilobytes = 1 megabyte, …），因此 Go 的 `byte` 資料型別通常用在其它型別的定義。還有三種與電腦架構有關的整數型別：`uint`、`int` 與 `uintptr`。它們都視電腦架構而定，因為它們的大小取決於你使用的架構類型。

一般而言，如果你用到整數，應該只會用到 `int` 型別。

#### 浮點數（Floating Point Numbers） <a href="#toc-floating-point-numbers" id="toc-floating-point-numbers"></a>

浮點數包含一個小數點（實數），（1.234, 123.4, 0.00001234, 12340000）。它們實際在電腦上的表示方式是相當複雜的，而且沒必要去瞭解它們的表示式。所以目前我們只需記得下列的事情：

* 浮點數是不精確的，有時候會無法表示一個數字，比如：`1.01 - 0.99` 的運算結果會得到 `0.020000000000000018`，一個相當接近我們期待的數字，但是不完全相同。
* 浮點數與整數類似，都有特定的大小（32 bit 或 64 bit）。使用一個大尺寸的浮點數可以增加它的精確度（可以表達多少位數）。
* 除了數字，還有幾個其它的值可以表示："非數字"（`NaN`，類似 `0/0` 的東西）、正無窮大及負無窮大（`+∞` 及 `−∞`）。

Go 有兩個浮點數型別：`float32` 與 `float64` （分別是單精度與雙精度），還有兩個額外的複數（有虛部的數）：`complex64` 與 `complex128`。我們一般在用浮點數時，應該無法擺脫 `float64`。

#### 範例 <a href="#toc" id="toc"></a>

我們寫個程式來示範如何使用數字，首先建立一個名為 `chapter3` 的資料夾，並建立一個名為 `main.go` 的檔案，內容如下：

```go
package main

import "fmt"

func main() {
    fmt.Println("1 + 1 =", 1 + 1)
}
```

若你執行了程式，可以看到執行結果如下：

```
$ go run main.go
1 + 1 = 2
```

注意，這個程式相當類似我們在第二章寫的程式，有一樣的 package、相同的 import、同樣的函式宣告，並使用一樣的 `Println` 函式。只是此時是將原本的 `Hello World` 字串改成印 `1 + 1 =` 及其計算結果的字串。這個運算式有三個部分：數字 `1`（型別是 `int`）、`+` 運算符（表示加法），以及另一個數值 `1`。我們試著用浮點數看看：

```
    fmt.Println("1 + 1 =", 1.0 + 1.0)
```

注意，我們用 `.0` 來讓 Go 知道這是個浮點數而不是整數，執行這個程式會得到跟之前一樣的結果。

此外，Go 還有其它的運算符：

| +  | addition             |
| -- | -------------------- |
| -  | subtraction          |
| \* | multiplication       |
| /  | division             |
| %  | <p>remainder<br></p> |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://go.netdpi.net/type/integer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
