Go 語言為基本常量和數學函數提供內置支持,以在 math 包的幫助下對數字執行運算。您可以在 math 包提供的 Hypot() 函數的幫助下找到斜邊,即 Sqrt(a*a + b*b)。因此,您需要在 import 關鍵字的幫助下在程序中添加一個數學包來訪問 Hypot() 函數。
用法:
func Hypot(a, b float64) float64
- 如果像 Hypot(+Inf, b) 或 Hypot(-Inf, b) 一樣在此函數中傳遞 +Inf 或 -Inf,則此函數將返回 +Inf。
- 如果像 Hypot(a, +Inf) 或 Hypot(a, -Inf) 一樣在此函數中傳遞 +Inf 或 -Inf,則此函數將返回 -Inf。
- 如果像 Hypot(NaN, b) 或 Hypot(a, NaN) 一樣在此函數中傳遞 NaN,則此函數將返回 NaN。
範例1:
// Golang program to illustrate hypot() function
package main
import (
"fmt"
"math"
)
// Main function
func main() {
// Finding hypotenuse
// Using Hypot() function
res_1:= math.Hypot(3, 4)
res_2:= math.Hypot(-2, 6)
res_3:= math.Hypot(4, math.Inf(1))
res_4:= math.Hypot(math.NaN(), 5)
// Displaying the result
fmt.Printf("Result 1:%.1f", res_1)
fmt.Printf("\nResult 2:%.1f", res_2)
fmt.Printf("\nResult 3:%.1f", res_3)
fmt.Printf("\nResult 4:%.1f", res_4)
}
輸出:
Result 1:5.0 Result 2:6.3 Result 3:+Inf Result 4:NaN
範例2:
// Golang program to illustrate hypot() function
package main
import (
"fmt"
"math"
)
// Main function
func main() {
// Finding hypotenuse
// Using Hypot() function
nvalue_1:= math.Hypot(3, 4)
nvalue_2:= math.Hypot(-2, 6)
res:= nvalue_1 + nvalue_2
fmt.Printf("%.5f + %.5f = %.5f",
nvalue_1, nvalue_2, res)
}
輸出:
5.00000 + 6.32456 = 11.32456
相關用法
- Golang math.Lgamma()用法及代碼示例
- Golang math.Float64bits()用法及代碼示例
- Golang atomic.AddInt64()用法及代碼示例
- Golang atomic.StoreInt64()用法及代碼示例
- Golang reflect.FieldByIndex()用法及代碼示例
- Golang string.Contains用法及代碼示例
- Golang bits.Sub()用法及代碼示例
- Golang io.PipeWriter.CloseWithError()用法及代碼示例
- Golang time.Round()用法及代碼示例
- Golang reflect.AppendSlice()用法及代碼示例
- Golang reflect.ChanOf()用法及代碼示例
- Golang flag.Bool()用法及代碼示例
- Golang time.Sleep()用法及代碼示例
- Golang time.Time.Year()用法及代碼示例
- Golang reflect.Indirect()用法及代碼示例
- Golang reflect.CanAddr()用法及代碼示例
- Golang reflect.CanInterface()用法及代碼示例
- Golang reflect.CanSet()用法及代碼示例
- Golang reflect.Cap()用法及代碼示例
- Golang strings.ContainsRune()用法及代碼示例
- Golang atomic.LoadInt64()用法及代碼示例
- Golang reflect.Index()用法及代碼示例
- Golang filepath.Abs()用法及代碼示例
- Golang filepath.Base()用法及代碼示例
注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 math.Hypot Function in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。