數學

Golang 基礎: 數學

Sqrt 開根號

package main

import (
	"fmt"
	"math"
)

func main() {
	// Square root of a integer
	res := math.Sqrt(4)
	// 2
	fmt.Println(res)

	// Square root of a integer
	// 3
	res = math.Sqrt(9)
	fmt.Println(res)

	// Square Root of a float
	res = math.Sqrt(30.33)
	// 5.5072679252057455
	fmt.Println(res)

	// Square Root of a negative number
	// NaN
	res = math.Sqrt(-9)
	fmt.Println(res)
}

參考資料


亂數

Golang 基礎: 亂數