0
0

Goのべき乗をintで出力

Posted at

べき乗は、Goのmath.Pow()で計算すると引数の型はfloat64にする必要があり、結果はそのままだとfloat64型で出てきます。

結果をintで出す方法について考え、いろいろ試して成功したやり方をご紹介します。

   var a,b,c int
    var result int
    
    a = 2
    b = 3
    c = 4
    
    result = int(math.Pow(float64((a+b+c),2))
    
    fmt.Println(result)

引数となる変数の値をfloat64型に変換してやり
Pow関数が返す結果をint型に変換してやればよいだけでした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0