Go言語
Go言語で絶対値を取得する
引数で与えた数値の絶対値を取得します。整数の場合も、小数の場合も、mathパッケージのAbs()を使用します。
get_absolute_value.go
package main
import (
"fmt"
"math"
)
func main() {
// Get absolute value of integer
fmt.Println("Absolute value of 5 is ", math.Abs(5))
fmt.Println("Absolute value of -5 is ", math.Abs(-5))
// Get absolute value of float
fmt.Println("Absolute value of 5.5 is ", math.Abs(5.5))
fmt.Println("Absolute value of -5.5 is ", math.Abs(-5.5))
}
実行結果
$ go run get_absolute_value.go
Absolute value of 5 is 5
Absolute value of -5 is 5
Absolute value of 5.5 is 5.5
Absolute value of -5.5 is 5.5