Go語言標(biāo)準(zhǔn)庫中math模塊詳細(xì)功能介紹與示例代碼
Go語言的 math
模塊提供了豐富的數(shù)學(xué)函數(shù)和常量,涵蓋基本運(yùn)算、三角函數(shù)、指數(shù)、對數(shù)、取整、特殊值等。以下是核心方法及示例說明:
1. 基本數(shù)學(xué)運(yùn)算
math.Abs
取絕對值(僅 float64
)。
fmt.Println(math.Abs(-3.5)) // 輸出: 3.5 fmt.Println(math.Abs(4)) // 錯(cuò)誤!需用 math.Abs(4.0)
math.Mod 和 math.Remainder
Mod
:取余數(shù)(符號與除數(shù)相同)。Remainder
:IEEE 754 標(biāo)準(zhǔn)余數(shù)(符號與被除數(shù)相同)。
fmt.Println(math.Mod(10, 3)) // 輸出: 1 fmt.Println(math.Remainder(10, 3)) // 輸出: 1 fmt.Println(math.Mod(-10, 3)) // 輸出: -1 fmt.Println(math.Remainder(-10, 3)) // 輸出: -1
math.Cbrt 和 math.Sqrt
立方根和平方根。
fmt.Println(math.Sqrt(16)) // 輸出: 4 fmt.Println(math.Cbrt(27)) // 輸出: 3
2. 三角函數(shù)
所有函數(shù)參數(shù)為弧度(非角度)。
math.Sin、math.Cos、math.Tan
rad := math.Pi / 2 fmt.Println(math.Sin(rad)) // 輸出: 1(近似值) fmt.Println(math.Cos(rad)) // 輸出: 6.123e-17(接近0)
math.Asin、math.Acos、math.Atan
反三角函數(shù)(返回弧度)。
fmt.Println(math.Asin(1)) // 輸出: 1.5708(π/2)
3. 指數(shù)與對數(shù)
math.Exp 和 math.Log
自然指數(shù)和對數(shù)。
fmt.Println(math.Exp(2)) // 輸出: ~7.389(e2) fmt.Println(math.Log(7.389)) // 輸出: ~2
math.Pow 和 math.Pow10
冪運(yùn)算。
fmt.Println(math.Pow(2, 3)) // 輸出: 8 fmt.Println(math.Pow10(3)) // 輸出: 1000
math.Log10 和 math.Log2
以10或2為底的對數(shù)。
fmt.Println(math.Log10(1000)) // 輸出: 3 fmt.Println(math.Log2(8)) // 輸出: 3
4. 取整與截?cái)?/h2>
math.Ceil 和 math.Floor
向上和向下取整。
fmt.Println(math.Ceil(3.2)) // 輸出: 4 fmt.Println(math.Floor(3.9)) // 輸出: 3
math.Trunc
直接截?cái)嘈?shù)部分。
fmt.Println(math.Trunc(3.9)) // 輸出: 3
math.Round 和 math.RoundToEven
四舍五入(RoundToEven
向最近的偶數(shù)舍入)。
fmt.Println(math.Round(2.5)) // 輸出: 3 fmt.Println(math.RoundToEven(2.5)) // 輸出: 2
5. 最大值與最小值
math.Max 和 math.Min
返回兩個(gè) float64
值的最大或最小值。
fmt.Println(math.Max(3, 5)) // 輸出: 5 fmt.Println(math.Min(3, 5)) // 輸出: 3
6. 特殊值處理
math.IsNaN 和 math.IsInf
檢查是否為非數(shù)值或無窮大。
val := math.Log(-1) // 生成 NaN fmt.Println(math.IsNaN(val)) // 輸出: true fmt.Println(math.IsInf(1/math.Inf(1), 0)) // 檢查是否無窮大
math.Inf 和 math.NaN
生成無窮大或非數(shù)值。
posInf := math.Inf(1) // 正無窮 negInf := math.Inf(-1) // 負(fù)無窮 nan := math.NaN()
7. 特殊函數(shù)
math.Hypot
計(jì)算直角三角形的斜邊長度。
fmt.Println(math.Hypot(3, 4)) // 輸出: 5
math.Gamma 和 math.Erf
伽馬函數(shù)和誤差函數(shù)。
fmt.Println(math.Gamma(5)) // 輸出: 24(4!) fmt.Println(math.Erf(1)) // 輸出: ~0.8427
8. 數(shù)學(xué)常量
常用常量
fmt.Println(math.Pi) // 輸出: 3.141592653589793 fmt.Println(math.E) // 輸出: 2.718281828459045 fmt.Println(math.Phi) // 輸出: 1.618033988749895(黃金分割)
總結(jié)
- 核心功能:
- 基礎(chǔ)運(yùn)算:
Abs
,Mod
,Sqrt
,Pow
- 三角函數(shù):
Sin
,Cos
,Asin
- 指數(shù)對數(shù):
Exp
,Log
,Log10
- 取整處理:
Ceil
,Floor
,Round
- 特殊值:
NaN
,Inf
,IsNaN
- 常量:
Pi
,E
- 基礎(chǔ)運(yùn)算:
- 注意事項(xiàng):
- 所有函數(shù)操作
float64
類型(需顯式轉(zhuǎn)換其他類型)。 - 三角函數(shù)參數(shù)為弧度(需將角度轉(zhuǎn)換為弧度:
rad = degrees * π / 180
)。 - 處理特殊值(如
NaN
)時(shí)需用math.IsNaN
判斷,不可直接比較。
- 所有函數(shù)操作
到此這篇關(guān)于Go語言標(biāo)準(zhǔn)庫中math模塊詳細(xì)功能介紹與示例代碼的文章就介紹到這了,更多相關(guān)Go語言標(biāo)準(zhǔn)庫math模塊介紹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入分析golang多值返回以及閉包的實(shí)現(xiàn)
相對于C/C++,golang有很多新穎的特性,例如goroutine,channel等等,這些特性其實(shí)從golang源碼是可以理解其實(shí)現(xiàn)的原理。今天這篇文章主要來分析下golang多值返回以及閉包的實(shí)現(xiàn),因?yàn)檫@兩個(gè)實(shí)現(xiàn)golang源碼中并不存在,我們必須從匯編的角度來窺探二者的實(shí)現(xiàn)。2016-09-09golang切片擴(kuò)容規(guī)則實(shí)現(xiàn)
這篇文章主要介紹了golang切片擴(kuò)容規(guī)則實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Golang操作mongodb的實(shí)現(xiàn)示例
本文主要介紹了Golang操作mongodb的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03詳解Go語言如何利用上下文進(jìn)行并發(fā)計(jì)算
在Go編程中,上下文(context)是一個(gè)非常重要的概念,它包含了與請求相關(guān)的信息,本文主要來和大家討論一下如何在并發(fā)計(jì)算中使用上下文,感興趣的可以了解下2024-02-02