一文帶你了解Go語(yǔ)言標(biāo)準(zhǔn)庫(kù)math和rand的常用函數(shù)
math 標(biāo)準(zhǔn)庫(kù)
math
標(biāo)準(zhǔn)庫(kù)提供了一些 常量如 int64
類(lèi)型的最大值、float64
類(lèi)型的最大值等,和常用的數(shù)學(xué)計(jì)算函數(shù)。
常用函數(shù)
函數(shù) | 說(shuō)明 |
---|---|
Abs(x float64) float64 | 傳入 x 參數(shù),返回 x 的絕對(duì)值 |
Max(x, y float64) float64 | 傳入 x、y 參數(shù),返回 x 與 y 中的最大值 |
Min(x, y float64) float64 | 傳入 x、y 參數(shù),返回 x 和 y 中的最小值 |
Ceil(x float64) float64 | 傳入 x 參數(shù),返回一個(gè)大于等于 x 的最小整數(shù)值,也就是向上取整 |
Ceil(x float64) float64 | 傳入 x 參數(shù),返回一個(gè)小于等于 x 的最小整數(shù)值,也就是向下取整 |
Trunc(x float64) float64 | 傳入 x 參數(shù),返回浮點(diǎn)數(shù) x 的整數(shù)部分,使用 Floor 函數(shù)也能實(shí)現(xiàn) |
Dim(x, y float64) float64 | 傳入 x、y 參數(shù),返回 x-y 與 0 中最大的值 |
Mod(x, y float64) float64 | 對(duì) x / y 進(jìn)行取余運(yùn)算 x % y |
Pow(x, y float64) float64 | 計(jì)算 x 的 y 次冪 |
Sqrt(x float64) float64 | 對(duì) x 開(kāi)平方 |
Cbrt(x float64) float64 | 對(duì) x 開(kāi)立方 |
Modf(f float64) (int float64, frac float64) | 分別取出 f 的整數(shù)部分和小數(shù)部分 |
如果想了解更多函數(shù)介紹和使用,可以到 pkg.go.dev/math 進(jìn)行查看。
Abs 函數(shù)
Abs(x float64) float64
:返回 x
的絕對(duì)值。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Abs(-3.14)) // 3.14 }
Max 函數(shù)
Max(x, y float64) float64
:返回 x
與 y
中的最大值。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Max(1.5, 2.5)) // 2.5 }
Min 函數(shù)
Min(x, y float64) float64
:返回 x
和 y
中的最小值。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Min(1.5, 2.5)) // 1.5 }
Ceil
Ceil(x float64) float64
:返回一個(gè)大于等于 x
的最小整數(shù)值,也就是向上取整。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Ceil(1.4)) // 2 fmt.Println(math.Ceil(2)) // 2 }
Floor 函數(shù)
Ceil(x float64) float64
:返回一個(gè)小于等于 x
的最小整數(shù)值,也就是向下取整。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Floor(1.4)) // 1 fmt.Println(math.Floor(1)) // 1 }
Trunc 函數(shù)
Trunc(x float64) float64
:返回浮點(diǎn)數(shù) x
的整數(shù)部分,使用 Floor
函數(shù)也能實(shí)現(xiàn)。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Trunc(1.4)) // 1 }
Dim 函數(shù)
Dim(x, y float64) float64
:返回 x-y
與 0
中最大的值。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Dim(2.0, 1.0)) // 1 fmt.Println(math.Dim(1.0, 2.0)) // 0 }
Mod 函數(shù)
Mod(x, y float64) float64
:對(duì) x / y
進(jìn)行取余運(yùn)算 x % y
。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Mod(5.0, 3.0)) // 3 fmt.Println(math.Mod(3.0, 3.0)) // 0 }
Pow 函數(shù)
Pow(x, y float64) float64
:計(jì)算 x
的 y
次冪。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Pow(2, 1)) // 2 fmt.Println(math.Pow(2, 5)) // 32 }
Sqrt 函數(shù)
Sqrt(x float64) float64
:對(duì) x
開(kāi)平方。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Sqrt(64)) // 8 fmt.Println(math.Sqrt(16)) // 4 }
Cbrt 函數(shù)
Cbrt(x float64) float64
:對(duì) x
開(kāi)立方。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Cbrt(64)) // 4 fmt.Println(math.Cbrt(8)) // 2 }
Modf 函數(shù)
Modf(f float64) (int float64, frac float64)
:分別取出 f
的整數(shù)部分和小數(shù)部分。
int
參數(shù),整數(shù)部分。frac
參數(shù),小數(shù)部分。 示例:
import ( "fmt" "math" ) func main() { integer, decimal := math.Modf(3.1415) fmt.Printf("整數(shù)部分:%.f 小數(shù)部分:%.4f", integer, decimal) // 整數(shù)部分:3 小數(shù)部分:0.1415 }
rand
rand
標(biāo)準(zhǔn)庫(kù)提供了多個(gè)獲取不同類(lèi)型隨機(jī)數(shù)的函數(shù)。
常用函數(shù)
函數(shù) | 說(shuō)明 |
---|---|
Int() int | 返回一個(gè) int 類(lèi)型的非負(fù)的偽隨機(jī)數(shù) |
Intn(n int) int | 返回一個(gè) 0 到 n 中(不包括 n)的 int 類(lèi)型的非負(fù)偽隨機(jī)數(shù) |
Int31() int32 | 返回一個(gè) int32 類(lèi)型的非負(fù)的偽隨機(jī)數(shù) |
Uint32() uint32 | 返回一個(gè) uint32 類(lèi)型的非負(fù)的偽隨機(jī)數(shù) |
Int31n(n int32) int32 | 返回一個(gè) 0 到 n 中(不包括 n)的 int32 類(lèi)型的非負(fù)偽隨機(jī)數(shù) |
Int63() int64 | 返回一個(gè) int64 類(lèi)型的非負(fù)的偽隨機(jī)數(shù) |
Uint64() uint64 | 返回一個(gè) uint64 類(lèi)型的非負(fù)的偽隨機(jī)數(shù) |
Int63n(n int64) int64 | 返回一個(gè) 0 到 n 中(不包括 n)的 int64 類(lèi)型的非負(fù)偽隨機(jī)數(shù) |
Float32() float32 | 返回一個(gè) 0.0 到 1.0 中(不包括 1.0)的 float32 類(lèi)型的非負(fù)偽隨機(jī)數(shù) |
Float64() float64 | 返回一個(gè) 0.0 到 1.0 中(不包括 1.0)的 float64 類(lèi)型的非負(fù)偽隨機(jī)數(shù) |
Seed(seed int64) | 設(shè)置隨機(jī)種子,使得每次獲取隨機(jī)數(shù)的值都不一樣 |
如果想了解更多函數(shù)介紹和使用,可以到 pkg.go.dev/math/rand 進(jìn)行查看。
代碼示例
import ( "fmt" "math/rand" ) func main() { fmt.Println(rand.Int()) // 5577006791947779410 fmt.Println(rand.Intn(10)) // 7 fmt.Println(rand.Int31()) // 1427131847 fmt.Println(rand.Uint32()) // 1879968118 fmt.Println(rand.Int31n(10)) // 1 fmt.Println(rand.Int63()) // 6334824724549167320 fmt.Println(rand.Uint64()) // 9828766684487745566 fmt.Println(rand.Int63n(10)) // 8 fmt.Println(rand.Float32()) // 0.09696952 fmt.Println(rand.Float64()) // 0.30091186058528707 }
多次運(yùn)行上述代碼,發(fā)現(xiàn)獲取到的隨機(jī)數(shù)都是一樣的,這是因?yàn)槲覀儧](méi)有設(shè)置隨機(jī)種子??梢酝ㄟ^(guò) Seed
函數(shù)進(jìn)行設(shè)置:
import ( "fmt" "math/rand" "time" ) func main() { rand.Seed(time.Now().Unix()) fmt.Println(rand.Intn(10)) }
使用 Seed
函數(shù)設(shè)置隨機(jī)種子,將當(dāng)前時(shí)間的秒數(shù)作為參數(shù)。后續(xù)多次獲取隨機(jī)數(shù)的值將不會(huì)一直一樣。
小結(jié)
本文介紹了標(biāo)準(zhǔn)庫(kù) math
和 rand
的常用函數(shù)的用法,并通過(guò)例子進(jìn)行說(shuō)明。
math
庫(kù)里雖說(shuō)有最大值和最小值比較,但是形參類(lèi)型必須是浮點(diǎn)型,如果我們想比較的是整型的最大最小值,就得自己封裝函數(shù)。
獲取隨機(jī)數(shù)時(shí),不要忘記設(shè)置隨機(jī)種子,否則多次獲取到的隨機(jī)數(shù)將會(huì)是一樣的。
以上就是一文帶你了解Go語(yǔ)言標(biāo)準(zhǔn)庫(kù)math和rand的常用函數(shù)的詳細(xì)內(nèi)容,更多關(guān)于Go語(yǔ)言math rand的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Golang實(shí)現(xiàn)圖片上傳功能的示例代碼
這篇文章主要和大家分享一下如何利用Golang實(shí)現(xiàn)圖片上傳功能,文中的實(shí)現(xiàn)步驟講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的參考價(jià)值,需要的可以參考一下2022-05-05一文詳解Golang中net/http包的實(shí)現(xiàn)原理
這篇文章主要介紹了如何用?net/http?自己編寫(xiě)實(shí)現(xiàn)一個(gè)?HTTP?Server?并探究其實(shí)現(xiàn)原理,具體講解Go語(yǔ)言是如何接收和處理請(qǐng)求的,希望能夠?qū)Υ蠹业膶W(xué)習(xí)或工作具有一定的幫助2022-08-08詳解Gotorch多機(jī)定時(shí)任務(wù)管理系統(tǒng)
遵循著“學(xué)一門(mén)語(yǔ)言最好的方式是使用它”的理念,想著用Go來(lái)實(shí)現(xiàn)些什么,剛好有一個(gè)比較讓我煩惱的問(wèn)題,于是用Go解決一下,即使不在生產(chǎn)環(huán)境使用,也可以作為Go語(yǔ)言學(xué)習(xí)的一種方式。2021-05-05使用Go語(yǔ)言實(shí)現(xiàn)遠(yuǎn)程傳輸文件
本文主要介紹如何利用Go語(yǔ)言實(shí)現(xiàn)遠(yuǎn)程傳輸文件的功能,有需要的小伙伴們可以參考學(xué)習(xí)。下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)。2016-08-08Go語(yǔ)言常見(jiàn)設(shè)計(jì)模式之裝飾模式詳解
在?Go?語(yǔ)言中,雖然裝飾模式?jīng)]有像?Python?中應(yīng)用那么廣泛,但也有其用武之地,這篇文章我們就來(lái)一起看下裝飾模式在?Go?語(yǔ)言中的應(yīng)用吧2023-07-07