📅  最后修改于: 2023-12-03 14:41:34.618000             🧑  作者: Mango
Golang 是一种编译型,静态类型的编程语言。它在语言层面上内置了很多有用的工具包,其中包括 "math" 包。这个包提供了一组基本的数学函数和常数。
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("Value of pi: %f\n", math.Pi)
fmt.Printf("Value of e: %f\n", math.E)
}
输出:
Value of pi: 3.141593
Value of e: 2.718282
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("NaN: ", math.NaN())
fmt.Println("+Inf: ", math.Inf(1))
fmt.Println("-Inf: ", math.Inf(-1))
}
输出:
NaN: NaN
+Inf: Inf
-Inf: -Inf
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("2^3: ", math.Pow(2, 3))
fmt.Println("Square root of 16: ", math.Sqrt(16))
}
输出:
2^3: 8
Square root of 16: 4
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("Sine of pi/2 radians: ", math.Sin((math.Pi / 2)))
fmt.Println("Cosine of pi radians: ", math.Cos(math.Pi))
fmt.Println("Tangent of pi/4 radians: ", math.Tan((math.Pi / 4)))
}
输出:
Sine of pi/2 radians: 1
Cosine of pi radians: -1
Tangent of pi/4 radians: 1
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("Arcsin of 1: %f\n", math.Asin(1))
fmt.Printf("Arccos of 1: %f\n", math.Acos(1))
fmt.Printf("Arctan of 1: %f\n", math.Atan(1))
}
输出:
Arcsin of 1: 1.570796
Arccos of 1: 0.000000
Arctan of 1: 0.785398
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("Sinh of 0.5: ", math.Sinh(0.5))
fmt.Println("Cosh of 0.5: ", math.Cosh(0.5))
fmt.Println("Tanh of 0.5: ", math.Tanh(0.5))
}
输出:
Sinh of 0.5: 0.52109530549
Cosh of 0.5: 1.12762596521
Tanh of 0.5: 0.46211715726
Math 包提供了很多有用的工具函数和常数,在处理数学问题时非常有用。无论是基本的幂运算和三角函数,还是高级函数如 Arc 正弦、余弦和正切,Math 包都能满足你的需求。