以下のコードを実行するとどうなるでしょうか?
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Pow(math.NaN(), 0))
}
-
0
が出力される -
1
が出力される -
NaN
が出力される - panic になる
回答と解説
答えは「2. 1
が出力される」です。 math.Pow の定義 (Pow(x, ±0) = 1 for any x
) に従ってそうなっています。大抵の数学関数は NaN
を引数に与えると NaN
が返ってくるのですが、これは特殊なケースのようです。ちなみにこれは IEEE754-2008 でも定義されているもののようです。